How about switching to objects, and create a closure for your callback:
use threads::shared;
my $obj = bless {}, 'foo';
sub foo::callback {
warn "I've been called ".++($_[0]->{count})." times\n";
}
share($obj); # only needed in a threaded environment
my $thread = threads->create('start_thread', sub { $obj->callback(@_)
+} );
$thread->join;
print "count = ", $obj->{count}, "\n"; # prints 3
sub start_thread {
my $cb = shift;
for (1..3) { $cb->() }
}
Is there a way to implement function call behavior for a blessed reference? If so, you could have the best of both worlds. |