Perl Script : The shift operator

The shift operator
Another way to do the same thing (because in Perl there's always another way to do the same thing) is to use the shift operator.
shift takes an array as an argument and does two things: it remove the first element of the list and returns the value it removed. Like many operators, shift
has both a side effect (modifying the array) and a return value (the result of the operation).
The following subroutine is the same as the previous one:
sub echo {
my $p1 = shift @_;
my $p2 = shift @_;
print "$p1 $p2 @_\n";
}
If you invoke shift without an argument, is uses @ by default. In this example,
it is possible (and common) to omit the argument.

No comments:

Post a Comment