When I got your post, I was reading (again) perlsub to check that it's the same to pass an array by reference than by value to save memory, because of aliasing magic built in @_.
Fortunately, shifting or directly assigning values into my variables, breaks that internal linking, so modifying them won't alter the source of the parameters as in foreach, but it exists the possibility power to do that by touching @_ directly.
Unfortunately, assigning @_ to a local variable just to write clear code, all the parameters array will be duplicated.
A test on this topic...
#!perl -w use strict; use warnings; use Data::Dumper; my @a = (1, 2, 3); my @b = (4, 5, 6); my @s = foo(@a, @b); print Dumper(\@a, \@b, \@s); sub foo { my ($t, $u) = (@_); # $t = 1 ; $u = 2 $u = undef; # $a[1] == 2 my $i = shift; # $i = 1 my $j = shift; # $j = 2 $_[0] = undef; # $a[2] = undef my $k = shift; # $k = undef $_[0] = undef; # $b[0] = undef my $x = $_[0]; # $x = undef $k = $i + $j; # $k = 1 + 2 ; $a[2] == undef $x = $i - $j; # $x = 1 - 2 ; $b[0] == undef my $y = shift; # $y = undef my @z = @_; # @z = [5, 6] $z[0] = undef; # @z = [undef, 6] ; $b[1] == 5 return $t,$u,$i,$j,$k,$x,$y,@z; } __END__ $VAR1 = [ 1, 2, undef ]; $VAR2 = [ undef, 5, 6 ]; $VAR3 = [ 1, undef, 1, 2, 3, -1, undef, undef, 6 ];
In reply to Re: I should've known better!
by vitoco
in thread I should've known better!
by ack
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |