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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.