1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Coro::Specific - manage coroutone-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 |
|
|
=over 4 |
22 |
|
|
|
23 |
|
|
=cut |
24 |
|
|
|
25 |
|
|
package Coro::Specific; |
26 |
|
|
|
27 |
pcg |
1.25 |
BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
28 |
root |
1.1 |
|
29 |
pcg |
1.29 |
$VERSION = 0.97; |
30 |
root |
1.1 |
|
31 |
|
|
=item new |
32 |
|
|
|
33 |
root |
1.8 |
Create a new coroutine-specific scalar and return a reference to it. The |
34 |
|
|
scalar is guarenteed to be "undef". Once such a scalar has been allocated |
35 |
|
|
you cannot deallocate it (yet), so allocate only when you must. |
36 |
root |
1.1 |
|
37 |
|
|
=cut |
38 |
|
|
|
39 |
|
|
my $idx; |
40 |
|
|
|
41 |
|
|
sub new { |
42 |
|
|
my $var; |
43 |
|
|
tie $var, Coro::Specific::; |
44 |
|
|
\$var; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
sub TIESCALAR { |
48 |
root |
1.8 |
my $idx = $idx++; |
49 |
root |
1.1 |
bless \$idx, $_[0]; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
sub FETCH { |
53 |
root |
1.8 |
$Coro::current->{specific}[${$_[0]}]; |
54 |
root |
1.1 |
} |
55 |
|
|
|
56 |
|
|
sub STORE { |
57 |
root |
1.8 |
$Coro::current->{specific}[${$_[0]}] = $_[1]; |
58 |
root |
1.1 |
} |
59 |
|
|
|
60 |
root |
1.8 |
#sub DESTROY { |
61 |
|
|
# push @idx, $$_[0]; |
62 |
|
|
#} |
63 |
root |
1.1 |
|
64 |
|
|
1; |
65 |
|
|
|
66 |
|
|
=back |
67 |
|
|
|
68 |
|
|
=head1 BUGS |
69 |
|
|
|
70 |
|
|
The actual coroutine specific values do not automatically get destroyed |
71 |
|
|
when the Coro::Specific object gets destroyed. |
72 |
|
|
|
73 |
|
|
=head1 AUTHOR |
74 |
|
|
|
75 |
|
|
Marc Lehmann <pcg@goof.com> |
76 |
|
|
http://www.goof.com/pcg/marc/ |
77 |
|
|
|
78 |
|
|
=cut |
79 |
|
|
|