Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: I should've known better!

by vitoco (Friar)
on Sep 10, 2009 at 22:32 UTC ( #794659=note: print w/ replies, xml ) Need Help??


in reply to I should've known better!

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 ];


Comment on Re: I should've known better!
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://794659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-23 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls