<?xml version="1.0" encoding="windows-1252"?>
<node id="243024" title="Re: Re: Lexical scoping like a fox" created="2003-03-14 08:43:40" updated="2005-07-19 22:21:23">
<type id="11">
note</type>
<author id="209409">
hv</author>
<data>
<field name="doctext">
&lt;p&gt;Maybe it would be easier to express the distinction as &lt;i&gt;spatial&lt;/i&gt; versus &lt;i&gt;temporal&lt;/i&gt; scoping, which is an approach that I think makes sense at least until you start throwing threads and processes into the mix.&lt;/p&gt;

&lt;p&gt;Let's take this example:&lt;code&gt;
print "Just before start of scope\n";
{
  print "Just after start of scope\n";
  my $a = 1;
  local $b = 1;
  print "a=$a, b=$b\n";
  foo();
  print "Just before end of scope\n";
}
print "Just after end of scope\n";
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, in this example, the line '&lt;code&gt;my $a = 1;&lt;/code&gt;' introduces a lexical variable. Any reference to &lt;code&gt;$a&lt;/code&gt; in the statements following that introduction until the end of the scope refer to this newly introduced variable - and only those lines. This is a spatial scope: if the &lt;code&gt;foo&lt;/code&gt; subroutine refers to a &lt;code&gt;$a&lt;/code&gt; variable, it won't see this &lt;code&gt;$a&lt;/code&gt;, because &lt;code&gt;foo&lt;/code&gt; is not defined within this scope.&lt;/p&gt;

&lt;p&gt;The line '&lt;code&gt;local $b = 1;&lt;/code&gt;' introduces a localisation - it tells the interpreter to save the current copy of the (package) variable &lt;code&gt;$b&lt;/code&gt;, to be restored when execution reaches the end of the scope. Now it is a matter of time: after this localisation has been executed, anyone that looks at this package variable &lt;code&gt;$b&lt;/code&gt; before the end of this scope is reached will see the localised version. If the &lt;code&gt;foo&lt;/code&gt; subroutine refers to the package variable &lt;code&gt;$b&lt;/code&gt;, it will see the localised version (for this call, at least).&lt;/p&gt;

&lt;p&gt;Does that help at all?&lt;/p&gt;

Hugo</field>
<field name="root_node">
213855</field>
<field name="parent_node">
242833</field>
</data>
</node>
