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

Re: [MacPerl] Importing search patterns



Alan Fry <ajf@afco.demon.co.uk> writes:
>It would be a nice Macish use of MacPerl to be able import a search pattern
>via MacPerl::Ask(), rather than type it directly into the script. However
>there are difficulties importing the operators <i, g, o, e>. For instance:
>
>        $str = MacPerl::Ask("What?", "/pattern/i");
>
>returns '$str' as '/pattern/i' but a line such as: <if ($_ =~ $str) {}> is
>clearly nonsense.
>
>It is possible to take the pattern apart by:
>
>        $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?
>
>Apart from abandon the project has anybody any suggestions?

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

Perl 5, though, has new regexp features: Just do:

$_ =~ /(?$2)$1/;

Matthias

-----
Matthias Neeracher <neeri@iis.ee.ethz.ch> http://err.ethz.ch/members/neeri.html
  "And that's why I am going to turn this world upside down, and make
   of it a fire so *bright* that someone real will notice"
                                -- Vernor Vinge, _Tatja Grimm's World_