"I read the unshift and sub documentation again and I still don't understand your hint."
Ah I meant shift of course, apologies for the confusion.
As far as I understand the @_ is a list that contains all arguments the sub was called with and consequently $_[x] would contain the element of that array with index x.
Correct.
while I think shift only makes sense if something might or might not be present at a specific index after 0
...
I want to continue processing @_ and am unsure about indexes and/or presence
Not sure what you mean by that. shift removes the first value off the list, so all remaining elements of the list move one index to the left (that's why it's called "shift"):
my @array = (2, 4, 6, 8); my $first = shift @array; # $first is 2 # @array is (4, 6, 8) my $second = shift @array; # $second is 4 # @array is (6, 8) # $array[0] is 6 # $array[1] is 8
It works exactly the same when used on @_ (which is the default when shift is called without argument).
my ($bar_ref) = @_; #same as above but I can not process anything else from the list
Not sure what you mean by that, either. Unlike shift, the assignment does not modify @_. Its effect is identical to that of:
my $bar_ref = $_[0];
It's just a more "stream-lined" way of writing it.
When both sides of an assignment are lists, Perl goes from left to right, assigning the first element of the RHS ("right-hand-side") to the first element of the LHS, then the second RHS element to the second LHS element, and so on, for as many elements as there are on the LHS:
my ($first) = (2, 4, 6, 8); my ($first, $second) = (2, 4, 6, 8); # an array on the LHS will "eat up" all remaining elements: my ($first, $second, @remaining) = (2, 4, 6, 8); # undef can be used to "skip" elements you don't want to assign # to anything: my (undef, undef, $third) = (2, 4, 6, 8);
In reply to Re^4: RFC: beginner level script improvement (various comments)
by smls
in thread RFC: beginner level script improvement
by georgecarlin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |