Two features that I have previously discouraged are now gone from Perl. The lexical $_
and auto dereferencing.
The lexical $_
was a consequence of the way Perl wanted smart match to work. In a given-when
, instead of aliasing $_
like foreach
does, the block had an implicit my $_ = ...
. This interfered with the package version, as I wrote about in Use for() instead of given() and Perl v5.16 now sets proper magic on lexical $_. In Effective Perl Programming, we initially promoted this feature in Item 24: Use given-when to make a switch statement and Item 15: Use $_ for elegance and brevity. Our advice on lexical $_
is dated and contrary to what we suggested for years. Now, with v5.24, it doesn’t matter because the feature is gone. (Reini Urban has more to say about this in The removal of the lexical topic feature in 5.24).
The other feature, automatic dereferencing for array and hash operators, I was never sold on. I’m not even settled on the notion that keys
or values
should be able to operate on both (chromatic wrote about it in Inadvertent Inconsistencies: each in Perl 5.12). This feature from v5.14 let you give you pass a scalar variable to the array and hash operators. You had to trust that the right sort of value would end up there since Perl could not check this at compile-time:
my @keys = keys $hash_or_array_reference;
I discouraged this in Don’t use auto-dereferencing with each or keys. The feature was confused even when it was stable, as you can read in Bug #80626 for perl5: More problems with push/keys $scalar. You can read the conversation about removing it in Bug #119437 for perl5: [EXPERIMENT] autoderef, the implicit deref in push REF and others. Now it’s gone too.
Lexical $_ is now gone because they couldn’t find the easy to spot
old OA_TARGLEX bug with the TARGET_MY optimization, which was only fixed in cperl in August 2015.
https://github.com/perl11/cperl/commit/597e929c930304418f422b0c01683da6f285587c
So given-when and smartmatch will also die soon, as they are not able to fix those bugs neither, and will not come with a proper design.
The autodereference of hash and array refs was a problematic design fart, which is thanksfully gone now.
Very well. given-when will die. Then what should we use as simple (not overcomplicated smartmatch) “switch” operator?
You only think you need a switch operator. You can easily make the same thing with basic Perl. The
when
works inside aforeach
loop too.