I am trying to match some text in a loop against a regex held in a variable.
On adding use re 'debug';
I see this on 1st attempt
Compiling REx "^(dev|demo|stg[12])$"
Final program:
1: SBOL /^/ (2)
2: OPEN1 (4)
4: TRIE-EXACT[ds] (24)
<dev> (24)
<demo> (24)
<stg> (13)
13: ANYOF[12] (24)
24: CLOSE1 (26)
26: SEOL (27)
27: END (0)
floating ""$ at 3..4 (checking floating) anchored(SBOL) minlen 3
Matching REx "^(dev|demo|stg[12])$" against "stg1"
and this on the 2nd attempt. The regex value in the variable hasn't changed in this loop iteration.
Compiling REx "^(dev|demo|stg[12])$"
Matching REx "^(dev|demo|stg[12])$" against "stg1"
Does this mean it compiling it again the 2nd time or not? Or is it saying that it would have compiled the 2nd time but didn't and is reusing the already compiled one?
thanks
After posting this question, I found this one Does perl cache regex generation?
So looks like, the above 2nd attempt is a cache hit (i.e. it is not recompiling it). Is my understanding correct?
Adding the code sample as well, it is a simple match being done in a for loop
for my $dbrow (@dbrows)
{
if ($dbrow->[$DB_ENV] =~ /$policy->[$PC_ENV]/
$policy->[$PC_ENV]
is a regex
$policy->[$PC_ENV]
value is not changing in this loop.
$dbrow->[$DB_ENV]
value is changing
At first, I thought perl was re compiling $policy->[$PC_ENV]
on every iteration because I saw that Compiling
line for each iteration (not what I had expected as its value wasn't changing) but then I noticed that it only says Compiling
but then might be finding it in its cache as it doesn't say Final program:
etc
Yes, it avoided compiling a second time. As a result of your question, Perl 5.32 will have an extra line of debug output to indicate that the compilation was not actually done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With