in reply to Re^3: Small troubles with references
in thread Small troubles with references

I'll bite. I'm not a Perl newbie, but there are certainly holes in my largely self-taught knowledge of Perl. I would guess that the reason the change to the array isn't permanent when it is passed by value is because of lexical scoping ... but the idea of the passed parameter being an alias under the covers is confusing to me. Would anyone care to explain?


No good deed goes unpunished. -- (attributed to) Oscar Wilde

Replies are listed 'Best First'.
Re^5: Small troubles with references (aliases)
by demerphq (Chancellor) on Feb 09, 2006 at 23:17 UTC

    Consider

    sub foo { my $x=shift; $x="Changed1"; $_[0]="Changed2"; } my $i="Orig1"; my $j="Orig2"; print "$i,$j\n"; foo($i,$j); print "$i,$j\n"; __END__ Orig1,Orig2 Orig1,Changed2

    Assigning to $_[$x] is the same as assigning to the original value that was passed into the subroutine. This is because its an alias. Its not a copy. IOW, its where two or more variable names refer to the same memory location. Its much as if you did

    sub bar { my $ref=shift; $$ref='changed' } bar(\$j);

    Except you dont have to deref before you do the assignment.

    for() aliases, as does map, and a few other places where it can be useful to have this behaviour.

    ---
    $world=~s/war/peace/g

Re^5: Small troubles with references
by runrig (Abbot) on Feb 09, 2006 at 23:15 UTC
    @_ is an array of aliases to the argument list. Try:
    sub inc_list { $_++ for @_; } my @arr = (1, 2); inc_list(@arr); print "@arr\n"; # prints "2 3" (untested)