File Coverage

blib/lib/Coro/Specific.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 17 100.0


line stmt bran cond sub pod time code
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 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 manually, it will be loaded
29             automatically when you C and call the C constructor.
30              
31             =over 4
32              
33             =cut
34              
35             package Coro::Specific;
36              
37 2     2   1371 use common::sense;
  2         16  
  2         10  
38              
39             our $VERSION = 6.512;
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 2     2   49 my $var;
53 2         9 tie $var, Coro::Specific::;
54 2         6 \$var;
55             }
56              
57             sub TIESCALAR {
58 2     2   6 my $idx = $idx++;
59 2         8 bless \$idx, $_[0];
60             }
61              
62             sub FETCH {
63 11     11   112 $Coro::current->{_specific}[${$_[0]}];
  11         45  
64             }
65              
66             sub STORE {
67 5     5   30 $Coro::current->{_specific}[${$_[0]}] = $_[1];
  5         21  
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
86             http://software.schmorp.de/pkg/Coro.html
87              
88             =cut
89