use strict; use warnings; # 1 2 # 012345678901234567890123456789 my $string = q(The cat scattered caterpillars\n); # ^ ^ ^ # 4 9 18 print "\n Match against string - $string\n"; print "Start matching\n\n"; my $match = 1; while($string =~ /(cat)/g) { print "Match @{[$match ++]}\n"; print " found - $1\n"; print " Value of \$-[1] - $-[1]\n"; print " Value of \$+[1] - $+[1]\n"; print " Value of \$+[0] - $+[0]\n"; print " Value of \$#+ - $#+\n"; print "Value of pos(\$string) - @{[pos($string)]}\n"; } print "\nStart annotation\n\n"; $string =~ s { (cat)(?{print "Value of pos(\$string) - @{[pos($string)]}\n"}) } { $1 . "[@{[pos($string)]}]" }xeg; print "\n Annotated string - $string\n"; #### Match against string - The cat scattered caterpillars Start matching Match 1 found - cat Value of $-[1] - 4 Value of $+[1] - 7 Value of $+[0] - 7 Value of $#+ - 1 Value of pos($string) - 7 Match 2 found - cat Value of $-[1] - 9 Value of $+[1] - 12 Value of $+[0] - 12 Value of $#+ - 1 Value of pos($string) - 12 Match 3 found - cat Value of $-[1] - 18 Value of $+[1] - 21 Value of $+[0] - 21 Value of $#+ - 1 Value of pos($string) - 21 Start annotation Value of pos($string) - 7 Value of pos($string) - 12 Value of pos($string) - 21 Annotated string - The cat[4] scat[9]tered cat[18]erpillars