in reply to Re: Scope of regular expression variables
in thread Scope of regular expression variables

Uh, not quite. It's because it's a failed match. Apparently, the block that localizes the match variables doesn't begin until after the open curlies (and does not include the boolean expression of the if). Check this:
$_ = "abcde"; if (/(.)(.)/) { # first two if (/(.)(.)$/) { # last two, by your theory won't upset print "inner: $1 $2\n"; } # by your theory, should see first two again print "outer: $1 $2\n"; }
for which I get
inner: d e outer: d e
But if you add a block:
$_ = "abcde"; if (/(.)(.)/) { # first two { # ADDED if (/(.)(.)$/) { # last two, by your theory won't upset print "inner: $1 $2\n"; } } # ADDED # by your theory, should see first two again print "outer: $1 $2\n"; }
we do in fact get the expected:
inner: d e outer: a b

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: RE: Re: Scope of regular expression variables
by takshaka (Friar) on Aug 30, 2000 at 02:35 UTC
    It works that way for manual localization in the conditional as well. That's fairly counterintuitive considering that my() does the opposite.
    ($one, $two) = (1,2); { if (local ($one, $two) = qw(abc def)) { print "local inner : $one $two\n"; } print "local outer : $one $two\n"; } print "local outside: $one $two\n"; my ($a, $b) = (1,2); { if (my ($a, $b) = qw(abc def)) { print "my inner : $a $b\n"; } print "my outer : $a $b\n"; } print "my outside: $a $b\n"; __END__ local inner : abc def local outer : abc def local outside: 1 2 my inner : abc def my outer : 1 2 my outside: 1 2
RE: RE: Re: Scope of regular expression variables
by Carl-Joseph (Scribe) on Aug 30, 2000 at 03:47 UTC

    The behavior of the regex vars is similar to what is obtained by using "local" inside the if() conditional.

    I've modified Merlyn's code to show that the value of regex vars is sustained through a function call.

    sub PrintRegExVar { print "Inside called sub: $1 $2\n"; } $_ = "abcde"; if (/(.)(.)/) { # first two if (/(.)(.)$/) { # last two # # This prints "d e" # print "inner: $1 $2\n"; &PrintRegExVar; } # # This also prints "d e" # print "outer: $1 $2\n"; &PrintRegExVar; } $_ = "abcde"; if (/(.)(.)/) { # first two { # ADDED EXTRA BLOCK if (/(.)(.)$/) { # last two # # This prints "d e" # print "inner: $1 $2\n"; &PrintRegExVar; } } # ADDED EXTRA BLOCK # # This prints "a b" # print "outer: $1 $2\n"; &PrintRegExVar; } __END__ inner: d e Inside called sub: d e outer: d e Inside called sub: d e inner: d e Inside called sub: d e outer: a b Inside called sub: a b
RE: RE: Re: Scope of regular expression variables
by chromatic (Archbishop) on Sep 16, 2000 at 02:15 UTC
    I'm not sure whether I disagree or not. To wit:
    $_ = "abcde"; if (/(.)(.)/) { # first two if (/(.)(.)$/) { print "inner: $1 $2\n"; } print "outer: $1 $2\n"; } print "outer: $1 $2\n";
    $1 and $2 aren't localized within the if block -- unless the regex is also within the same enclosing block.

    (At least, that makes it more explicit.)