File Coverage

blib/lib/Crypt/OpenPGP/ErrorHandler.pm
Criterion Covered Total %
statement 13 14 92.8
branch 6 6 100.0
condition n/a
subroutine 4 5 80.0
pod 2 3 66.6
total 25 28 89.2


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::ErrorHandler;
2 12     12   53 use strict;
  12         17  
  12         397  
3              
4 12     12   54 use vars qw( $ERROR );
  12         18  
  12         2427  
5              
6 0     0 0 0 sub new { bless {}, shift }
7             sub error {
8 28     28 1 139 my $msg = $_[1];
9 28 100       144 $msg .= "\n" unless $msg =~ /\n$/;
10 28 100       82 if (ref($_[0])) {
11 25         66 $_[0]->{_errstr} = $msg;
12             } else {
13 3         7 $ERROR = $msg;
14             }
15 28         123 return;
16             }
17 5 100   5 1 540 sub errstr { ref($_[0]) ? $_[0]->{_errstr} : $ERROR }
18              
19             1;
20             __END__
21              
22             =head1 NAME
23              
24             Crypt::OpenPGP::ErrorHandler - Crypt::OpenPGP error handling
25              
26             =head1 SYNOPSIS
27              
28             package Foo;
29             use Crypt::OpenPGP::ErrorHandler;
30             use base qw( Crypt::OpenPGP::ErrorHandler );
31              
32             sub class_method {
33             my $class = shift;
34             # Stuff happens...
35             return $class->error("Help!");
36             }
37              
38             sub object_method {
39             my $obj = shift;
40             # Stuff happens...
41             return $obj->error("I am no more");
42             }
43              
44             package main;
45              
46             Foo->class_method or die Foo->errstr;
47              
48             my $foo = Foo->new;
49             $foo->object_method or die $foo->errstr;
50              
51             =head1 DESCRIPTION
52              
53             I<Crypt::OpenPGP::ErrorHandler> provides an error-handling mechanism
54             for all I<Crypt::OpenPGP> modules/classes. It is meant to be used as
55             a base class for classes that wish to use its error-handling methods:
56             derived classes use its two methods, I<error> and I<errstr>, to
57             communicate error messages back to the calling program.
58              
59             On failure (for whatever reason), a subclass should call I<error>
60             and return to the caller; I<error> itself sets the error message
61             internally, then returns C<undef>. This has the effect of the method
62             that failed returning C<undef> to the caller. The caller should
63             check for errors by checking for a return value of C<undef>, and
64             in this case should call I<errstr> to get the value of the error
65             message. Note that calling I<errstr> when an error has not occurred
66             is undefined behavior and will I<rarely> do what you want.
67              
68             As demonstrated in the I<SYNOPSIS> (above), I<error> and I<errstr> work
69             both as class methods and as object methods.
70              
71             =head1 USAGE
72              
73             =head2 Class->error($message)
74              
75             =head2 $object->error($message)
76              
77             Sets the error message for either the class I<Class> or the object
78             I<$object> to the message I<$message>. Returns C<undef>.
79              
80             =head2 Class->errstr
81              
82             =head2 $object->errstr
83              
84             Accesses the last error message set in the class I<Class> or the
85             object I<$object>, respectively, and returns that error message.
86              
87             =head1 AUTHOR & COPYRIGHTS
88              
89             Please see the Crypt::OpenPGP manpage for author, copyright, and
90             license information.
91              
92             =cut