in reply to Re-use of a global match

Do gube's original question and the fine answers above not point also to some optomization (a single instance in the symbol table of the two identical if...} clauses? Note that adding the third time thru, but using a variant if... (no 'g') produces a match there but then repeating the variant if...} finds no match in 4.

#!/usr/local/bin/perl use strict; use warnings; my $regexp = 'Perl Monks'; my $regexp2 = 'Perl Monks is a wonderful place to visit'; # even if No +de Reaper is sometimes a bit loud if($regexp =~ m/^Perl Monks/gi) { print "\nFound.0."; } else { print "\nNot Found.0."; } if($regexp =~ m/^Perl Monks/gi) { print "\nFound.1."; } else { print "\nNot Found.1."; } if($regexp =~ m/^Perl Monks/i) { # No 'g' print "\nFound.2."; } else { print "\nNot Found.2."; } if($regexp2 =~ m/^Perl Monks/gi) { # 'g' is back, but different $var +being tested print "\nFound.3."; } else { print "\nNot Found.3."; } if($regexp2 =~ m/^Perl Monks/gi) { # second identical use of $regex2 +and match print "\nFound.4."; } else { print "\nNot Found.4."; }
produces:
perl gube_g.pl Found.0. Not Found.1. Found.2. Found.3. Not Found.4.

Replies are listed 'Best First'.
Re^2: Re-use of a global match
by shmem (Chancellor) on Aug 01, 2007 at 10:48 UTC
    Do gube's original question and the fine answers above not point also to some optomization (a single instance in the symbol table of the two identical if...} clauses?

    No*. It all has to do with match positions not being reset until a match failed, if /g is in effect.

    Without /g, those internal state variables are reset before each match; with /g, they are reset after a match failed.

    *) which doesn't mean there isn't such optimization, merely that the behaviour gives no evidence about that.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}