/usr/share/perl5/AnyEvent/Callback.pm is in libanyevent-callback-perl 0.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | package AnyEvent::Callback;
use 5.010001;
use strict;
use warnings;
require Exporter;
use base 'Exporter';
use Carp;
our @EXPORT = qw(CB CBS);
our $VERSION = '0.06';
=head1 NAME
AnyEvent::Callback - callback aggregator for L<AnyEvent> watchers.
=head1 SYNOPSIS
use AnyEvent::Callback;
# usually watchers are looked as:
AE::something @args, sub { ... };
AE::something
@args,
sub { ... }, # result
sub { ... }; # error
use AnyEvent::Callback;
AE::something @args, CB { ... };
AE::something @args,
CB sub { ... }, # result
sub { ... }; # error
AE::something @args,
CB sub { ... }, # result
sub { ... }, # error
sub { ... }; # anyway callback
Callback hierarchy
my $cbchild = $cb->CB(sub { ... });
...
$cbchild->error('error'); # will call $cb->error('error');
Inside Your callback You can:
sub my_watcher {
my $cb = pop;
my @args = @_;
# ...
$cb->error( @error ); # error callback will be called
# or:
$cb->( $value ); # result callback will be called
}
Callbacks stack
my $cbs = CBS;
for (1 .. $n) {
AE::something @args, $cbs->cb;
}
$cbs->wait(sub {
for (@_) {
if ($_->is_error) { # handle one error
my @err = $_->errors; # or:
my $errstr = $_->errstr;
} else { # results
my @res = $_->results;
}
}
});
=head1 DESCRIPTION
The module allows You to create callback's hierarchy. Also the module groups
error and result callbacks into one object.
Also the module checks if one callback was called by watcher or not.
If a watcher doesn't call result or error callback, error callback will be
called automatically.
Also the module checks if a callback was called reentrant. In the case the
module will complain (using L<Carp/carp>).
If a watcher touches error callback and if superior didn't define error
callback, the module will call error callback upwards hierarchy. Example:
AE::something @args, CB \&my_watcher, \&on_error;
sub on_error {
}
sub my_watcher {
my $cb = pop;
...
the_other_watcher $cb->CB( sub { # error callback wasn't defined
my $cb = pop;
...
yet_another_watcher1 $cb->CB( sub {
my $cb = pop;
...
$cb->( 123 ); # upwards callback
});
yet_another_watcher2 $cb->CB( sub {
my $cb = pop;
...
$cb->error( 456 ); # on_error will be called
});
});
}
=head1 METHODS
=head2 'CODE' (overloaded fake method)
$cb->( ... );
You can use the object as usually B<CODEREF>.
=cut
use overload
'&{}' => sub {
my ($self) = shift;
sub {
$self->{called}++;
carp "Repeated callback calling: $self->{called}"
if $self->{called} > 1;
carp "Calling result callback after error callback"
if $self->{ecalled};
if ($self->{cb}) {
$self->{cb}->(@_);
$self->{acb}->() if $self->{acb};
}
delete $self->{cb};
delete $self->{acb};
delete $self->{ecb};
delete $self->{parent};
return;
};
},
bool => sub { 1 } # for 'if ($cb)'
;
=head2 CB
Creates new callback object that have binding on parent callback.
my $new_cb = $cb->CB(sub { ... }); # the cb doesn't catch errors
my $new_cb = CB(sub { ... }, sub { ... }); # the cb catches errors
my $new_cb = $cb->CB(sub { ... }, sub { ... }); # the same
=cut
sub CB(&;&&) {
my $parent;
my ($cb, $ecb, $acb) = @_;
($parent, $cb, $ecb, $acb) = @_ unless 'CODE' eq ref $cb;
croak 'Callback must be CODEREF' unless 'CODE' eq ref $cb;
croak 'Error callback must be CODEREF or undef'
unless 'CODE' eq ref $ecb or !defined $ecb;
croak 'Anyway callback must be CODEREF or undef'
unless 'CODE' eq ref $acb or !defined $acb;
# don't translate erorrs upwards if error callback if exists
$parent = undef if $ecb;
my $self = bless {
cb => $cb,
ecb => $ecb,
acb => $acb,
parent => $parent,
called => 0,
ecalled => 0,
} => __PACKAGE__;
$self;
}
sub CBS {
return AnyEvent::Callback::Stack->new;
}
=head2 error
Calls error callback. If the object has no registered error callbacks,
parent object's error callback will be called.
$cb->error('WTF?');
=cut
sub error {
my ($self, @error) = @_;
$self->{ecalled}++;
carp "Repeated error callback calling: $self->{ecalled}"
if $self->{ecalled} > 1;
carp "Calling error callback after result callback"
if $self->{called};
if ($self->{ecb}) {
$self->{ecb}( @error );
$self->{acb}() if $self->{acb};
delete $self->{ecb};
delete $self->{cb};
delete $self->{parent};
delete $self->{acb};
return;
}
my $acb = delete $self->{acb};
delete $self->{ecb};
delete $self->{cb};
my $parent = delete $self->{parent};
if ($parent) {
$parent->error( @error );
} else {
carp "Uncaught error: @error";
}
$acb->() if $acb;
return;
}
sub DESTROY {
my ($self) = @_;
return if $self->{called} or $self->{ecalled};
$self->error("no one touched registered callback");
delete $self->{cb};
delete $self->{ecb};
}
package AnyEvent::Callback::Stack;
use Scalar::Util 'weaken';
use Carp;
sub new {
my ($class) = @_;
return bless { stack => [], done => 0 } => ref($class) || $class;
}
sub cb {
my ($self) = @_;
my $idx = @{ $self->{stack} };
my $cb = AnyEvent::Callback::CB
sub {
$self->{stack}[$idx] = AnyEvent::Callback::Stack::Result->new(@_);
$self->{done}++;
$self->_check_if_done;
},
sub {
$self->{stack}[$idx] = AnyEvent::Callback::Stack::Result->err(@_);
$self->{done}++;
$self->_check_if_done;
}
;
push @{ $self->{stack} } => $cb;
weaken $self->{stack}[$idx];
return $self->{stack}[$idx];
}
sub _check_if_done {
my ($self) = @_;
return unless $self->{waiter};
return unless $self->{done} >= @{ $self->{stack} };
my $cb = delete $self->{waiter};
$cb->(@{ $self->{stack} });
$self->{stack} = [];
$self->{done} = 0;
}
sub wait :method {
my ($self, $cb) = @_;
croak 'Usage: $cbs->wait(sub { ... })' unless 'CODE' eq ref $cb;
croak 'You have already initiated wait process' if $self->{waiter};
$self->{waiter} = $cb;
$self->_check_if_done;
}
package AnyEvent::Callback::Stack::Result;
sub new {
my ($class, @res) = @_;
return bless { res => \@res } => ref($class) || $class;
}
sub err {
my ($class, @res) = @_;
return bless { err => \@res, res => [] } => ref($class) || $class;
}
sub is_error {
my ($self) = @_;
return exists $self->{err};
}
sub results {
my ($self) = @_;
return $self->{res} unless wantarray;
return @{ $self->{res} };
}
sub errors {
my ($self) = @_;
return unless $self->is_error;
return $self->{err} unless wantarray;
return @{ $self->{err} };
}
sub errstr {
my ($self) = @_;
return join ' ' => $self->errors;
}
=head1 COPYRIGHT AND LICENCE
Copyright (C) 2012 by Dmitry E. Oboukhov
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|