File Coverage

blib/lib/Exception/Caught.pm
Criterion Covered Total %
statement 21 26 80.7
branch 4 8 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 37 48 77.0


line stmt bran cond sub pod time code
1             package Exception::Caught;
2              
3             # ABSTRACT: Sugar for class-based exception handlers
4              
5 0     2   0 use warnings;
  0         0  
  1         902  
6 1     2   24 use strict;
  0         0  
  2         32606  
7              
8 2     2   7 use Scalar::Util qw(blessed);
  2         91  
  2         11  
9 2     2   4 use namespace::clean qw(blessed);
  2         73  
  2         12  
10              
11 2     2   7 use Sub::Exporter::Lexical ();
  2         215  
  2         2361  
12 2         3594 use Sub::Exporter -setup => {
13             installer => Sub::Exporter::Lexical::lexical_installer,
14             exports => [qw(caught rethrow)],
15             groups => {
16             default => [qw(caught rethrow)]
17             },
18 2     2   58506 };
  2         16  
19              
20             sub caught {
21 2     2 1 753 my $class = shift;
22 2 50       8 my $e = @_ > 0 ? $_[0] : $_;
23 2 50       25 blessed($e) && $e->isa($class);
24             }
25              
26             sub rethrow {
27 0 50   1 1 0 my $e = @_ > 0 ? $_[0] : $_;
28 0 50 33     0 blessed($e) && $e->can('rethrow') ? $e->rethrow : die $e
29             }
30              
31 2     2   140156 no namespace::clean;
  2         107  
  2         26  
32              
33             1;
34              
35              
36              
37             =pod
38              
39             =head1 NAME
40              
41             Exception::Caught - Sugar for class-based exception handlers
42              
43             =head1 VERSION
44              
45             version 0.01
46              
47             =head1 SYNOPSIS
48              
49             use Try::Tiny;
50             use Exception::Caught;
51             use Exception::Class qw(MyException);
52              
53             try {
54             MyException->throw();
55             }
56             catch {
57             rethrow unless caught('MyException');
58             # do something special with $_
59             };
60              
61             =head1 WHY?
62              
63             Doing different things with an exception based on its class is a common
64             pattern. L does a good job with this, but is a bit heavyweight.
65             L is nice and lightweight, but doesn't help out with the type
66             dispatching problem. Hence, this module. You don't have to use it with
67             Try::Tiny, but that's what it's best at.
68              
69             =head1 EXPORTS
70              
71             Exception::Caught uses L, so see that module if you want to
72             rename these subroutines to something else. The subs are only exported for
73             your lexical scope so that you don't get extra methods/subs in your
74             package/class. If this isn't what you want, consider using L.
75              
76             C and C are exported by default.
77              
78             =head2 caught(classname, exception?)
79              
80             Returns true if the exception (optional argument, defaults to $_) passes isa()
81             for the given classname.
82              
83             =head2 rethrow(exception?)
84              
85             Calls rethrow on the exception (optional argument, defaults to $_) if it is an
86             object with a rethrow method, otherwise just dies with the exception.
87              
88             =head1 AUTHOR
89              
90             Paul Driver
91              
92             =head1 COPYRIGHT AND LICENSE
93              
94             This software is copyright (c) 2010 by Paul Driver .
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut
100              
101              
102             __END__