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

Re: Salvaging lexical $_ from deprecation

Thread Previous | Thread Next
From:
Jesse Luehrs
Date:
February 19, 2013 21:14
Subject:
Re: Salvaging lexical $_ from deprecation
Message ID:
20130219211416.GO23176@tozt.net
On Tue, Feb 19, 2013 at 10:07:08PM +0100, Rafael Garcia-Suarez wrote:
> Hi Porters,
> I notice that the lexical topicalizer "my $_" is deprecated in blead.
> At this point I assume that the reasons for the deprecation given in
> perl5177delta are exhaustive. All those reasons are pointing to
> implementation problems, notably at the XS interface level, which can
> be solved; there are no reasons given that pertain to language design
> issues.
> 
> In my opinion, the more important problem here is the impression that
> P5P is throwing away without much thought of a perfectly nice and
> modern language feature (for some value of modern that means
> "post-FORTRAN"). This could give the impression of a the lack of
> vision for Perl 5 (and reinforce the "perl is dead" death spiral as
> perceived by the outside world -- the Perl users).
> 
> While I totally understand that failed experiments in language design
> should be deprecated and removed (like the ~~ operator and the
> given/when keywords), I'm yet to be convinced that this is the case
> for "my $_". Lexical $_ could be marked experimental, as a number of
> other features are, but there is no reason to mark it deprecated. At
> the very contrary there are excellent reasons to keep working on
> improving it.
> 
> Let me advocate once again this language construct. $_ (or the ability
> to not specify an implicit topic, like in natural languages) is an
> essential part of the perlishness of perl. There is no intrinsic value
> in having $_ as a global at all; by essence a topic is local to its
> context. However $_ is global because, being a Perl 1 feature, it
> predates the very existence of lexical variables. In that aspect $_ is
> fundamentally different from most other punctuation variables, used to
> globally tune the interpreter, or give access to global or external
> resources ($0, $$, $^V, $^D, etc).
> 
> So, to be clear: $_ is still global by default due to the need for
> backwards compatibility. If something should be deprecated, it should
> be plain $_, not "my $_".

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.

> Having a global $_ creates a mismatch between the implementation (the
> _ glob) and the language design intent behind $_. In practice that is
> revealed by confusing action-at-distance bugs (either on $_ itself or
> via *_; or having "local $_" not working as intended).
> 
> Other punctuation variables do not suffer from this drawback. For
> example the $1, $2... capture variables are already automatically
> lexicalised. @_ is another piece of dubious implementation, but at
> least it's not a global either. I'll also add that for/foreach does
> the right thing with $_ (this remark does not include "given") and
> that it's an old but first step in the good direction.

Also on the contrary, both @_ and $1 are localized, not lexicalized.

> In conclusion: think first about language design; think about having a
> vision for the future. Don't throw away language features just because
> they introduce difficult bugs you'd rather sweep under the rug.
> Deprecating $_ is laziness and impatience of the worst kind; do not
> forget to also display hubris, because only hubris makes the future
> promises shine.
> 
> Incidentally, I hastily pushed a branch rgs/undeprecate-lex-topic to
> remove the deprecation warning (since this has to be merged really
> quick before the deprecation escapes to a stable release.)

-doy

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About