[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Importing search patterns



>>         $str =~ /\/(.*)\/(.*)/;      #followed by:
>>         if ($_ =~ /$1/$2) {}
>>
>> but MacPerl baulks at $2 since it expects an operator in that position.
>> That problem can be avoided by;
>>
>>         if (eval('$_ =~ /$1/'.$2)) {print "Hooray\n"};
>>
>> which seems to work, but the use of 'eval' for each line of the file slows
>> up execution and also mops up a relatively large amount of memory?

Matthias put forward these thoughts:

>The traditional solution is to wrap the *whole* file loop in an eval, which
>works fine but can be a security risk sometimes.

Something like this...(?)
        eval ('while(<IN>) { if ($_ =~ /'.$1.'/'.$2.') {print $_}}');
...seems to work properly.

>Perl 5, though, has new regexp features: Just do:
>
>$_ =~ /(?$2)$1/;

Really elegant and works beautifully. I saw it in the manual but missed the
significance...

Andrew Langmead suggested (with interesting variations):

>Besides the suggestion that Matthias gave, another option is to use
>eval once to create a new subroutine, and call it as much as needed.

>eval "sub match { local(\$_) = \@_ if defined \@_;/$1/$2}";

>Will create a subroutine that will match what is contained in $1 at
>the point the eval is executed, modified by the modifiers contained in
>$2 (again at the point of the eval.)

Also a neat idea but I found trouble getting it to work. The following
version (after a good deal of experimentation -- I think it boils down to
the same idea) seems to run satisfactorily;

        eval('sub match { shift =~ /'.$1.'/'.$2.'}');
        while(<IN>) {if ( &match($_)) {print $_}}

Thank you both very much. I shall do some speed trials and publish the
results here if anything interesting turns up.

Alan