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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The fastest and most efficient in this case is:
The difference between qr//, using /o, and using no flag is the time of binding variables in your pattern to the RE. If (as in this case) the pattern doesn't involve any variables, perl will recognize that at compile time and you should leave it to the interpreter. However if your pattern involves a variable, there are three basic possibilities.
  1. The safest assumption is that your variable may change at any time, and your pattern will need to be recompiled before each use. That is what you get by default.
  2. The fastest but most dangerous assumption is that your variable is a constant that never will change in the lifetime of your program. That is what /o is for.
  3. In between those two extremes you have cases where the programmer knows that a pattern will be used repeatedly but the variable it depends on may change. The qr// construct (which is explained in your local documentation, try perldoc perlop) is for this case. Here when you call qr// you are telling Perl exactly when to compile the pattern. I typically would do it in functions where I will be matching a pattern many times. I want the speedup, but I don't want to risk having my function work incorrectly if it is called twice.
In older Perl's (say Camel 2 vintage) there wasn't a qr// operator (introduced in 5.004 IIRC). To get a similar effect people used to use eval to produce an anonymous subroutine that would do the match and had a hard-coded pattern. (By showing a friend how to do this I sped his data-cleaning-and-loading program up by 40%.) I mention this as a historical curiousity only, today you would just use qr// for this problem.

This is my 1000'th post. I had thought about issuing some sort of general thank-you, but then I decided that the best kind of thank-you for finding both Perl and this site enjoyable is giving another explanation. If I didn't enjoy it, I wouldn't be here. :-)

In reply to Post 1000, /o vs qr// by tilly
in thread difference between qr// and the use of the o modifier ? by arhuman

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-05-28 20:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found