| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# $Id: ErrorHandler.pm,v 1.1.1.1 2004/08/15 14:55:43 btrott Exp $ |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Class::ErrorHandler; |
|
4
|
2
|
|
|
2
|
|
11506
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
94
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
10
|
use vars qw( $VERSION $ERROR ); |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
361
|
|
|
7
|
|
|
|
|
|
|
$VERSION = '0.03'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub error { |
|
10
|
4
|
|
50
|
4
|
1
|
535
|
my $msg = $_[1] || ''; |
|
11
|
4
|
100
|
|
|
|
12
|
if (ref($_[0])) { |
|
12
|
2
|
|
|
|
|
5
|
$_[0]->{_errstr} = $msg; |
|
13
|
|
|
|
|
|
|
} else { |
|
14
|
2
|
|
|
|
|
4
|
$ERROR = $msg; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
4
|
|
|
|
|
10
|
return; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub errstr { |
|
20
|
4
|
100
|
|
4
|
1
|
878
|
ref($_[0]) ? $_[0]->{_errstr} : $ERROR |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
1; |
|
24
|
|
|
|
|
|
|
__END__ |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=for stopwords errstr |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 NAME |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Class::ErrorHandler - Base class for error handling |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
package Foo; |
|
35
|
|
|
|
|
|
|
use base qw( Class::ErrorHandler ); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub class_method { |
|
38
|
|
|
|
|
|
|
my $class = shift; |
|
39
|
|
|
|
|
|
|
... |
|
40
|
|
|
|
|
|
|
return $class->error("Help!") |
|
41
|
|
|
|
|
|
|
unless $continue; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub object_method { |
|
45
|
|
|
|
|
|
|
my $obj = shift; |
|
46
|
|
|
|
|
|
|
... |
|
47
|
|
|
|
|
|
|
return $obj->error("I am no more") |
|
48
|
|
|
|
|
|
|
unless $continue; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
package main; |
|
52
|
|
|
|
|
|
|
use Foo; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Foo->class_method or die Foo->errstr; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $foo = Foo->new; |
|
57
|
|
|
|
|
|
|
$foo->object_method or die $foo->errstr; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
I<Class::ErrorHandler> provides an error-handling mechanism that's generic |
|
62
|
|
|
|
|
|
|
enough to be used as the base class for a variety of OO classes. Subclasses |
|
63
|
|
|
|
|
|
|
inherit its two error-handling methods, I<error> and I<errstr>, to |
|
64
|
|
|
|
|
|
|
communicate error messages back to the calling program. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
On failure (for whatever reason), a subclass should call I<error> and return |
|
67
|
|
|
|
|
|
|
to the caller; I<error> itself sets the error message internally, then |
|
68
|
|
|
|
|
|
|
returns C<undef>. This has the effect of the method that failed returning |
|
69
|
|
|
|
|
|
|
C<undef> to the caller. The caller should check for errors by checking for a |
|
70
|
|
|
|
|
|
|
return value of C<undef>, and calling I<errstr> to get the value of the |
|
71
|
|
|
|
|
|
|
error message on an error. |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
As demonstrated in the L<SYNOPSIS>, I<error> and I<errstr> work as both class |
|
74
|
|
|
|
|
|
|
methods and object methods. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 USAGE |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 Class->error($message) |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 $object->error($message) |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Sets the error message for either the class I<Class> or the object |
|
83
|
|
|
|
|
|
|
I<$object> to the message I<$message>. Returns C<undef>. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 Class->errstr |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 $object->errstr |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Accesses the last error message set in the class I<Class> or the |
|
90
|
|
|
|
|
|
|
object I<$object>, respectively, and returns that error message. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 LICENSE |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 AUTHOR & COPYRIGHT |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Except where otherwise noted, I<Class::ErrorHandler> is Copyright 2004 |
|
99
|
|
|
|
|
|
|
Benjamin Trott, cpan@stupidfool.org. All rights reserved. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |