<?xml version="1.0" encoding="windows-1252"?>
<node id="1007141" title="s/RegEx/substitutions/: Variable interpolation and when to use /e - modifiers" created="2012-12-04 13:58:52" updated="2012-12-04 13:58:52">
<type id="11">
note</type>
<author id="708738">
LanX</author>
<data>
<field name="doctext">
Let me try to write a &lt;u&gt;deeper conceptional answer&lt;/u&gt;, because the handling of /e irritated me for long and I need to pin down the source of this irritation myself.&lt;P&gt;

&lt;h4&gt;The short answer&lt;/h4&gt;&lt;P&gt;

The RHS of &lt;c&gt;s/LHS/RHS/&lt;/c&gt; undergoes a [http://perldoc.perl.org/perlop.html#Gory-details-of-parsing-quoted-constructs|variable interpolation] (like doublequotes) if and only if
there are no &lt;c&gt;/e&lt;/c&gt; modifiers used.&lt;P&gt;

&lt;table border=1&gt;&lt;P&gt;

&lt;tr&gt;  &lt;th&gt; modifiers &lt;/th&gt;       &lt;th&gt;substition&lt;/th&gt;            &lt;th&gt; equivalent &lt;/th&gt;                          &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; no &lt;c&gt;e&lt;/c&gt; &lt;/td&gt;     &lt;td&gt; VAR =~ s/LHS/RHS/  &lt;/td&gt;  &lt;td&gt; VAR =~ m/LHS/; MATCH = "RHS" &lt;/td&gt;          &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; one &lt;c&gt;e&lt;/c&gt; &lt;/td&gt;    &lt;td&gt; VAR =~ s/LHS/RHS/e &lt;/td&gt;  &lt;td&gt; VAR =~ m/LHS/; MATCH = eval 'RHS' &lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; two &lt;c&gt;e&lt;/c&gt; &lt;/td&gt;    &lt;td&gt; VAR =~ s/LHS/RHS/ee &lt;/td&gt; &lt;td&gt; VAR =~ m/LHS/; MATCH = eval eval 'RHS' &lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; more &lt;c&gt;e&lt;/c&gt; &lt;/td&gt;   &lt;td colspan=2&gt; Don't! Only for brainfuck and obfuscation, better call function &lt;/td&gt; &lt;/tr&gt;&lt;P&gt;

&lt;/table&gt;&lt;P&gt;

&lt;small&gt;MATCH meaning the substring from VAR matching the LHS-pattern.&lt;/small&gt;&lt;P&gt;

For completeness nota bene ...&lt;P&gt;

&lt;table border=1&gt;
&lt;tr&gt;  &lt;th&gt; concept &lt;/th&gt;       &lt;th&gt;code&lt;/th&gt;            &lt;th&gt; equivalent &lt;/th&gt;                          &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; disabling interpolation &lt;/td&gt; &lt;td&gt; VAR =~ s'LHS'RHS'  &lt;/td&gt;  &lt;td&gt; VAR =~ m/LHS/; MATCH = 'RHS' &lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; string interpolation    &lt;/td&gt; &lt;td&gt; $a = "$b - $c"  &lt;/td&gt;  &lt;td&gt;  $a = $b . ' - ' . $c &lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt;  &lt;td&gt; variable evaluation     &lt;/td&gt; &lt;td&gt; $a = $b  &lt;/td&gt;  &lt;td&gt;  $a = eval '$b' &lt;/td&gt; &lt;/tr&gt;
&lt;/table&gt;&lt;P&gt;


 
&lt;h4&gt; The long answer ...&lt;/h4&gt;&lt;P&gt;

&lt;h5&gt;Problem &lt;/h5&gt;&lt;P&gt;

At first it's confusing  that there is no difference between 
&lt;c&gt;s/(pattern)/$1/&lt;/c&gt;
 and 
&lt;c&gt;s/(pattern)/$1/e&lt;/c&gt;&lt;P&gt;

example:
&lt;c&gt;
  DB&lt;1&gt; $_='abc'; s/a(b)c/$1/; print $_
b
  DB&lt;2&gt; $_='abc'; s/a(b)c/$1/e; print $_
b
&lt;/c&gt;&lt;P&gt;

&lt;h5&gt;Explanation&lt;/h5&gt;&lt;P&gt;

&lt;u&gt;Line 1 w/o e-modifier but with interpolation:&lt;/u&gt; &lt;P&gt;

Variables in RHS undergo an interpolation like the result in double-quotes &lt;c&gt;"RHS"&lt;/c&gt;!&lt;P&gt;

the following code is equivalent &lt;P&gt;

&lt;c&gt;
  DB&lt;1&gt; $_='abc'; s/a(b)c/$1/; print $_
b
  DB&lt;3&gt; $_='abc'; m/a(b)c/; $_ = "$1" ; print $_
b
&lt;/c&gt;&lt;P&gt;


&lt;u&gt;Line 2: with e-modifier but w/o interpolation:&lt;/u&gt;&lt;P&gt;

RHS is evaluated without variable interpolation¹ like in simple quotes &lt;c&gt;eval 'RHS'&lt;/c&gt;&lt;P&gt;

&lt;c&gt;
  DB&lt;2&gt; $_='abc'; s/a(b)c/$1/e; print $_
b
  DB&lt;4&gt; $_='abc'; m/a(b)c/; $_= eval '$1'; print $_
b
&lt;/c&gt;&lt;P&gt;

And  &lt;c&gt;$_= eval '$1'&lt;/c&gt; just means &lt;c&gt;$_= $1&lt;/c&gt;&lt;P&gt;

&lt;h5&gt;I hope now double evals &lt;c&gt;/ee&lt;/c&gt; can be better understood&lt;/h5&gt;&lt;P&gt;

Like [ikegami] stated correctly those &lt;c&gt; s/LHS/RHS/ee;&lt;/c&gt; and &lt;c&gt; s/LHS/eval RHS/e;&lt;/c&gt; are equivalent &lt;P&gt;

Those double &lt;c&gt;/ee&lt;/c&gt; are used if you need a matchvariables (a substring from LHS) to be interpolated within an eval, b/c variable interpolation is missing!&lt;P&gt;

Any code from LHS will be treated as uninterpolated string, as in these equivalent expamples&lt;P&gt;

&lt;c&gt;
  DB&lt;5&gt; $_='warn 666'; s/(.*)/$1/e; print $_
warn 666
  DB&lt;6&gt; $_='warn 666'; m/(.*)/; $_= eval '$1'; print $_
warn 666
&lt;/c&gt;&lt;P&gt;

So if I need code-evaluation from parts of LHS I need a second &lt;c&gt;/e&lt;/c&gt;&lt;P&gt;

&lt;c&gt; 
  DB&lt;7&gt; $_='warn 666'; s/(.*)/$1/ee; print $_
666 at (eval 171) ....    # skipped error message
1      # $_ = return code from warn
 
  DB&lt;8&gt; $_='warn 666'; m/(.*)/; $_= eval eval '$1';  print $_
666 at (eval 175). ... # skipped error message
1      # $_ = return code from warn
&lt;/c&gt;&lt;P&gt;


&lt;h4&gt;Resumee&lt;/h4&gt;&lt;P&gt;



I think this explanation could be better worded ... &lt;P&gt;

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. =)&lt;P&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-708738"&gt;
&lt;p&gt;Cheers Rolf
&lt;/div&gt;&lt;/div&gt;&lt;P&gt;


1) As a side note:&lt;P&gt;

Variable interpolation is a powerful tool which allows &lt;u&gt;some (not all)&lt;/u&gt; "evaluation like effects" like interpolation of hash-variables. &lt;P&gt;

&lt;c&gt;
  DB&lt;134&gt; $hash{b}=666;
 
  DB&lt;135&gt; $_='abc'; s/a(b)c/$hash{$1}/; print $_
666
  DB&lt;136&gt; $_='abc'; m/a(b)c/; print "$hash{$1}";
666
&lt;/c&gt;&lt;P&gt;

</field>
<field name="root_node">
1006930</field>
<field name="parent_node">
1006930</field>
</data>
</node>
