Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Compiling Regular Expressions

by integral (Hermit)
on Feb 19, 2003 at 06:07 UTC ( #236550=note: print w/ replies, xml ) Need Help??


in reply to Compiling Regular Expressions

Zaxo has mentioned the qr operator, and diotalevi has said that regexps will be compiled and then cached by perl so that they are only compiled once. In addition to this there is the /o modifier (see perlop), which tells perl that the pattern should be compiled only once even if there is an interpolation in the pattern.

--
integral, resident of freenode's #perl


Comment on Re: Compiling Regular Expressions
Re^2: Compiling Regular Expressions
by diotalevi (Canon) on Feb 19, 2003 at 14:10 UTC

    /o is exactly like working with the qr operator except less flexible. I'm not convinced there are any real reasons to keep /o except for backwards compatibility. In any case, its not good to recommend this for new code - either use qr or normal regexes.


    Seeking Green geeks in Minnesota

      /o can still be useful from time to time. Firstly, you occasionally expect that the interpolated variable may change but still want to use the original value (but this is rare). Secondly, the "recompile only if changed" logic requires checking whether the pattern has changed, which takes time:

      perl -MBenchmark -w ($s, $t) = qw/ foo xfoox /; timethese(-1, { oful => q{ $t =~ /$s/o }, oless => q{ $t =~ /$s/ }, }) __END__ Benchmark: running oful, oless for at least 1 CPU seconds... oful: 2 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ +2427258.33/s (n=2621439) oless: 2 wallclock secs ( 1.15 usr + 0.00 sys = 1.15 CPU) @ +1841144.35/s (n=2117316)

      Hugo

        And I was expecting that the programmer who knows the expression should use the original value is better off just using qr instead of /o. The difference is in binding the regex permanently to the optree and at least having the option to alter things with qr.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://236550]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-12-26 23:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls