Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

A regular expression (or regex)

by anakin30 (Acolyte)
on Aug 16, 2010 at 14:13 UTC ( #855283=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

dear monks, i have a A regular expression(or regex) in my perl script. this is the code snippet, if ($this_in =~ /^dn: /gio) i know what does the "g" and "i" means, as below. g - DO globally pattern matching. i - Do case-insensitive pattern matching. can somebody please explain to me whats the "o" means. i would like to thank you guys in advanced for helping.. anakin30.....

Comment on A regular expression (or regex)
Download Code
Re: A regular expression (or regex)
by suhailck (Friar) on Aug 16, 2010 at 14:21 UTC
    from, perlretut
    If $pattern won't be changing over the lifetime of the script, we can +add the "//o" modifier, which directs Perl to only perform variable substitutions once: #!/usr/bin/perl # Improved simple_grep $regexp = shift; while (<>) { print if /$regexp/o; # a good deal faster }
Re: A regular expression (or regex)
by SuicideJunkie (Priest) on Aug 16, 2010 at 14:22 UTC
Re: A regular expression (or regex)
by kennethk (Monsignor) on Aug 16, 2010 at 14:22 UTC
    This is answered in Optimizing pattern evaluation in perlretut:

    If $pattern won't be changing over the lifetime of the script, we can add the //o modifier, which directs Perl to only perform variable substitutions once

    It wouldn't make sense to use this optimization in the context you've shown - even if it were embedded in a loop, there is no variable being interpolated.

    A point on formatting posts - please do not use <pre> tags. Rather, write normal text, separate paragraphs with <p> tags and wrap your code in <code> tags. See How do I post a question effectively?.

Re: A regular expression (or regex)
by marto (Chancellor) on Aug 16, 2010 at 14:23 UTC
Re: A regular expression (or regex)
by JavaFan (Canon) on Aug 16, 2010 at 14:57 UTC
    To me, it says the author of the regexp is considering Perl programming to be magic he doesn't fully understand.

    Neither the /g nor the /o are actually useful here. And I wouldn't be surprised if the /i isn't either.

Re: A regular expression (or regex)
by ikegami (Pope) on Aug 16, 2010 at 15:29 UTC

    i know what does the "g" and "i" means, as below.

    Apparently not. Not only does if (/.../g) make no sense, it's practically always a bug. (if (/.../gc) might be seen in some advanced code.)

Re: A regular expression (or regex)
by ikegami (Pope) on Aug 16, 2010 at 15:35 UTC

    can somebody please explain to me whats the "o" means.

    In interpolating patterns, /o will only interpolate once, the first time the regex is evaluated.

    for my $x (qw( foo bar )) { /$x/o; }
    is the same as
    /foo/; /foo/;

    It's used for optimising, but it's made rather useless by qr//.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://855283]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-09-22 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (188 votes), past polls