views:

459

answers:

4

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?

+15  A: 

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.

pts
You got my +1 already, but if you have a link for ??{...} .
Thilo
Updated my answer with what I know about ??{...}
pts
+3  A: 
my $int = @_;

This will give you the count of parameters, always '1' in your case.

I think you want

my $int = shift;
Thilo
A: 

my $int is the scalar context, he has ($int) for the list context and that puts $_[0] into $int. In the following only 10 is put into $int and the rest 11 to 99 are lost.

my ($int)=(10..99); print $int; 10

+2  A: 

To dynamically pass a regexp to a function, rather than dynamically build it in the function, use qr//.

#!/usr/bin/perl

use strict;

my $var = 1234567890;


foreach (1 .. 9){
    &theSub(qr/$int/);
}



sub theSub {
    my($regexp) = @_;
    my($var2) = ($var =~ $regexp);
    print "$var2\n";
}

qr// accepts the same trailing arguments that m// does: i, m, s, and x

Craig Lewis