I'd like to run a few lines of code on each instance of a string of a particular form. To accomplish this I've been using a regex of the form:
$s =~ /(string)(?{ print $^N, $/; })(?!)/;
While this works to print all occurances of the string in question (see Global Regex sans //g), I cannot seem to get this to work in list (or perhaps array) context:
my @letters = ('a'..'z'); for my $letter (0..$#letters) { $letters[$letter] =~ /(?{ print $letter })(?!)/; }
The code seems to run each iteration; however instead of printing the current value of $letter, it returns the following error:
Use of uninitialized value in print at (re_eval 1) line 1.
As I type this I wonder if the regex and the embeded code are being compiled prior to the instantion of the variable? I tried running it through Deparse, but it appeared to be more or less unaltered. Should this be the case, I'd surmise it happens afterwards -- perhaps at runtime?
How can I print the current value of the iteration from within the regex -- preferrably sans eval?
In reply to Iterating over code embeded in regex by eibwen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |