On Jan 8, 12:21 pm, frank.w.w...@gmail.com (Soldier) wrote: > Hi, > I came across these two pieces of codes, why would the "local $i=$i+1" > be backtracking-safe? > > $_ = 'lothlorien';
> m/ (?{ $i = 0 }) # Set $i to 0 > (. (?{ $i++ }) )* # Update $i, even after > backtracking > lori # Forces a backtrack > /x; > > $_ = 'lothlorien'; > m/ (?{ $i = 0 }) > (. (?{ local $i = $i + 1; }) )* # Update $i, backtracking-safe. > lori > (?{ $result = $i }) # Copy to non-localized location. > /x; I don't know how the internals but, adding a bit of debug exposes what happens IIUC: m/ ... (. (?{ local $i = $i + 1; warn "\$i=$i\n";}) )* ... /x; warn "\$i=$i \$result=$result\n" m/ ... (. (?{ $i++; warn "\$i=$i\n";}) )* ... /x; warn "\$i=$i \$result=$result\n" The output below suggests both regexes find the initial match at pos. 4 within the string; pos. 4 is saved; but * greedily continues to bump along failing with each bump until the end of the string is reached. Then with 'local' the first and only match at pos. 4 gets restored. Without the 'local' though, $i runs up to the end of the string at pos. 10. $i=1 $i=2 ... $i=10 $i=0 $result=4 $i=1 $i=2 ... $i=10 $i=10 $result=10 -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/