[Wellington-pm] How to cure "variable will not stay shared?

Grant McLean grant at mclean.net.nz
Thu Nov 3 16:23:15 PDT 2011


On Fri, 2011-11-04 at 09:08 +1000, Srdjan wrote:
> I think that inner subs vars do not
> take outer sub scope anyway. Can anyone confirm this?

A Perl subroutine does have access to variables from the enclosing
scope.  This is useful because it allows the inner sub to access the
current value of any shared variable at the time the inner sub is
called.

However this really only makes sense when defining anonymous subroutines
and passing them around as callbacks, e.g.:

  $stop_button->signal_connect(clicked => sub { $app->stop_timer() });

It's almost always a bug in Perl to create a nested named subroutine (as
opposed to an anonymous one).

Cheers
Grant




More information about the Wellington-pm mailing list