<?xml version="1.0" encoding="windows-1252"?>
<node id="809069" title="Re: closure clarity, please" created="2009-11-24 08:15:41" updated="2009-11-24 08:15:41">
<type id="11">
note</type>
<author id="763880">
vitoco</author>
<data>
<field name="doctext">
&lt;p&gt;As [ikegami] said, &lt;c&gt;sub&lt;/c&gt;s are global:&lt;/p&gt;

&lt;code&gt;
#!perl
use v5.10;
use strict;
use warnings;

my $a = shift;

sub f {
  say "global f";
}

sub g {
  say "global g";
  my ($a) = @_;
  sub f {
    say "local f ($a)"; # line 16
  }
  f();
}

f();
g(shift);
f();

__END__

C:\Temp&gt;localsub.pl
Variable "$a" will not stay shared at C:\Temp\localsub.pl line 16.
Subroutine f redefined at C:\Temp\localsub.pl line 15.
Use of uninitialized value $a in concatenation (.) or string at C:\Temp\localsub.pl line 16.
local f ()
global g
Use of uninitialized value $a in concatenation (.) or string at C:\Temp\localsub.pl line 16.
local f ()
Use of uninitialized value $a in concatenation (.) or string at C:\Temp\localsub.pl line 16.
local f ()

C:\Temp&gt;localsub.pl AAA BBB
Variable "$a" will not stay shared at C:\Temp\localsub.pl line 16.
Subroutine f redefined at C:\Temp\localsub.pl line 15.
Use of uninitialized value $a in concatenation (.) or string at C:\Temp\localsub.pl line 16.
local f ()
global g
local f (BBB)
local f (BBB)
&lt;/code&gt;

&lt;p&gt;What is a strange behavior is that variables got stuck with a value, even if it is assigned after it's first use... That does not seem to be a compile time assignment.&lt;/p&gt;

&lt;p&gt;Conclusion: always define global subroutines or pass variables as parameters to them if defined locally for clarity.&lt;/p&gt;
</field>
<field name="root_node">
809000</field>
<field name="parent_node">
809000</field>
</data>
</node>
