in reply to callback for regex engine

Perl regexes allow to embed perlcode which is called during backtracking process.

Something like /pattern(?{ code } )/g (see also (??{...})

But in your simple case an iterator with while (/pattern/g) {code} is certainly better style (readability, maintainability, not an experimental feature!¹)

Cheers Rolf

( addicted to the Perl Programming Language)

PS: plz use code-tags

update
DB<123> $str = join "","a".."j" => "abcdefghij" DB<124> $x=0;$str =~ /(\w)(?{print "match ",++$x," $1 \n"})/g match 1 a match 2 b match 3 c match 4 d match 5 e match 6 f match 7 g match 8 h match 9 i match 10 j => ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j") DB<125> $x=0; while ( $str =~ /(\w)/g ) {print "match ",++$x," $1 \n +"} match 1 a match 2 b match 3 c match 4 d match 5 e match 6 f match 7 g match 8 h match 9 i match 10 j => ""

¹) perlre 5.10-5.16

"(?{ code })" WARNING: This extended regular expression feature is considered experimental, and may be changed without n +otice. Code executed that has side effects may not perform identically from version to version due to the effect + of future optimisations in the regex engine.