Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Perl, how can I build a dynamic regexp by passing in an argument to a subroutine?

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?

like image 521
Nick Messick Avatar asked May 01 '09 05:05

Nick Messick


2 Answers

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.

like image 64
pts Avatar answered Oct 20 '22 16:10

pts


my $int = @_;

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

I think you want

my $int = shift;
like image 27
Thilo Avatar answered Oct 20 '22 17:10

Thilo