File Coverage

blib/lib/Continuation/Escape.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 37 38 97.3


line stmt bran cond sub pod time code
1             package Continuation::Escape;
2 8     8   195314 use strict;
  8         21  
  8         283  
3 8     8   40 use warnings;
  8         14  
  8         204  
4 8     8   102 use 5.8.0;
  8         27  
  8         331  
5 8     8   49 use base 'Exporter';
  8         13  
  8         1577  
6             our @EXPORT = 'call_cc';
7             our $VERSION = '0.03';
8              
9 8     8   7821 use Scope::Upper qw/unwind HERE/;
  8         9211  
  8         1969  
10              
11             # This registry is just so we can make sure that the user is NOT trying to save
12             # and run continuations later.
13             # Sorry if the name got you excited. :/
14             our %CONTINUATION_REGISTRY;
15              
16             sub call_cc (&) {
17 17     17 0 2950 my $code = shift;
18              
19 17         120 my $escape_level = HERE;
20 17         35 my $wantarray = wantarray;
21              
22 17         25 my $escape_continuation;
23             $escape_continuation = sub {
24 10 100   10   146 if (!exists($CONTINUATION_REGISTRY{$escape_continuation})) {
25 2         16 require Carp;
26 2         44 Carp::croak("Escape continuations are not usable outside of their original scope.");
27             }
28              
29 8 100       106 unwind(($wantarray ? @_ : $_[0]) => $escape_level);
30 17         77 };
31              
32 17         68 local $CONTINUATION_REGISTRY{$escape_continuation} = $escape_continuation;
33 17         54 return $code->($escape_continuation);
34             }
35              
36             1;
37              
38             __END__