File Coverage

blib/lib/App/Info/Handler/Carp.pm
Criterion Covered Total %
statement 17 18 94.4
branch 2 4 50.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 26 29 89.6


line stmt bran cond sub pod time code
1             package App::Info::Handler::Carp;
2              
3             =head1 NAME
4              
5             App::Info::Handler::Carp - Use Carp to handle App::Info events
6              
7             =head1 SYNOPSIS
8              
9             use App::Info::Category::FooApp;
10             use App::Info::Handler::Carp;
11              
12             my $carp = App::Info::Handler::Carp->new('carp');
13             my $app = App::Info::Category::FooApp->new( on_info => $carp );
14              
15             # Or...
16             my $app = App::Info::Category::FooApp->new( on_error => 'croak' );
17              
18             =head1 DESCRIPTION
19              
20             App::Info::Handler::Carp objects handle App::Info events by passing their
21             messages to Carp functions. This means that if you want errors to croak or
22             info messages to carp, you can easily do that. You'll find, however, that
23             App::Info::Handler::Carp is most effective for info and error events; unknown
24             and prompt events are better handled by event handlers that know how to prompt
25             users for data. See L
26             for an example of that functionality.
27              
28             Upon loading, App::Info::Handler::Carp registers itself with
29             App::Info::Handler, setting up a number of strings that can be passed to an
30             App::Info concrete subclass constructor. These strings are shortcuts that
31             tell App::Info how to create the proper App::Info::Handler::Carp object
32             for handling events. The registered strings are:
33              
34             =over
35              
36             =item carp
37              
38             Passes the event message to C.
39              
40             =item warn
41              
42             An alias for "carp".
43              
44             =item croak
45              
46             Passes the event message to C.
47              
48             =item die
49              
50             An alias for "croak".
51              
52             =item cluck
53              
54             Passes the event message to C.
55              
56             =item confess
57              
58             Passes the event message to C.
59              
60             =back
61              
62             =cut
63              
64 1     1   2520 use strict;
  1         3  
  1         188  
65 1     1   7 use App::Info::Handler;
  1         2  
  1         28  
66 1     1   6 use vars qw($VERSION @ISA);
  1         3  
  1         778  
67             $VERSION = '0.57';
68             @ISA = qw(App::Info::Handler);
69              
70             my %levels = ( croak => sub { goto &Carp::croak },
71             carp => sub { goto &Carp::carp },
72             cluck => sub { goto &Carp::cluck },
73             confess => sub { goto &Carp::confess }
74             );
75              
76             # A couple of aliases.
77             $levels{die} = $levels{croak};
78             $levels{warn} = $levels{carp};
79              
80             # Register ourselves.
81             for my $c (qw(croak carp cluck confess die warn)) {
82             App::Info::Handler->register_handler
83             ($c => sub { __PACKAGE__->new( level => $c ) } );
84             }
85              
86             =head1 INTERFACE
87              
88             =head2 Constructor
89              
90             =head3 new
91              
92             my $carp_handler = App::Info::Handler::Carp->new;
93             $carp_handler = App::Info::Handler::Carp->new( level => 'carp' );
94             my $croak_handler = App::Info::Handler::Carp->new( level => 'croak' );
95              
96             Constructs a new App::Info::Handler::Carp object and returns it. It can take a
97             single parameterized argument, C, which can be any one of the following
98             values:
99              
100             =over
101              
102             =item carp
103              
104             Constructs a App::Info::Handler::Carp object that passes the event message to
105             C.
106              
107             =item warn
108              
109             An alias for "carp".
110              
111             =item croak
112              
113             Constructs a App::Info::Handler::Carp object that passes the event message to
114             C.
115              
116             =item die
117              
118             An alias for "croak".
119              
120             =item cluck
121              
122             Constructs a App::Info::Handler::Carp object that passes the event message to
123             C.
124              
125             =item confess
126              
127             Constructs a App::Info::Handler::Carp object that passes the event message to
128             C.
129              
130             =back
131              
132             If the C parameter is not passed, C will default to creating an
133             App::Info::Handler::Carp object that passes App::Info event messages to
134             C.
135              
136             =cut
137              
138             sub new {
139 6     6 1 10 my $pkg = shift;
140 6         54 my $self = $pkg->SUPER::new(@_);
141 6 50       23 if ($self->{level}) {
142 6 50       19 Carp::croak("Invalid error handler '$self->{level}'")
143             unless $levels{$self->{level}};
144             } else {
145 0         0 $self->{level} = 'carp';
146             }
147 6         35 return $self;
148             }
149              
150             sub handler {
151 6     6 1 8 my ($self, $req) = @_;
152             # Change package to App::Info to trick Carp into issuing the stack trace
153             # from the proper context of the caller.
154             package App::Info;
155 6         16 $levels{$self->{level}}->($req->message);
156             # Return true to indicate that we've handled the request.
157 3         257 return 1;
158             }
159              
160             1;
161             __END__