in reply to precompiling regular expressions

Replies are listed 'Best First'.
Re^2: precompiling regular expressions
by Anonymous Monk on Oct 22, 2010 at 10:09 UTC

    Compiles once (since Perl notices the pattern doesn't change):

    sub f { my $pat = $_[0]; (my $str = 'abba') =~ s/$pat//g; } f('b') f('b') f('b')

    are you sure about that? basicly I do that, but the profiler claims that CORE:regcomp is called as often as f is executed...

    I am using a rather ancient edition of perl (5.8.6), has this behavivour changed since then?

      are you sure about that?

      Yes.

      >perl -Mre=debug -e"sub f { my $pat = $_[0]; (my $str = 'abba') =~ s/$ +pat//g; } f('b'); f('b'); f('b');" 2>&1 | find /c "Compiling" 1

      Before compiling, it checks if the pattern is the same as the one from the last time the operator was evaluated. If so, it reuses the compiled pattern from the last evaluation.

      I am using a rather ancient edition of perl (5.8.6), has this behavivour changed since then?

      It's not new, but I don't know how old.