<?xml version="1.0" encoding="windows-1252"?>
<node id="1009094" title="Re: 'Dynamic scoping' of capture variables ($1, $2, etc.) (localization of captures in recursions is buggy!)" created="2012-12-16 16:03:21" updated="2012-12-16 16:03:21">
<type id="11">
note</type>
<author id="708738">
LanX</author>
<data>
<field name="doctext">
I understand your problem, for my understanding "dynamically scoped" should mean &lt;c&gt;local&lt;/c&gt;ized such that the sum at the end shouldn't be 4=1+1+1+1 but 1+7777+666+55.&lt;P&gt;

but if you read the docs differently&lt;P&gt;

&lt;tt&gt; &lt;pre&gt;
Capture group contents are dynamically scoped and available to you
outside the pattern until the end of the enclosing block or until the
next successful match, &lt;b&gt;whichever comes first.&lt;/b&gt;
&lt;/pre&gt;&lt;/tt&gt;&lt;P&gt;

you can see that the next match within the recursion sets $1 to another value.&lt;P&gt;

At least that's ATM my understanding of the designers intention.&lt;P&gt;

&lt;i&gt; &gt; a branch to a point within the same block&lt;/i&gt;&lt;P&gt;

no, no tail-head optimization in Perl!&lt;P&gt;

I will try to run some experiments tomorrow.&lt;P&gt;

&lt;b&gt;UPDATE1:&lt;/b&gt;&lt;P&gt;

hmm, w/o recursion and with different strings it works like localized:&lt;P&gt;

&lt;c&gt;
  DB&lt;112&gt; sub tst2 { 'a' =~ /(\w)/; $1. tst(). $1 }
 =&gt; 0
 
  DB&lt;113&gt; sub tst { 'b' =~ /(\w)/; $1}
 =&gt; 0
 
  DB&lt;114&gt; 'c' =~ /(\w)/; print $1. tst2(). $1
 =&gt; 1
cabac
&lt;/c&gt;&lt;P&gt;

&lt;b&gt;UPDATE2&lt;/b&gt;&lt;P&gt;

you're right &lt;b&gt;&lt;u&gt;there is a bug&lt;/u&gt; in how recursive functions are handled:&lt;/b&gt;&lt;P&gt;

&lt;c&gt;  DB&lt;123&gt; sub delchar { $x =~ s/(\w)// ? $1 . delchar() . $1 : "x"  } 
 =&gt; 0
 
  DB&lt;124&gt; $x='abc'
 =&gt; "abc"
 
  DB&lt;125&gt; delchar()
 =&gt; "cccxccc"
&lt;/c&gt;&lt;P&gt;

in contrast explicitly different functions:
&lt;c&gt;
  DB&lt;138&gt; sub del1 { $x =~ s/(\w)// ? $1 . del2() . $1 : "x"  } 
 =&gt; 0
 
  DB&lt;139&gt; sub del2 { $x =~ s/(\w)// ? $1 . del3() . $1 : "x"  } 
 =&gt; 0
 
  DB&lt;140&gt; sub del3 { $x =~ s/(\w)// ? $1 . del4() . $1 : "x"  } 
 =&gt; 0
 
  DB&lt;141&gt; sub del4 { $x =~ s/(\w)// ? $1 . del5() . $1 : "x"  } 
 =&gt; 0
 
  DB&lt;142&gt; $x='abc'; del1()
 =&gt; "abcxcba"
&lt;/c&gt;&lt;P&gt;

Perl 5.10&lt;P&gt;

&lt;b&gt;UPDATE3&lt;/b&gt;&lt;P&gt;

the following shows that $1 is static per function, maybe they where implemented like closures.&lt;P&gt;

&lt;c&gt;
  DB&lt;111&gt; sub del2 { $x =~ s/(\w)// ? $1 . del1() . $1 : "-"  } 
 =&gt; 0
 
  DB&lt;112&gt; sub del1 { $x =~ s/(\w)// ? $1 . del2() . $1 : "-"  } 
 =&gt; 0
 
  DB&lt;113&gt; $x='abcd'; del1()
 =&gt; "cdcd-dcdc"
&lt;/c&gt;&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;</field>
<field name="root_node">
1009091</field>
<field name="parent_node">
1009091</field>
</data>
</node>
