<?xml version="1.0" encoding="windows-1252"?>
<node id="164515" title="Re: Question of scope" created="2002-05-06 23:43:04" updated="2005-07-19 14:08:11">
<type id="11">
note</type>
<author id="2329">
stephen</author>
<data>
<field name="doctext">
&lt;p&gt;Be careful, for here you are using a closure. Remember, when an anonymous subroutine is defined, it uses whatever lexical variables are in scope at the time it was created. So:
&lt;code&gt;
use strict;

my @foo = ();

my $pushit = sub {
    push @foo, @_;
};

$pushit-&gt;('apple');
$pushit-&gt;('orange');

print @foo, "\n";
&lt;/code&gt;
will print "appleorange", but
&lt;code&gt;
use strict;

my @foo = ();

my $pushit = sub {
    push @foo, @_;
};

my @foo = ();

$pushit-&gt;('apple');
$pushit-&gt;('orange');

print @foo, "\n";
&lt;/code&gt;
will print nothing, since the @foo that is printed is not the same @foo that is being populated. (That'll also happen if you're not using strict, and somehow only declare @foo after the closure is created.) If this is the case, it'll show up if you turn warnings on... if the new @foo (or @coords in your case) is in the same scope as the other one. If @coords is file-scoped, you might well want to use 'our' instead of 'my'... that way you're guaranteed a single variable.
&lt;/p&gt;
 
&lt;p&gt;
stephen
&lt;/p&gt;</field>
<field name="root_node">
164411</field>
<field name="parent_node">
164411</field>
</data>
</node>
