I’m having a little trouble comprehending this simple use of the /e regex modifier.
my $var = 'testing';
$_ = 'In this string we are $var the "e" modifier.';
s/(\$\w+)/$1/ee;
print;
Returns: “In this string we are testing the “e” modifier.”
I cannot see why two ‘e’ modifiers are required. As far as I can see, $1 should capture ‘$var’ from the string and a single ‘e’ modifier should then be able to replace the variable with its value. I must be misunderstanding something however, since trying the above code with just one ‘e’ modifier does not visibly replace anything in the string.
Excuse me for asking such a simple question!
Thanks.
It’s not exactly a “simple” question, so don’t beat yourself up.
The issue is that with a single
/e, the RHS is understood to be code whoseeval’d result is used for the replacement.What is that RHS? It’s
$1. If you evaluated$1, you find that contains the string$var. It does not contain the contents of said variable, just$followed by avfollowed by anafollowed by anr.Therefore you must evaluate it twice, once to turn
$1into$var, then again to turn the previous result of$varinto the string"testing". You do that by having the doubleeemodifier on thesoperator.You can check this pretty easily by running it with one
/eversus with two of them. Here’s a demo a both, plus a third way that uses symbolic dereferencing — which, because it references the package symbol table, works on package variables only.When run, that produces: