http://www.perlmonks.org?node_id=855283

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.....

Replies are listed 'Best First'.
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 marto (Cardinal) 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 kennethk (Abbot) 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 ikegami (Patriarch) 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 (Patriarch) 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//.

Re: A regular expression (or regex)
by SuicideJunkie (Vicar) on Aug 16, 2010 at 14:22 UTC