Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

First, I'll apologize. I haven't read every node in this thread in detail. But I think there are a few key points about /o that should be brought up that I didn't notice being mentioned.

1) Don't use /o anymore. If you aren't into the subtle points, then just remember that. /o used to provide a rather significant speed improvement in certain rare situtations. Several things have changed related to this. One is that now it provides only a minor speed improvement.

What hasn't changed is that /o can easily lead to bugs in your code. It means that $y =~ /$x/ no longer matches $y against $x but instead matches $y against whatever was in $x the first time the code got executed. This can be quite confusing and it is easy to get this wrong.

2) If you really want the minor speed improvement that /o could still give you, then you should use qr// instead. That is, replace:

sub foo { $y =~ /$x/o; }
with
BEGIN { my $re; sub foo { $re= qr/$x/ unless $re; $y =~ $re; } }
or, more likely, where you now set $x to be the one value you want to use, instead set $re= qr/$x/ in that place (sorry, the second example looks a bit complicated, but that is mostly do to with the contrived nature of the example -- in reality, using qr// in place of //o likely makes your code easier to understand).

Using qr// is even better because you get the speed improvement of being able to use $re 10,000 times without recompiling and yet you can update your program such that you do $re= qr/$x/ a second time and then use $re 10,000 more times with only the one extra compilation. qr// is every bit as fast as //o, but it is much more flexible and much less error prone.

3) qr//o is identical to qr//. In fact, japhy (I believe) ran off one day to patch Perl such that qr//o would no longer be supported (since supporting it just leads to confusion as you have now seen). I'm curious to see what happened to that patch, but unfortunately now is not the time to me to go digging for that.

More rambling details...

It used to be that saying /$x/ would cause the regex to be recompiled every single time it was executed. Now, /$x/ is only recompiled if $x has changed since the last time it was recompiled.

qr// gets compiled every time the qr// part is executed, even if you say qr//o. If you really only want it to compile once, then you should only execute the qr// once. (qr//o really should be an error.) BTW, I tested this assertion in Perl 5.006.

        - tye

In reply to (tye)Re: meaning of /o in regexes by tye
in thread meaning of /o in regexes by mce

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (9)
    As of 2014-12-21 12:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (105 votes), past polls