Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Meaning of /o in Regexes

by cjf-II (Monk)
on Dec 06, 2002 at 12:30 UTC ( #218049=note: print w/replies, xml ) Need Help??


in reply to meaning of /o in regexes

The /o modifier is used when you only want the pattern compiled once. So in the second pattern, $x will not be incremented and as such, only matches once.

Update: Okay, I promise to read the questions in full before replying from now on (and this time I mean it! ;-). So to answer your actual question...

The difference between qr// and the /o modifier as I understand it is that with /o you can never change the pattern, whereas with qr// you can compile the pattern to be used as part of a larger regex. As the good book shows:

@regexes = (); for $pattern (@patterns) { push @regexes, qr/$pattern/; } for $item (@data) { for $re (@regexes) { if ($item =~ /$re/) { print "Matches!\n"; } } }

I Hope that's slightly more helpful :).

Replies are listed 'Best First'.
Re: Re: Meaning of /o in Regexes
by mce (Curate) on Dec 06, 2002 at 12:45 UTC
    Thanks, But this part I understood, it is the comparison with qr that I am puzzeled about.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

      Thanks, But this part I understood, it is the comparison with qr that I am puzzeled about.

      /o and qr// provide different services to the programmer:

      /o says 'Take this pattern and create a compiled regex that will not change for the life of the program'

      qr// says 'Take this pattern and return a special object that I can assign to a variable, interpolate into another pattern, pass to a subroutine, etc, etc, and as a side-effect creates a compiled form that I can use in pattern matches directly.'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://218049]
help
Chatterbox?
[Lady_Aleena]: Would someone please derail my train of thought?
[Discipulus]: marto as Marcus Manlius Capitolinus
[Discipulus]: use Dynamite; Lady_Aleena
[Lady_Aleena]: That's the problem. I have 29 scripts, 3 modules, and gads of data I am thinking about deleting out of anger over an API change that made all of those things worthless.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-05-25 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?