Perl Script : List assignment

List assignment
When a list of variables appears on the left side of an assignment, Perl performs list assignment. The right side is evaluated in list context, and then the first element of the result is assigned to the first variable, the second element to the second variable, and so on.

A common use of this feature is to assign values from a parameter list to local variables.

The following subroutine assigns the first parameter to p1, the second to p2,
and a list of the remaining parameters to @params.
1.9 The shift operator 7
sub echo {
my ($p1, $p2, @params) = @_;
print "$p1 $p2 @params\n";
}

The argument of print is a double-quoted expression that uses variable interpolation to display the values of the parameters. This sort of print statement is often useful for debugging. Whenever there is an error in a subroutine, I start by printing the values of the parameters.

No comments:

Post a Comment