New feature: eval code for send/croak with condvar
David
post at david-zurb.org
Wed May 6 10:29:19 CEST 2015
Hi,
I'm using the following snippet in many cases:
my $cv = AE::cv;
eval {
do_something_that_might_croak;
$cv->send(...);
};
if ($@) {
$cv->croak($@);
}
That ensures to return the result or croak properly by $cv->recv.
Nowadays I subclass AnyEvent::CondVar with an additional method. The
package looks like:
package My::CondVar;
use base 'AnyEvent::CondVar';
sub eval {
my $self = shift;
$self = $self->new unless ref $self;
my $code = shift;
eval {
$self->send($code->(@_));
};
if ($@) {
$self->croak($@);
}
return $self;
}
That allows the following syntax for producers:
my $cv = AE::cv;
$cv->eval(sub {
do_something_that_might_croak;
return (1, 2, 3);
});
my ($one, $two, $three) = $cv->recv; # may croak
# this very short syntax is also possible
my $cv = AE::cv->eval(sub { ... });
Is it possible to add the eval() method to AnyEvent::CondVar? I think
many others could make profit of it...
Greetings,
David
More information about the anyevent
mailing list