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

Thank you, this is really useful. So, you suggest localizing $_ as a precaution?

Replies are listed 'Best First'.
Re^3: using shift and $_ in loop
by ikegami (Patriarch) on Oct 21, 2009 at 16:37 UTC
    Yeah, don't clobber your caller's variables.
      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.