in reply to RE: Re: Scope of regular expression variables
in thread Scope of regular expression variables
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
|
|---|