T
Tim McDaniel
Currently I do
local $_;
at the start of each sub, but it's an annoyance and it's not
fail-safe (I can forget).
Just found out about Perl 5.10. Pretty cool.
Lexical $_
The default variable $_ can now be lexicalized, by declaring it
like any other lexical variable, with a simple
my $_;
The operations that default on $_ will use the lexically-scoped
version of $_ when it exists, instead of the global $_.
In a map or a grep block, if $_ was previously my'ed, then the $_
inside the block is lexical as well (and scoped to the block).
In a scope where $_ has been lexicalized, you can still have
access to the global version of $_ by using $::_, or, more simply,
by overriding the lexical declaration with our $_. (Rafael
Garcia-Suarez)
I like the convenience of the implicit USE of $_ in things like
"s/foo/bar/", but I've had an abhorrence of implicitly-declared
variables since FORTRAN days, and hatred of accidental variable
leakage since C.
Is there any way that I can dispose of any possibility of accidentally
using the global dynamic variable $_? That is, where by default
foreach (@array)
errors out (ideally at compile time), requiring that I do
foreach my $_ (@array)
or declare it per sub or whatever I like?
Should I look into playing with main's package symbol table (I know
nothing about the subject other than that it exists)?
local $_;
at the start of each sub, but it's an annoyance and it's not
fail-safe (I can forget).
Just found out about Perl 5.10. Pretty cool.
Lexical $_
The default variable $_ can now be lexicalized, by declaring it
like any other lexical variable, with a simple
my $_;
The operations that default on $_ will use the lexically-scoped
version of $_ when it exists, instead of the global $_.
In a map or a grep block, if $_ was previously my'ed, then the $_
inside the block is lexical as well (and scoped to the block).
In a scope where $_ has been lexicalized, you can still have
access to the global version of $_ by using $::_, or, more simply,
by overriding the lexical declaration with our $_. (Rafael
Garcia-Suarez)
I like the convenience of the implicit USE of $_ in things like
"s/foo/bar/", but I've had an abhorrence of implicitly-declared
variables since FORTRAN days, and hatred of accidental variable
leakage since C.
Is there any way that I can dispose of any possibility of accidentally
using the global dynamic variable $_? That is, where by default
foreach (@array)
errors out (ideally at compile time), requiring that I do
foreach my $_ (@array)
or declare it per sub or whatever I like?
Should I look into playing with main's package symbol table (I know
nothing about the subject other than that it exists)?