blib/lib/Coro/LocalScalar.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 81 CC 4 SCAR 14.1
CC
4
Cov
0%
_proto
12.0
attach
6.0
new
2.0
CRAP 4.1
1 package Coro::LocalScalar;
2 use strict;
3 no strict 'refs';
4 use Guard;
5 use Carp;
6 use Scalar::Util qw/weaken/;
7 our $VERSION = '0.21';
8
9 =head1 NAME
10
11 Coro::LocalScalar - local() for Coro
12
13 =head1 ABOUT
14
15 Perl local() function unuseful for Coro threads. This module uses tie magick to make scalar local for each Coro thread. Unlike L<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. It has an overhead of calling one callback per localized variable when coroutine is destroyed.
16
17 =head1 SYNOPSIS
18
19 use Coro;
20 use Coro::LocalScalar;
21 use Coro::EV;
22
23 my $scalar;
24
25 Coro::LocalScalar->new->localize($scalar);
26
27 async {
28 $scalar = "thread 1";
29 print "1 - $scalar\n";
30 cede;
31 print "3 - $scalar\n";
32 cede;
33 print "5 - $scalar\n";
34
35 };
36
37 async {
38 $scalar = "thread 2";
39 print "2 - $scalar\n";
40 cede;
41 print "4 - $scalar\n";
42 cede;
43 print "6 - $scalar\n";
44 };
45 EV::loop;
46
47 prints
48 1 - thread 1
49 2 - thread 2
50 3 - thread 1
51 4 - thread 2
52 5 - thread 1
53 6 - thread 2
54
55
56
57 my $obj = Coro::LocalScalar->new;
58
59 # no tie magick used
60 $obj->value("data");
61 $obj->value = "data";
62 my $value = $obj->value;
63
64 #or
65
66 my $local_lvalue_closure = $obj->closure; # lvalue coderef
67
68 $local_lvalue_closure->() = "local data"; # no tie magick used
69
70
71
72 my $testobj = Someclass->new;
73
74 # attach setter/getter and tied hash element to your object
75 $obj->attach($testobj, 'element_local_in_coros');
76
77 $testobj->element_local_in_coros("data");
78 $testobj->element_local_in_coros = "data";
79
80 $testobj->{element_local_in_coros}; # tie magick used
81
82
83 =cut
84
85
86
87
88
89 sub new { bless {} }
90 sub TIESCALAR {$_[1]};
91
92 sub closure {
93 my $self = shift;
94
95 return sub :lvalue {
96 $self->get;
97 }
98 }
99
100 sub localize { # tie scalar to container
101 my $self = shift;
102
103 $self->{scalar} = \$_[0];
104 weaken $self->{scalar}; # no circular
105
106 tie($_[0], __PACKAGE__, $self );
107 $self;
108 }
109
110 sub attach { # attach setter/getter and tied hash element to class
111 # $container->attach($comeobj, 'db_conn'); $comeobj->{db_conn} and $comeobj->db_conn now local in Coros
112 *{ref($_[1]).'::'.$_[2]} = $_[0]->closure;
113
114 $_[1]->{$_[2]} = undef unless exists $_[1]->{$_[2]};
115 $_[0]->localize($_[1]->{$_[2]});
116
117 $_[0];
118 }
119
120
121
122
123 sub _proto : lvalue {
124 my $self = shift;
125
126 unless(exists($self->{$Coro::current})){
127 my $coro = $Coro::current;
128
129 $self->{$coro} = undef;
130
131 # init coro data destructor
132 $coro->on_destroy(sub {
133
134 ${ $self->{scalar} } = undef; # Delete scalar value to prevent unexpected behavior
135 #perl stores values in scalar even if it tied. When Coro::LocalScalar destroys internal value, value stored in scalar still persists although you expected that it would be deleted( and DESTROY called if it`s object)
136
137 delete $self->{$coro};
138 undef $coro;
139 });
140
141 };
142 if(@_){ $self->{$Coro::current} = shift }
143
144 $self->{$Coro::current};
145 }
146
147 *STORE = *_proto;
148
149 sub FETCH : lvalue { shift->{$Coro::current} };
150
151
152 our $AUTOLOAD;
153 sub AUTOLOAD :lvalue { *{$AUTOLOAD} = *_proto; shift->_proto }
154
155 sub DESTROY {}
156 sub UNTIE {};
157
158 1;