in reply to Why are closures cool?
if ( $cool_stuff ) { my $foo = 0; my $bar = 42; $my_gui_object->add_callback( "<Neato_Event>", sub { my ($event, $argument) = ( @_ + ); if ( $argument == $bar ) { $foo++; return 1; } return 0; }, ); $my_gui_object->add_callback( "<WayCool_Event>", sub { my ($event, $argument) = ( @_ + ); if ( $argument == $bar ) { $foo--; return 1; } return 0; }, ); }
now $foo is out of scope, safe and snug in it's closures. And those closures are going to be run in an unknown namespace. The only other way to get this behavior would be with... globals of some kind. bleh. :)
|
|---|