in reply to using shift and $_ in loop

You never assigned to $_. The equivalent of
sub scribble { while ( $a = shift ){ $a; # has arg } }
that assigns to $_ is
sub scribble { while ( $_ = shift ){ $_; # has arg } }

In both cases, you are using global variables. $_ is particularly dangerous to clobber. Fix:

sub scribble { local $_; while ( $_ = shift ){ $_; # has arg } }

Replies are listed 'Best First'.
Re^2: using shift and $_ in loop
by leocharre (Priest) on Oct 21, 2009 at 15:59 UTC
    Thank you, this is really useful. So, you suggest localizing $_ as a precaution?
      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)