|
|
|
|
|
by kbenson
4785 days ago
|
|
It's also lexicalized in every loop, so writing to it doesn't affect anything else. The only time I can think of you would have to worry about this is if some function modified $_ without any loop, and I would consider that a bug (similar to any function that set a static class variable indiscriminately). For example, the following code performs as expected, and prints "123" use 5.016;
use warnings;
my @a = qw(1 2 3);
for (@a) {
my @b = qw(4 5 6);
for (@b) {
$_ = 1;
}
print;
}
This code works (prints "123") fine as well: use 5.016;
use warnings;
sub test {
my @b = qw(4 5 6);
for (@b) {
$_ = 1;
}
}
my @a = qw(1 2 3);
for (@a) {
test;
print;
}
The following causes a problem (prints "111"). Don't do this, it's stupid. use 5.016;
use warnings;
sub test {
$_ = 1;
}
my @a = qw(1 2 3);
for (@a) {
test;
print;
}
The moral? Buggy libraries are buggy, don't use them, or submit a fix. |
|
And yes, I both fixed my coworker's code to not use $_, and submitted a patch to the CPAN library with the bug. But the experience taught me to be cautious about $_.