Callback-Frame
view release on metacpan or search on metacpan
DESCRIPTION
The problem that this module solves is that although closures preserve
their lexical environment, they don't preserve error handlers or "local"
variables.
Consider the following piece of broken code:
use AnyEvent;
eval {
$watcher = AE::timer 0.1, 0,
sub {
die "some error";
};
};
## broken!
if ($@) {
print STDERR "Oops: $@";
}
By the way, "lexical" and "dynamic" are the lisp terms. When it applies
to variables, perl confusingly calls dynamic scoping "local" scoping,
even though the scope is temporal, not local.
Here is how we could fix the code above using Callback::Frame:
use AnyEvent;
use Callback::Frame;
frame_try {
$watcher = AE::timer 0.1, 0, fub {
die "some error";
};
} frame_catch {
print STDERR "Oops: $@";
};
AE::cv->recv;
Now we see the desired error message:
lib/Callback/Frame.pm view on Meta::CPAN
=head1 DESCRIPTION
The problem that this module solves is that although closures preserve their lexical environment, they don't preserve error handlers or C<local> variables.
Consider the following piece of B<broken> code:
use AnyEvent;
eval {
$watcher = AE::timer 0.1, 0,
sub {
die "some error";
};
};
## broken!
if ($@) {
print STDERR "Oops: $@";
}
lib/Callback/Frame.pm view on Meta::CPAN
The root of the problem is that the dynamic environment has not been preserved. In this case it is the dynamic exception handlers that we would like to preserve. In some other cases we would like to preserve dynamically scoped (aka "local") variables...
By the way, "lexical" and "dynamic" are the lisp terms. When it applies to variables, perl confusingly calls dynamic scoping "local" scoping, even though the scope is temporal, not local.
Here is how we could fix the code above using L<Callback::Frame>:
use AnyEvent;
use Callback::Frame;
frame_try {
$watcher = AE::timer 0.1, 0, fub {
die "some error";
};
} frame_catch {
print STDERR "Oops: $@";
};
AE::cv->recv;
Now we see the desired error message:
( run in 0.957 second using v1.01-cache-2.11-cpan-49f99fa48dc )