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

Salvaging lexical $_ from deprecation

Thread Next
Rafael Garcia-Suarez
February 19, 2013 21:07
Salvaging lexical $_ from deprecation
Message ID:
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

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 $_".

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.

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.)

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