Subject: | Use of local $_ |
In case you didn’t know, local($_) is evil. It interferes with a caller’s tied $_. This was
supposedly fixed in Perl 5.14, but is actually still half broken. See
<https://rt.perl.org/rt3/Ticket/Display.html?id=105912>. In earlier versions, it’s fully
broken. (And Test::Simple still runs on earlier versions.)
It actually surprisingly easy to run into this when an object provides a hash-like interface:
for($thing->bar->baz->{foo}) {
$_->frobnicate(0);
$_->primble(1);
$_->turnamate;
}
I downloaded MSCHWERN/Test-Simple-1.005000_002.tar.gz and ran ack on it:
$ ack 'local\s*\(?\s*\$_'
lib/TB2/Mouse.pm
691: local $_ = $args[0];
1573: local $_ = $thing;
1614: local $_ = $value;
lib/Test/Builder/Tester.pm
260: local $_;
340: local $_;
t/test.pl
168:# Trying to avoid setting $_, or relying on local $_ to work.
You ought to be using for($thing) instead. If you need to set it to a new variable that you can
then subsequently modify, use for(my $dummy) { ... } or local *_ = \my $dummy;