in reply to Re^2: using shift and $_ in loop
in thread using shift and $_ in loop

Yeah, don't clobber your caller's variables.

Replies are listed 'Best First'.
Re^4: using shift and $_ in loop
by leocharre (Priest) on Oct 21, 2009 at 20:26 UTC
    Don't clobber your caller's variables.. so.. in ..
    sub a { @_; $_; }

    $_ is .. what of the caller?

    And am I to understand that in sub a, messing with @_, regardless if the caller passed any references as arguments, is potentially messing with the caller's arguments?

    (thank you for your patience here)

      $_ is .. what of the caller?

      $_ is $_. No telling how the caller is using it.

      And am I to understand that in sub a, messing with @_, regardless if the caller passed any references as arguments, is potentially messing with the caller's arguments?

      Easy to test...

      Not quite. Messing with the elements of @_ will. Adding elements and removing elements (including assigning to @_ specifically) won't.

      foo($x, $y+1, $z);
      is basically equivalent to
      { local @_; alias $_[0] = $x; alias $_[1] = $y+1; alias $_[2] = $z; &foo; }

      Changing $_[0] in foo will change $x, for example.