The easiest way to fix your code is to add parentheses around my
, and remove ??{
. Here is the fixed program:
#!/usr/bin/perl
use strict;
my $var = 1234567890;
foreach (1 .. 9){
theSub($_);
}
sub theSub {
my($int) = @_;
my($var2) = $var =~ m/($int)/;
print "$var2\n";
}
One of the problematic lines in your code was my $int = @_
, which was equivalent to my $int = 1
, because it evaluated @_
in scalar context, yielding the number of elements in @_
. To get the first argument of your sub, use my($int) = @_;
, which evaluates @_
in list context, or fetch the first element using my $int = $_[0];
, or fetch+remove the first element using my $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 the o
flag (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 with my ($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, so my ($var2) = $var =~ m/((??{$int + 5}))/
would have always matched 6
.