I would like to create subroutine with a dynamically created regxp. Here is what I have so far:
#!/usr/bin/perl
use strict;
my $var = 1234567890;
foreach (1 .. 9){
&theSub($_);
}
sub theSub {
my $int = @_;
my $var2 = $var =~ m/(??{$int})/;
print "$var2\n";
}
It looks like it will work, but it seems that once the $int in the regex gets evaluated for the first time, it’s there forever.
Is there anyway to do something similar to this, but have the regex pick up the new argument each time the sub is called?
The easiest way to fix your code is to add parentheses around
my, and remove??{. Here is the fixed program:One of the problematic lines in your code was
my $int = @_, which was equivalent tomy $int = 1, because it evaluated@_in scalar context, yielding the number of elements in@_. To get the first argument of your sub, usemy($int) = @_;, which evaluates@_in list context, or fetch the first element usingmy $int = $_[0];, or fetch+remove the first element usingmy $int = shift;There was a similar problem in the
my $var2 =line, you need the parentheses there as well to evaluate the regexp match in list context, yielding the list of($1, $2, ...), and assigning$var2 = $1.The construct
(??{...})you were trying to use had the opposite effect to what you wanted: (among doing other things) it compiled your regexp the first time it was used for matching. For regexps containing$or@, but not containing??{...}, Perl recompiles the regexp automatically for each match, unless you specify theoflag (e.g.m/$int/o).The construct
(??{...})means: use Perl code...to generate a regexp, and insert that regexp here. To get more information, search for??{on http://perldoc.perl.org/perlre.html . The reason why it didn’t work in your example is that you would have needed an extra layer of parentheses to capture$1, but even withmy ($var2) = $var =~ m/((??{$int}))/it wouldn’t have worked, because??{has an undocumented property: it forces the compilation of its argument the first time the regexp is used for matching, somy ($var2) = $var =~ m/((??{$int + 5}))/would have always matched6.