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.
SYNOPSIS
use Coro;
use Coro::LocalScalar;
use Coro::EV;
my $scalar;
Coro::LocalScalar->new->localize($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