On Wed, Aug 20, 2003 at 07:12:22PM +0100, Nick Ing-Simmons wrote: > Dave Mitchell <davem@fdgroup.com> writes: > >But named subs also capture their lexical state at creation time, > > Things get confusing: > > > sub harry > { > my $foo = shift; > sub fred { print "$foo\n" } > } > > harry('Ouch'); > harry('Weird'); > > > fred(); Run it with -w and you get: Variable "$foo" will not stay shared at /tmp/p line 6. Subs capture their lexical environment at creation time. When fred is created, the instance of $foo it captures happens to be the first one. A bit later, 'Ouch' is assigned to this instance. Even later, 'Weird' is assigned to the second instance of $foo, but fred still only sees the first instance. D. -- Justice is when you get what you deserve. Law is when you get what you pay for.Thread Previous