The use of 'my' doesn't change the way parens (or lack thereof) affect the context of the variables. You've figured out that shift returns a single scalar: the left-most item in @array. The difference between that and the 2nd method above (what you were using previously), is that the array itself is unchanged. Shift modifies it, so subsequent calls will get you something different, whereas the assignment in the list context above will always give you the same values.@array = qw/ a b c /; $scalar = @array; # puts @array into scalar context print $scalar; # '3' ($scalar) = @array; # forces @array into list context print $scalar; # 'a' ($a, $b, $c) = @array; # likewise print "$a, $b, $c"; # 'a, b, c'
This also is what allows us to do things like this:
($a, $b) = ($b, $a); # swap print "$a, $b, $c"; # 'b, a, c'
In reply to Re: using 'my $var' vs. 'my($var)'
by Fastolfe
in thread using 'my $var' vs. 'my($var)'
by decnartne
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |