I have this problem: to capture two strings into an URL, but if the first string have the 'video' string, I need that $1 and $2 return '' (o undefined).
foreach my $url (
'<a href="/story/43480/">The Bottled Water Lie</a>',
'<a href="/story/video/43480/">The Bottled Water Lie</a>',
) {
print " $url\n";
if ( $url =~ m{href="(.+)">(.+)</a>} ) {
print "O1: $1\n";
print "O2: $2\n";
if ( $1 !~ /video/ ) {
print "Y1: $1\n";
print "Y2: $2\n";
}
else {
print "N1: $1\n";
print "N2: $2\n";
}
print "F1: $1\n";
print "F2: $2\n";
}
print "L1: $1\n";
print "L2: $2\n";
}
Output:
<a href="/story/43480/">The Bottled Water Lie</a>
O1: /story/43480/
O2: The Bottled Water Lie
Y1: /story/43480/
Y2: The Bottled Water Lie
F1: /story/43480/
F2: The Bottled Water Lie
L1: /story/43480/
L2: The Bottled Water Lie
<a href="/story/video/43480/">The Bottled Water Lie</a>
O1: /story/video/43480/
O2: The Bottled Water Lie
N1:
N2:
F1:
F2:
L1: /story/video/43480/
L2: The Bottled Water Lie
Ok. This work. The second $url don't show nothing... but... into the else part (N1 & N2 lines) shown that match variables are reset to undef. And the F1 & F2 lines show undefined vars also.
perlre say:
The numbered match variables ($1, $2, $3, etc.) and the related punctuation set ($+ , $& , $` , $' , and $^N ) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first. (See "Compound Statements" in perlsyn.)
NOTE: failed matches in Perl do not reset the match variables, which makes it easier to write code that tests for a series of more specific cases and remembers the best match.
But the second 'if' reset the match variables in a failed test.
So... I need a very deep explanation of this mystery, please.
Anyway, I reduced the problem to
$url =~ m{href="(.+)">(.+)</a>} and $1 !~ /video/;
print "$1 $2 \n";
but I don't know how it is working, also :-(