Also take a look at push, pop, and unshift.
These are also used (though not as often as shift).
Shift is used the most because it removes and returns the next item in an array. This is ideal for subroutines, where each call to shift() gets the next argument passed to the subroutine.
Example:
#!/usr/bin/perl
use strict;
&doIt("foo", "bar", "foobar");
sub doIt() {
print shift, "\n"; #prints "foo"
print shift, "\n"; #prints "bar"
print shift, "\n"; #prints "foobar"
}
Or, if you are demented in the head, you could use pop() and pass the arguments backwards:
#!/usr/bin/perl
use strict;
&doIt("foo", "bar", "foobar");
sub doIt() {
print pop, "\n"; #prints "foobar"
print pop, "\n"; #prints "bar"
print pop, "\n"; #prints "foo"
}
Though as far as subroutines go, I have been getting used to using 'named arguments' I suppose you might call them:
#!/usr/bin/perl
use strict;
&doIt(-first => "foo", -second => "bar", -third => "foobar");
sub doIt() {
my %params = @_; #turns the passed arguments into an array
print $params{'-first'}, "\n"; #prints "foo"
print $params{'-second'}, "\n"; #prints "bar"
print $params{'-third'}, "\n"; #prints "foobar"
}
|