in reply to
Re: Delimited Backtracking with Regex
in thread Delimited Backtracking with Regex
I've been asked why I used a package variable instead of a lexical variable. It's because regexps close around the lexicals that exist when they are first run.
# pass 1 2 3
# --- --- ---
sub test {
my @matches;
'' =~ /
(?{ push @matches, 'a' })
(?{ print(scalar(@matches), "\n") }) # 1 2 3
/xg;
print(scalar(@matches), "\n"); # 1 0 0
}
test() for 1..3;
A variable called @matches is created everytime test is called. The regexp always uses the variable from the first call.