in reply to Re: Optimize a pluggable regex matching mechanism (/o practically deprecated)
in thread Optimize a pluggable regex matching mechanism

Perl's checking a regex-cache to see if it has already compiled the pattern.

No, the cache is not nearly so general. Each op simply remembers the last pattern it compiled.

>perl -Mre=debug -e"/$_/ for qw( a a b a )" 2>&1 | find "Compiling" Compiling REx "a" Compiling REx "b" Compiling REx "a"
>perl -Mre=debug -e"/a/; /a/;" 2>&1 | find "Compiling" Compiling REx "a" Compiling REx "a"

Nothing's stopping you from making your own such cache, though.

my $compiled_pat = $compiled_pats{$pat} ||= qr/$pat/;

Replies are listed 'Best First'.
Re^3: Optimize a pluggable regex matching mechanism (/o practically deprecated)
by LanX (Saint) on Nov 10, 2009 at 16:52 UTC
    For completeness, the following code stresses out that the last compilation per code position is reused if possible.

    perl -Mre=debug -e '@x=qw/a b a a c a/; while (($a,$b,@x)=@x) { /$a/;/ +$b/ }' 2>&1|grep Compiling Compiling REx "a" Compiling REx "b" Compiling REx "a" Compiling REx "c"

    Each op remembers the last pattern it compiled.

    Yeah, that's kind of what I expected (was too lazy for details), but a general cache would be indeed real overkill.

    Both ways there is not much use left for /o, IMHO the perlre and perlretut should be updated with at least footnote...

    Cheers Rolf