perlquestion
doran
I had (apparently mistakenly) thought that a backreference to a parenthesized substring in a regexp would be undefined or at least equal to '' if it didn't match, so that in:
<code>
#!/usr/bin/perl -Tw
use strict;
$|++;
my $valid ='realgoodname';
my $invalid ='bad name';
my ($test1,$test2);
if ($valid =~ /^([a-z]+)$/i){
$test1=$1;
}
{
$invalid =~ /^([a-z]+)$/i;
$test2=$1;
die "Invalid test2" unless $test2;
}
print "Valid word\t= $valid\nInvalid word\t= $invalid\n\n";
print "Valid test\t= $test1\nInvalid test\t= $test2\n";
exit();
</code>
$test2 would always be left without a value (causing the script would die in this case). I certainly thought this given the <code>$test1=$1;</code> line was in a block after that <i>if</i> statement. Plus I created another block by throwing that pair of braces around the taint-checking/untainting of $invalid and $test2. This feeling was re-enforced when I checked the Camel book which sez: <p><i>"The variables $1, $2, $3, ... are automatically localized, and their scope...extends to the end of the enclosing block or eval string, or to the next successfull pattern match, whichever comes first."</i>
<p>
Because of this I felt relatively safe in throwing a pair of braces around these tests, thinking that the backreference would never escape my enclosing block.<p>
However, it seems that the block after the <i>if</i> statement isn't enough. In the above example, $test2 is left with 'realgoodname' as a value. If I throw <i>another</i> block around the <i>if</i> statement, it seems to fix it.<p>
I guess my question is: How is it that $1 (in this case) can still be 'seen' outside of it's enclosing block? True, the second pattern match wasn't successful, but I sure thought that closing brace after the <i>if</i> statement would have prevented the previous value of $1 from carrying over to the next use of $1.
<p>
If this is clearly documented someplace, feel free to point me in the correct direction. -Thanks