develooper Front page | perl.perl5.porters | Postings from February 2013

Re: Salvaging lexical $_ from deprecation

Thread Previous | Thread Next
Darin McBride
February 26, 2013 15:59
Re: Salvaging lexical $_ from deprecation
Message ID:
On Wednesday February 20 2013 9:45:19 AM Ed Avis wrote:
> Jesse Luehrs <doy <at>> writes:
> >$_ is basically useless except for the global form.
> >
> >"my $_ = 'foo'; my @bar = any { $_ eq 'a' } qw(a b c)"
> Might it be better for higher-order functions like map, any, and sort to
> pass the argument as an argument, rather than in $_?  Unfortunately there
> is a lot of existing code that relies on global $_ as you point out.

Please, no.  One of the nice features here, IMO, is that a "topic" is set and 
we can just use it.  (I'd _almost_ argue for 'eq' and kin to accept the topic, 
making the above "my @bar = any { eq 'a' } qw(a b c)", but that's too far.)

my $has_vowel = any { /[aeiou]/ } @list;

Sometimes, it makes sense to pass in a parameter.  But for small uses like 
this, a localised $_ is awesome.

Now, I grant, sometimes I have a map inside a map, or an any inside a map, and 
the repeated $_'s gets annoying, but I've found that to be rare.  I'd rather 
be forced to write "my $topic = $_;" at the top of the outer map in the rare 
case than to lose $_ for the common case.

As to the original topic, I'd love to see lexical $_ stay.  It's a good idea 
in the abstract.  Unfortunately, when brought into the concrete of reality, I 
personally don't see how it can stay when mixed with closures like this.  But 
please let's not through out the $_ baby with the bathwater here :)
Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About