Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: grep2 (like 'grep', but also return non-matches)

by Tanktalus (Canon)
on Jan 24, 2005 at 19:39 UTC ( #424681=note: print w/replies, xml ) Need Help??

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.

    --Brent Dax
    There is no sig.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://424681]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-28 23:45 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.