in reply to Re: grep2 (like 'grep', but also return non-matches)
in thread grep2 (like 'grep', but also return non-matches)

Could I please ask that anyone who plays with $_ like this ... please localise it?

sub part(&@) { my $code=shift; my @ret; local $_; for(@_) { my $i=$code->($_); next unless defined $i; push @{$ret[$i]}, $_; } return @ret; }

Thanks. There are some really hard-to-figure-out bugs that creep up on people when the subs they call go and affect $_ like this.

Tanktalus, twice bitten, once shy. Or something like that.

Replies are listed 'Best First'.
Re^3: grep2 (like 'grep', but also return non-matches)
by BrentDax (Hermit) on Jan 24, 2005 at 19:50 UTC

    perlsyn sez about foreach:

    ...the variable is implicitly local to the loop and regains its former value upon exiting the loop.

    So I think this is quite safe.

    =cut
    --Brent Dax
    There is no sig.