1Class::ErrorHandler(3)User Contributed Perl DocumentationClass::ErrorHandler(3)
2
3
4
6 Class::ErrorHandler - Base class for error handling
7
9 package Foo;
10 use base qw( Class::ErrorHandler );
11
12 sub class_method {
13 my $class = shift;
14 ...
15 return $class->error("Help!")
16 unless $continue;
17 }
18
19 sub object_method {
20 my $obj = shift;
21 ...
22 return $obj->error("I am no more")
23 unless $continue;
24 }
25
26 package main;
27 use Foo;
28
29 Foo->class_method or die Foo->errstr;
30
31 my $foo = Foo->new;
32 $foo->object_method or die $foo->errstr;
33
35 Class::ErrorHandler provides an error-handling mechanism that's generic
36 enough to be used as the base class for a variety of OO classes.
37 Subclasses inherit its two error-handling methods, error and errstr, to
38 communicate error messages back to the calling program.
39
40 On failure (for whatever reason), a subclass should call error and
41 return to the caller; error itself sets the error message internally,
42 then returns "undef". This has the effect of the method that failed
43 returning "undef" to the caller. The caller should check for errors by
44 checking for a return value of "undef", and calling errstr to get the
45 value of the error message on an error.
46
47 As demonstrated in the SYNOPSIS, error and errstr work as both class
48 methods and object methods.
49
51 Class->error($message)
52 $object->error($message)
53 Sets the error message for either the class Class or the object $object
54 to the message $message. Returns "undef".
55
56 Class->errstr
57 $object->errstr
58 Accesses the last error message set in the class Class or the object
59 $object, respectively, and returns that error message.
60
62 This library is free software; you can redistribute it and/or modify it
63 under the same terms as Perl itself.
64
66 Except where otherwise noted, Class::ErrorHandler is Copyright 2004
67 Benjamin Trott, cpan@stupidfool.org. All rights reserved.
68
69
70
71perl v5.32.0 2020-07-28 Class::ErrorHandler(3)