I found a possible bug in perl’s closures and $1 regexp variables. Simply, they don’t mix together.
Let’s take this code.
use warnings;
while ("1234567890"=~/(.)/sg) {
push @subs, sub{print $1;};
}
for (@subs) {$_->()}
You would imagine that perl will now print all the numbers – instead, I got 10 warnings from undefined $1.
Is it really a bug, or did I just missed something in perl documentation? Is there some reason, why should $1 be undefined and NOT being part of the closure?
I think the answer is similar to the answer to perl closures and $_.
$1is a global variable, too.What you need to do is: