Update: As ikegami illustrates below, neosamurai and I were in agreement about the answer to the wrong question. It did seem a bit odd that someone as knowledgeable as brian_d_foy would be asking a question with an answer that seemed so clear to me...
My original post remains below.
neosamurai is right. $& goes out of scope when redo moves code execution back to the top of the block. redo doesn't reevaluate the loop's conditional (where we set $&), so a new value for $& is never set.
There doesn't seem to be anything special about a for loop, as the same behavior can be seen in a while loop.
for( $_ = 'fred'; print "loop\n" and s/(.)//; ) {
print qq'I saw [$&]\n';
redo if $& eq 'e';
}
__END__
loop
I saw [f]
loop
I saw [r]
loop
I saw [e]
I saw []
loop
I saw [d]
loop
$_ = 'fred';
while( print "loop\n" and s/(.)// ) {
print qq'I saw [$&]\n';
redo if $& eq 'e';
}
__END__
loop
I saw [f]
loop
I saw [r]
loop
I saw [e]
I saw []
loop
I saw [d]
loop
|