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

Re: Salvaging lexical $_ from deprecation

Thread Previous | Thread Next
Jesse Luehrs
February 19, 2013 21:31
Re: Salvaging lexical $_ from deprecation
Message ID:
On Tue, Feb 19, 2013 at 10:27:00PM +0100, Rafael Garcia-Suarez wrote:
> On 19 February 2013 22:14, Jesse Luehrs <> wrote:
> > On the contrary, $_ is basically useless except for the global form. Or
> > are you saying that there shouldn't be any way to implement things like
> > Try::Tiny (which does something along the lines of
> > "sub catch (&) { local $_ = $@; $_[0]->() }") without using XS? The only
> > serious use of $_ I have ever seen (or used personally) is when $_ is
> > implicitly localized by some other code (whether that code is perl
> > itself in a for loop, or in some other module). This cannot work when $_
> > is lexical, and making $_ lexical in fact breaks any code like that.
> > "my $_ = 'foo'; my @bar = any { $_ eq 'a' } qw(a b c)" for instance.
> > That is why it has been deprecated.
> You're pointing at an incomplete implementation to dismiss the whole
> feature. This short-sighted criticism might be valid for bug fixes, not
> for new language features.

I don't know how you would change the implementation without making it
no longer lexical. The whole point of lexical scope is that code outside
of the point of declaration cannot see the value at all. What are you
referring to here?

> > Also on the contrary, both @_ and $1 are localized, not lexicalized.
> I agree for @_. I also agree that @_ is a kludge, please see the number
> of bugs reported over the years about the stack not being refcounted;
> so do not take a kludge as an example.
> But the $<digit> variables are implicitly lexically scoped for a definition
> of scope that includes a last sight of a pattern match. In other contexts
> their behaviour is not guaranteed.

But that isn't what "lexically scoped" means.

  $ perl -E'sub foo { say $1 } "foo" =~ /(.)/; foo'

$1 in this example is not in the lexical scope of the pattern match by
any definition of the word "lexical". This is dynamic scope, which is a
different thing entirely.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About