home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / ErrorHandler.pm < prev    next >
Encoding:
Perl POD Document  |  2001-08-06  |  2.5 KB  |  98 lines

  1. # $Id: ErrorHandler.pm,v 1.1 2001/08/07 00:29:02 btrott Exp $
  2.  
  3. package Convert::PEM::ErrorHandler;
  4. use strict;
  5.  
  6. use vars qw( $ERROR );
  7.  
  8. sub new    { bless {}, shift }
  9. sub error  {
  10.     my $msg = $_[1];
  11.     $msg .= "\n" unless $msg =~ /\n$/;
  12.     if (ref($_[0])) {
  13.         $_[0]->{_errstr} = $msg;
  14.     } else {
  15.         $ERROR = $msg;
  16.     }
  17.     return;
  18.  }
  19. sub errstr { ref($_[0]) ? $_[0]->{_errstr} : $ERROR }
  20.  
  21. 1;
  22. __END__
  23.  
  24. =head1 NAME
  25.  
  26. Convert::PEM::ErrorHandler - Convert::PEM error handling
  27.  
  28. =head1 SYNOPSIS
  29.  
  30.     package Foo;
  31.     use Convert::PEM::ErrorHandler;
  32.     use base qw( Convert::PEM::ErrorHandler );
  33.  
  34.     sub class_method {
  35.         my $class = shift;
  36.         ...
  37.         return $class->error("Help!")
  38.             unless $continue;
  39.     }
  40.  
  41.     sub object_method {
  42.         my $obj = shift;
  43.         ...
  44.         return $obj->error("I am no more")
  45.             unless $continue;
  46.     }
  47.  
  48.     package main;
  49.     use Foo;
  50.  
  51.     Foo->class_method or die Foo->errstr;
  52.  
  53.     my $foo = Foo->new;
  54.     $foo->object_method or die $foo->errstr;
  55.  
  56. =head1 DESCRIPTION
  57.  
  58. I<Convert::PEM::ErrorHandler> provides an error-handling mechanism
  59. for all I<Convert::PEM> modules/classes. It is meant to be used as
  60. a base class for classes that wish to use its error-handling methods:
  61. derived classes use its two methods, I<error> and I<errstr>, to
  62. communicate error messages back to the calling program.
  63.  
  64. On failure (for whatever reason), a subclass should call I<error>
  65. and return to the caller; I<error> itself sets the error message
  66. internally, then returns C<undef>. This has the effect of the method
  67. that failed returning C<undef> to the caller. The caller should
  68. check for errors by checking for a return value of C<undef>, and
  69. in this case should call I<errstr> to get the value of the error
  70. message. Note that calling I<errstr> when an error has not occurred
  71. is undefined behavior and will I<rarely> do what you want.
  72.  
  73. As demonstrated in the I<SYNOPSIS> (above), I<error> and I<errstr> work
  74. both as class methods and as 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 AUTHOR & COPYRIGHTS
  93.  
  94. Please see the Convert::PEM manpage for author, copyright, and
  95. license information.
  96.  
  97. =cut
  98.