1 |
=head1 NAME |
2 |
|
3 |
Coro::Specific - manage coroutine-specific variables. |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::Specific; |
8 |
|
9 |
my $ref = new Coro::Specific; |
10 |
|
11 |
$$ref = 5; |
12 |
print $$ref; |
13 |
|
14 |
=head1 DESCRIPTION |
15 |
|
16 |
This module can be used to create variables (or better: references to |
17 |
them) that are specific to the currently executing coroutine. This module |
18 |
does not automatically load the Coro module (so the overhead will be small |
19 |
when no coroutines are used). |
20 |
|
21 |
A much faster method is to store extra keys into C<%$Coro::current> |
22 |
- all you have to do is to make sure that the key is unique (e.g. by |
23 |
prefixing it with your module name). You can even store data there before |
24 |
loading the L<Coro> module - when Coro is loaded, the keys stored in |
25 |
C<%$Coro::current> are automatically attached to the coro thread executing |
26 |
the main program. |
27 |
|
28 |
You don't have to load C<Coro::Specific> manually, it will be loaded |
29 |
automatically when you C<use Coro> and call the C<new> constructor. |
30 |
|
31 |
=over 4 |
32 |
|
33 |
=cut |
34 |
|
35 |
package Coro::Specific; |
36 |
|
37 |
use common::sense; |
38 |
|
39 |
our $VERSION = 6.57; |
40 |
|
41 |
=item new |
42 |
|
43 |
Create a new coroutine-specific scalar and return a reference to it. The |
44 |
scalar is guaranteed to be "undef". Once such a scalar has been allocated |
45 |
you cannot deallocate it (yet), so allocate only when you must. |
46 |
|
47 |
=cut |
48 |
|
49 |
my $idx; |
50 |
|
51 |
sub new { |
52 |
my $var; |
53 |
tie $var, Coro::Specific::; |
54 |
\$var; |
55 |
} |
56 |
|
57 |
sub TIESCALAR { |
58 |
my $idx = $idx++; |
59 |
bless \$idx, $_[0]; |
60 |
} |
61 |
|
62 |
sub FETCH { |
63 |
$Coro::current->{_specific}[${$_[0]}]; |
64 |
} |
65 |
|
66 |
sub STORE { |
67 |
$Coro::current->{_specific}[${$_[0]}] = $_[1]; |
68 |
} |
69 |
|
70 |
#sub DESTROY { |
71 |
# push @idx, $$_[0]; |
72 |
#} |
73 |
|
74 |
1; |
75 |
|
76 |
=back |
77 |
|
78 |
=head1 BUGS |
79 |
|
80 |
The actual coroutine specific values do not automatically get destroyed |
81 |
when the Coro::Specific object gets destroyed. |
82 |
|
83 |
=head1 AUTHOR/SUPPORT/CONTACT |
84 |
|
85 |
Marc A. Lehmann <schmorp@schmorp.de> |
86 |
http://software.schmorp.de/pkg/Coro.html |
87 |
|
88 |
=cut |
89 |
|