NAME
Coro::LocalScalar - local() for Coro
ABOUT
Perl local() function unuseful for Coro threads. This module uses tie magick to make scalar local for each Coro thread. Unlike Coro::Specific this module destroys all data attached to coroutine when coroutine gets destroyed. It's useful when you need to call destructors of coroutine local objects when coroutine destroyed. And you can easily localize value of hash with this module
SYNOPSIS
use Coro;
use Coro::LocalScalar;
use Coro::EV;
my $scalar;
Coro::LocalScalar->new->localize($scalar);
# or just
Coro::LocalScalar->new($scalar);
async {
$scalar = "thread 1";
print "1 - $scalar\n";
cede;
print "3 - $scalar\n";
cede;
print "5 - $scalar\n";
};
async {
$scalar = "thread 2";
print "2 - $scalar\n";
cede;
print "4 - $scalar\n";
cede;
print "6 - $scalar\n";
};
EV::loop;
prints
1 - thread 1
2 - thread 2
3 - thread 1
4 - thread 2
5 - thread 1
6 - thread 2
my $obj = Coro::LocalScalar->new;
# no tie magick used
$obj->value("data");
$obj->value = "data";
my $value = $obj->value;
#or
my $local_lvalue_closure = $obj->closure; # lvalue coderef
$local_lvalue_closure->() = "local data"; # no tie magick used
my $testobj = Someclass->new;
# attach setter/getter and tied hash element to your object
$obj->attach($testobj, 'element_local_in_coros');
$testobj->element_local_in_coros("data");
$testobj->element_local_in_coros = "data";
$testobj->{element_local_in_coros}; # tie magick used