Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Configurable Matches

by Stevie-O (Friar)
on Jun 03, 2004 at 04:17 UTC ( #359916=note: print w/replies, xml ) Need Help??

in reply to Configurable Matches

It's a lot easier than you might think... m// interpolates.
#!/usr/bin/perl -l chomp($tmp = <DATA>); $foo = 'the rain in spain'; print "match" if $foo =~ /$tmp/; __END__ __DATA__ ain$
Change the line after __DATA__ to change the regex matched against.
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

Replies are listed 'Best First'.
Re^2: Configurable Matches
by jdhawke (Acolyte) on Jun 03, 2004 at 04:41 UTC
    Hmmm, this makes it even easier, any pros/cons of doing it this way vs the qr// method that Mr.Muskrat demonstrated above?
      Well, you hit the pro on the head... it's much easier to interpolate variables into m//.

      There is a major con, though. If you plan on using this regular expression in a loop, it will need to be recompiled every time it is used. With qr//, you will only compile once... You may want to benchmark them to find out the difference:

      #!/usr/bin/perl -w use Benchmark; my $txt = 'find this regex'; my $str = 'regex?'; my $rgx = qr/$str/i; timethese ( 1000000, { match => sub{ $txt =~ m/$str/i }, qreg => sub{ $txt =~ $rgx }, }, );

      Gives me the following:

      % Benchmark: timing 1000000 iterations of match, qreg... match: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12 +19512.20/s (n=1000000) qreg: 0 wallclock secs ( 0.73 usr + 0.00 sys = 0.73 CPU) @ 13 +69863.01/s (n=1000000)

      As you can see, for a regex this simple, it doesn't matter too much... but more complicated regexs wll show a bigger difference.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://359916]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-06-18 06:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.