File Coverage

blib/lib/Error/Tiny.pm
Criterion Covered Total %
statement 41 41 100.0
branch 13 14 92.8
condition 2 3 66.6
subroutine 10 10 100.0
pod 0 3 0.0
total 66 71 92.9


line stmt bran cond sub pod time code
1             package Error::Tiny;
2              
3 1     1   24624 use strict;
  1         2  
  1         28  
4 1     1   5 use warnings;
  1         2  
  1         26  
5              
6 1     1   5 use vars qw(@ISA @EXPORT @EXPORT_OK);
  1         6  
  1         89  
7              
8             our $VERSION = '0.03';
9              
10             BEGIN {
11 1     1   4 require Exporter;
12 1         60 @ISA = qw(Exporter);
13             }
14              
15             @EXPORT = @EXPORT_OK = qw(try then catch);
16              
17             require Carp;
18             $Carp::Internal{+__PACKAGE__}++;
19              
20             require Scalar::Util;
21              
22 1     1   673 use Error::Tiny::Exception;
  1         2  
  1         21  
23 1     1   418 use Error::Tiny::Catch;
  1         2  
  1         20  
24 1     1   401 use Error::Tiny::Then;
  1         2  
  1         292  
25              
26             sub try(&;@) {
27 17     17 0 379 my ($try, @handlers) = @_;
28              
29 17         28 my $wantarray = wantarray;
30              
31 17         21 my @ret;
32 17 100       29 eval { @ret = $wantarray ? $try->() : scalar $try->(); 1 } || do {
  17 100       56  
  4         25  
33 13         724 my $e = $@;
34 13         22 my $orig_e = $e;
35              
36 13 100       62 if (!Scalar::Util::blessed($e)) {
37 9         81 $orig_e =~ s{ at ([\S]+) line (\d+)\.\s*$}{}ms;
38 9         59 $e = Error::Tiny::Exception->new(
39             message => $orig_e,
40             file => $1,
41             line => $2
42             );
43             }
44              
45 13         93 for my $handler (@handlers) {
46 15 100 66     127 if ($handler && $handler->isa('Error::Tiny::Catch')) {
47 14 100       44 if ($e->isa($handler->class)) {
48 12         34 return $handler->handler->($e);
49             }
50             }
51             }
52              
53 1         341 Carp::croak($orig_e);
54             };
55              
56 4 100       19 return $wantarray ? @ret : $ret[0];
57             }
58              
59             sub catch(&;@) {
60 14 50   14 0 32127 my ($class, $handler) =
61             @_ == 2 ? ($_[0], $_[1]->handler) : ('Error::Tiny::Exception', $_[0]);
62              
63 14         77 Error::Tiny::Catch->new(handler => $handler, class => $class);
64             }
65              
66             sub then(&;@) {
67 4     4 0 6069 my ($handler, $subhandler) = @_;
68              
69 4         31 (Error::Tiny::Then->new(handler => $handler), $subhandler);
70             }
71              
72             1;
73             __END__