File Coverage

lib/AnyEvent/Tools.pm
Criterion Covered Total %
statement 114 123 92.6
branch 23 34 67.6
condition 6 12 50.0
subroutine 32 33 96.9
pod 7 8 87.5
total 182 210 86.6


line stmt bran cond sub pod time code
1             package AnyEvent::Tools;
2              
3 7     7   534880 use 5.010001;
  7         28  
  7         288  
4 7     7   38 use strict;
  7         15  
  7         268  
5 7     7   151 use warnings;
  7         17  
  7         214  
6 7     7   40 use Carp;
  7         27  
  7         702  
7              
8             require Exporter;
9 7     7   5913 use AnyEvent::Util;
  7         100530  
  7         736  
10 7     7   5260 use AnyEvent::AggressiveIdle 0.04, qw(aggressive_idle stop_aggressive_idle);
  7         6020  
  7         2151  
11              
12             our @ISA = qw(Exporter);
13              
14             our %EXPORT_TAGS = (
15             all => [
16             qw(
17             mutex rw_mutex
18             async_for async_repeat
19             async_rfor async_foreach
20             pool buffer
21             )
22             ],
23             mutex => [ qw( mutex rw_mutex ) ],
24             foreach => [ qw( async_for async_rfor async_repeat ) ],
25             pool => [ qw( pool buffer ) ],
26              
27              
28             );
29              
30             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
31              
32             our @EXPORT = qw();
33              
34             our $VERSION = '0.12';
35              
36             sub pool(@)
37             {
38 2     2 1 6884 require AnyEvent::Tools::Pool;
39              
40 7     7   44 no strict 'refs';
  7         13  
  7         221  
41 7     7   34 no warnings 'redefine';
  7         15  
  7         914  
42 2         100 *{ __PACKAGE__ . "::pool" } = sub (@) {
43 2     2   14 return AnyEvent::Tools::Pool->new(@_);
44 2         13 };
45              
46 2         10 goto &pool;
47             }
48              
49              
50             sub buffer(@)
51             {
52 4     4 1 42302 require AnyEvent::Tools::Buffer;
53 7     7   37 no warnings 'redefine';
  7         10  
  7         234  
54 7     7   109 no strict 'refs';
  7         14  
  7         727  
55 4         33 *{ __PACKAGE__ . "::buffer" } = sub (@) {
56 4     4   26 return new AnyEvent::Tools::Buffer(@_);
57 4         22 };
58              
59 4         18 goto &buffer;
60             }
61              
62             sub mutex()
63             {
64 3     3 1 51641 require AnyEvent::Tools::Mutex;
65              
66 7     7   35 no strict 'refs';
  7         12  
  7         214  
67 7     7   34 no warnings 'redefine';
  7         18  
  7         833  
68 3         32 *{ __PACKAGE__ . "::mutex" } = sub () {
69 3     3   25 return AnyEvent::Tools::Mutex->new;
70 3         19 };
71              
72 3         13 goto &mutex;
73             }
74              
75             sub rw_mutex()
76             {
77 3     3 1 4180 require AnyEvent::Tools::RWMutex;
78              
79 7     7   57 no strict 'refs';
  7         12  
  7         218  
80 7     7   46 no warnings 'redefine';
  7         10  
  7         8443  
81 3         29 *{ __PACKAGE__ . "::rw_mutex" } = sub () {
82 3     3   19 return AnyEvent::Tools::RWMutex->new;
83 3         20 };
84              
85 3         15 goto &rw_mutex;
86             }
87              
88             sub _async_repeati($$&;&);
89             sub async_repeat($&;&) {
90 17     17 1 2607504 my ($count, $cb, $cbe) = @_;
91              
92 17 100       71 if (!$count) {
93 2 100       8 $cbe->() if $cbe;
94 2         97 return;
95             }
96 15         66 return &_async_repeati(0, $count, $cb, $cbe);
97             }
98              
99             sub async_for($&;&) {
100 5     5 1 513015 my ($obj, $cb, $cbe) = @_;
101 5 100 66     34 if ('ARRAY' eq ref $obj or "$obj" =~ /=ARRAY\(/) {
102 4 100       15 unless (@$obj) {
103 2 100       9 $cbe->() if $cbe;
104 2         94 return;
105             }
106             return &async_repeat(
107             scalar(@$obj),
108             sub {
109 20     20   37 my ($g, $index, $first, $last) = @_;
110 20         81 $cb->($g, $obj->[$index], $index, $first, $last);
111             },
112 2         16 $cbe
113             );
114             }
115              
116 1 50 33     8 if ('HASH' eq ref $obj or "$obj" =~ /=HASH\(/) {
117 1 50       5 unless (%$obj) {
118 0 0       0 $cbe->() if $cbe;
119 0         0 return;
120             }
121              
122 1         8 my @keys = keys %$obj;
123             return &async_repeat(
124             scalar(@keys),
125             sub {
126 10     10   17 my ($g, $index, $first, $last) = @_;
127 10         28 $cb->($g, $keys[$index], $obj->{$keys[$index]}, $first, $last);
128             },
129 1         9 $cbe
130             );
131             }
132              
133 0         0 croak "Usage: async_for ARRAYREF|HASHREF, callback [, end_callback ]";
134             }
135              
136              
137 0     0 0 0 sub async_foreach($&;&) { goto &async_for; }
138              
139              
140             sub async_rfor($&;&) {
141 2     2 1 16133 my ($obj, $cb, $cbe) = @_;
142 2 100 66     23 if ('ARRAY' eq ref $obj or "$obj" =~ /=ARRAY\(/) {
143 1 50       5 unless (@$obj) {
144 0 0       0 $cbe->() if $cbe;
145 0         0 return;
146             }
147             return &async_repeat(
148             scalar(@$obj),
149             sub {
150 10     10   17 my ($g, $index, $first, $last) = @_;
151 10         33 $cb->(
152             $g,
153             $obj->[$#$obj - $index],
154             $#$obj - $index,
155             $first,
156             $last
157             );
158             },
159 1         7 $cbe
160             );
161             }
162              
163 1 50 33     8 if ('HASH' eq ref $obj or "$obj" =~ /=HASH\(/) {
164 1 50       10 unless (%$obj) {
165 0 0       0 $cbe->() if $cbe;
166 0         0 return;
167             }
168              
169 1         7 my @keys = keys %$obj;
170             return &async_repeat(
171             scalar(@keys),
172             sub {
173 10     10   20 my ($g, $index, $first, $last) = @_;
174 10         39 $cb->(
175             $g,
176             $keys[$#keys - $index],
177             $obj->{$keys[$#keys - $index]},
178             $first,
179             $last
180             );
181             },
182 1         10 $cbe
183             );
184             }
185              
186 0         0 croak "Usage: async_for ARRAYREF|HASHREF, callback [, end_callback ]";
187             }
188              
189             sub _async_repeati($$&;&) {
190 15     15   78 my ($start, $count, $cb, $cbe) = @_;
191              
192 15         25 my $idle;
193 15         29 my $wantarray = wantarray;
194             $idle = aggressive_idle sub {
195              
196 159     159   2614575 my (undef, $guard) = @_;
197 159         658 my $first = $start == 0;
198 159         334 my $last = $start >= $count - 1;
199              
200 159 100       711 if ($start >= $count) {
201 10 100       48 $cbe->() if $cbe;
202 10         111 undef $idle;
203 10         42 undef $cb;
204 10         229 undef $cbe;
205 10         28 undef $guard;
206 10         44 return;
207             }
208              
209 149         433 $cb->($guard, $start, $first, $last);
210 149         2085 $start++;
211 15         137 };
212              
213 15 100       616 return unless defined $wantarray;
214 6     6   47 return guard { undef $cbe; undef $cb; undef $idle; };
  6         101212  
  6         16  
  6         40  
215             }
216              
217             1;
218             __END__