in reply to Re^2: passing regular expression
in thread passing regular expression
It can also be faster (since qr// compiles up front)But it still compiles onces. my $digit = '\d'; /$digit/; only compiles a regexp once as well. And so does my $digit = '\d'; /$digit/ for 1 .. 100000.
Note that my $digit = qr /\d/; /$digit/ if rand(2) < 1 always compiles a regexp once, m $digit = '\d'; /$digit/ if rand(2) < 1 compiles a regexp only 50% of the time.
Whether or not passing a pattern as a compile regexp or as string is faster depend on what the subroutine is doing with it. Compiling the pattern up front means you're going to pay the price, regardless whether you need it. Passing it as a string means you're only going to pay the price if pattern is actually needed as a regexp.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: passing regular expression
by ikegami (Patriarch) on Oct 08, 2009 at 17:27 UTC | |
|
Re^4: passing regular expression
by BioLion (Curate) on Oct 08, 2009 at 17:40 UTC |