in reply to Re: regex "o" modifier
in thread regex "o" modifier

Just a quick question on something here.

If I had the following:

my $regex = qr!yes|no!; my $response = "yes"; print "Not maybe" if $response =~ /$regex/o

Is that a case where the /o would come in to play as that isn't going to change or does the qr compile the regex before the if statement?

Thanks!

There is no emoticon for what I'm feeling now.

Replies are listed 'Best First'.
Re: Re: Re: regex "o" modifier
by edoc (Chaplain) on May 07, 2003 at 01:14 UTC

    nope, you wouldn't benefit from an /o in this case..

    qr quotes and compiles the pattern as a regular expression. To see what it's doing, try this:

    perl -e 'my $re = qr!yes|no!; print "$re\n";' prints: (?-xism:yes|no)

    The result of qr, in this case $re, can be used on it's own, or within another regex.

    my $re = qr!yes|no!; print "yes or no!\n" if "yes" =~ $re; print "yes or no only" if "maybe" !~ $re; print "polite yes or no\n" if "yes thankyou" =~ /$re thankyou/;

    The first 2 uses in this code will not trigger the recompling of any regex. The 3rd will need to be compiled, although I believe you could do...

    my $re = qr!yes|no!; my $re2 = qr!\sthankyou!; print "yes or no!\n" if "yes" =~ $re; print "yes or no only" if "maybe" !~ $re; print "polite yes or no\n" if "yes thankyou" =~ /$re$re2/;

    ..and avoid any recompilation after the initial qr's

    cheers,

    J

      You'll want to revisit Re^2: meaning of /o in regexes. If you interpolate qr// compiled expressions you lose because the thing is compiled more often than it needs to be. Your "I believe you could do..." is incorrect.

        hmm.. yup, gotcha there.. shouldn't have added that last bit.

        ok.. knew I saw something somewhere.. in "Programming Perl" it has the example:

        $regex = qr/$pattern/; $string =~ /foo${regex}bar/; # interpolate into larger patterns

        ..and then the comment, "This time, Perl does recompile the pattern, but you could always chain several qr// operators together into one."

        So would they maybe be suggesting something more like:

        my $re = qr!yes|no!; my $re2 = qr!\sthankyou!; my $bigre = $re.$re2; print "polite yes or no\n" if "yes thankyou" =~ $bigre;

        hmm.. on further thought, that doesn't seem right either. Would $re & $re2 each be stringified in that operation? If so that would mean that $bigre would simply be eq to the string '(?-xism:yes|no)(?-xism:\sthankyou)'

        The only other thing I can think they mean is:

        my $bigre = qr!yes|no!.qr!\sthankyou!;

        But I really don't see the point in that... Anyone know what they mean by "chain several qr// operators together"?

        cheers,

        J

Re: Re: Re: regex "o" modifier
by Anonymous Monk on May 06, 2003 at 23:29 UTC
    You cannot apply modifiers to a regex object after it has been defined. Rewrite this as qr!yes|no!o. And the answer to your question is that yes the o modifier comes into play with qr// and locks in the regex the first time it is used.
      Thanks! Now it makes sense.

      But it does make me question one other point. If I don't use the /o in the qr!yes|no!o, if I loop over a set of values using the qr'd regex, does it recompile it every time?

      There is no emoticon for what I'm feeling now.

        Nope, it won't recompile every time. That's the benefit of using qr.

        J