in reply to Re: Debugger and lexicals
in thread Debugger and lexicals
when run, this outputs:{ my $a = 1; sub f1 { $a; print "f1: a=", eval('$a'), "\n"; } sub f2 { print "f2: a=", eval('$a'), "\n"; } } f1(); f2();
(The block is there to mimic the implied block that's wrapped around a use'd file). Here, f1 creates a closure, which means it captures the initial (and in this case, only) instance of $a; i.e. a reference to $a is stored in f1's pad. During execution, when the block end is reached, $a goes out of scope for the main sub, and the reference to it from the main pad is removed. When the evals are run, the eval in f1 sees $a since that sub has captured it; whereas the f2 eval can't see it, as it wasn't captured by f2, and no longer exists in the parent.f1: a=1 f2: a=
Dave.
|
|---|