P
Peter J. Holzer
But they're different scopes: "my" is lexical scopes but the implicit
declaration is dynamic scope.
1) I know.
2) Starting with 5.10: Not necessarily:
#!/usr/local/bin/perl5.10.0
use warnings;
use strict;
sub foo {
$_ = 'foo';
}
sub bar {
my $_ = 'bar';
for (1, 2, 3) {
print "$_\n";
foo();
print "$_\n";
}
print "$_\n";
}
bar();
(So adding "my $_" to the start of each file, as somebody in this
thread suggested, does indeed help against "weird" bugs)
Dynamic Scope Considered Harmful.
If each modification of $_ is surrounded with an (explicit or implicit)
local $_ with the smallest possible scope, there is little practical
difference.
The problematical constructs (i.e. those where you have to remember to
add a local $_) are "while (<>)" and direct assignment. Foreach, grep,
and map already add a scope by themselves. It should not be necessary
to make them more verbose.
hp