in reply to Re: precompiling regular expressions
in thread precompiling regular expressions

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?

Replies are listed 'Best First'.
Re^3: precompiling regular expressions
by ikegami (Patriarch) on Oct 22, 2010 at 10:34 UTC

    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.