IMO, one great thing to come out of lexical $_
is the new _
prototype symbol.
This allows you to specify a subroutine so that it will take one scalar or if none is provided it will grab $_
.
So instead of writing:
sub foo {
my $arg = @_ ? shift : $_;
# Do stuff with $_
}
I can write:
sub foo(_) {
my $arg = shift;
# Do stuff with $_ or first arg.
}
Not a big change, but it's just that much simpler when I want that behavior. Boilerplate removal is a good thing.
Of course, this has the knock on effect of changing the prototypes of several builtins (eg chr
), which may break some code.
Overall, I welcome lexical $_
. It gives me a tool I can use to limit accidental data munging and bizarre interactions between functions. If I decide to use $_
in the body of a function, by lexicalizing it, I can be sure that whatever code I call, $_
won't be modified in calling code.
Dynamic scope is interesting, but for the most part I want lexical scoping. Add to this the complications around $_
. I've heard dire warnings about the inadvisability of simply doing local $_;
--that it is best to use for ( $foo ) { }
instead. Lexicalized $_
gives me what I want 99 times out of 100 when I have localized $_
by whatever means. Lexical $_
makes a great convenience and readability feature more robust.
The bulk of my work has had to work with perl 5.8, so I haven't had the joy of playing with lexical $_
in larger projects. However, it feels like this will go a long way to make the use of $_
safer, which is a good thing.