for (1 .. 5) {
sub {
eval {
- # create Scope::Context objects
+ # Create Scope::Context objects for different upper frames.
my ($block, $sub, $eval, $loop);
{
$block = Scope::Context->new;
# This prints "hello" when the eval block above ends.
$eval->reap(sub { print "hello\n" });
- # Ignore $SIG{__DIE__} just for the loop.
+ # Ignore $SIG{__DIE__} just for the loop body.
$loop->localize_delete('%SIG', '__DIE__');
# Execute the callback as if it ran in place of the sub.
# Immediately return (1, 2, 3) from the sub, bypassing the eval.
$sub->unwind(@values, 3);
+
+ # Not reached.
}
+
+ # Not reached.
}->();
+
+ # unwind() returns here. "hello\n" was printed, and now
+ # $SIG{__DIE__} is undefined.
}
=head1 DESCRIPTION
The L<Scope::Context> methods actually do more than their subroutine counterparts from L<Scope::Upper> : before each call, the target context will be checked to ensure it is still active (which means that it is still present in the current call stack), and an exception will be thrown if you attempt to act on a context that has already expired.
This means that :
- my $sc;
+ my $cxt;
{
- $sc = Scope::Context->new;
+ $cxt = Scope::Context->new;
}
- $sc->reap(sub { print "hello\n });
+ $cxt->reap(sub { print "hello\n });
will croak when L</reap> is called.
}
sub inner {
- my $sub = Scope::Context->new->sub(1); # = Scope::Context->sub
+ my $sub = Scope::Context->new->sub(1); # = Scope::Context->sub(1)
# $sub points to the context for the outer() sub.
}