I don't understand why constant folding doesn't work as expected.
I'm not an expert on the Perl internals, for now I'm just drawing my conclusions from the documentation and the debug output. All I can offer at the moment is the output of B::Concise, I have trimmed it down to only the relevant differences between the two:
$ perl -MO=Concise -we 'sub parse{${$_[0]}=~/\Gfoo/gc or die} parse \(
+""."foo") for 1..2'
...
a <1> srefgen sKM/1 ->b
- <1> ex-list lKRM ->a
9 <$> const(PV "foo") sPRM/FOLD ->a
...
$ perl -MO=Concise -we 'sub parse{${$_[0]}=~/\Gfoo/gc or die} parse \(
+"foo") for 1..2'
...
9 <$> const(IV \"foo") sM/FOLD ->a
...
The way I interpret that is that in the first case, the constant ""."foo" is apparently folded down to "foo", however what apparently isn't folded is taking a reference to that string. "Constant Folding" in perlop only talks about the constant folding of strings and numeric values, so I'm sorry I don't have a very solid explanation of why the two examples are different (other than my above interpretation of the concat op, which no longer seems to fully accurately explain the situation).
In general, I would try to find a solution that doesn't depend on internals like whether certain optimizations were performed or certain internal flags like Readonly are set.
Can I count on this 'fix' (e.g. prepending an empty string) to parse a hard-coded string several times
If the problem is that you want to reset m/\G.../gc matches, then what you can do reliably is pos($string)=undef; to reset that particular state, I posted an example here.
|