s/RegEx/substitutions/: Variable interpolation and when to use /e - modifiersby LanX (Sage)
|on Dec 04, 2012 at 18:58 UTC||Need Help??|
Let me try to write a deeper conceptional answer, because the handling of /e irritated me for long and I need to pin down the source of this irritation myself.
The short answer
The RHS of s/LHS/RHS/ undergoes a variable interpolation (like doublequotes) if and only if there are no /e modifiers used.
MATCH meaning the substring from VAR matching the LHS-pattern.
For completeness nota bene ...
The long answer ...
At first it's confusing that there is no difference between s/(pattern)/$1/ and s/(pattern)/$1/e
Line 1 w/o e-modifier but with interpolation:
Variables in RHS undergo an interpolation like the result in double-quotes "RHS"!
the following code is equivalent
Line 2: with e-modifier but w/o interpolation:
RHS is evaluated without variable interpolation╣ like in simple quotes eval 'RHS'
And $_= eval '$1' just means $_= $1
I hope now double evals /ee can be better understood
Like ikegami stated correctly those s/LHS/RHS/ee; and s/LHS/eval RHS/e; are equivalent
Those double /ee are used if you need a matchvariables (a substring from LHS) to be interpolated within an eval, b/c variable interpolation is missing!
Any code from LHS will be treated as uninterpolated string, as in these equivalent expamples
So if I need code-evaluation from parts of LHS I need a second /e
I think this explanation could be better worded ...
It already helped me a lot to clarify whats happening and I hope it helps you too or anyone else who tries to explain it better. =)
1) As a side note:
Variable interpolation is a powerful tool which allows some (not all) "evaluation like effects" like interpolation of hash-variables.