Class:...Handler(3pm)

πŸ“¦
version 0.04 Β· 7 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Class:...Handler(3pm) πŸ’–

okay bestie, buckle up, we're reading a Perl manpage together and honestly? it's giving ✨foundational✨. this one lives in the User Contributed Perl Documentation corner of your man pages, and it is absolutely not boring, i promise. 🫢


🌸 NAME 🌸

Class::ErrorHandler β€” the base class for error handling, no cap. it's the blueprint other classes inherit from so they can go "hey, something went wrong" in a polite, structured way. πŸ’…


✨ SYNOPSIS ✨

okay so here's the whole vibe in code. literally copy this energy:

package Foo;
use base qw( Class::ErrorHandler );

sub class_method {
    my $class = shift;
    ...
    return $class->error("Help!")
        unless $continue;
}

sub object_method {
    my $obj = shift;
    ...
    return $obj->error("I am no more")
        unless $continue;
}

package main;
use Foo;

Foo->class_method or die Foo->errstr;

my $foo = Foo->new;
$foo->object_method or die $foo->errstr;

see how Foo inherits from it and then just... uses error like it's nothing? main character energy fr fr. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so Class::ErrorHandler gives you an error-handling mechanism that's generic enough to be the base class for literally a whole variety of OO classes. it's the friend that works with everybody. 🌈

subclasses inherit its two error-handling methods β€” error and errstr β€” and those are how they communicate error messages back to the calling program. that's it. that's the whole relationship. πŸ’ž

here's the important part, so pay attention bestie:

when something fails (for whatever reason), a subclass should call error and then return to the caller. and here's the little magic trick β€” error itself sets the error message internally, and then it returns "undef". which means the method that failed ends up returning "undef" to its caller. cute, right? 😌

so the caller should check for errors by checking for a return value of "undef", and then call errstr to get the value of the error message when an error has happened. that's the pattern. that's the whole dance. πŸ’ƒ

and as demonstrated up in the SYNOPSIS (go look, i'll wait πŸ“), error and errstr work as both class methods and object methods. flexible queen.


πŸŽ€ USAGE πŸŽ€

πŸ’… Class->error($message)

πŸ’… $object->error($message)

sets the error message for either the class Class or the object $object to the message $message. and then? returns "undef". that's the whole job. ✨


πŸ’œ Class->errstr

πŸ’œ $object->errstr

accesses the last error message set in the class Class or the object $object, respectively, and returns that error message. it's basically the receipts. 🧾


πŸ‘‘ LICENSE πŸ‘‘

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, legally speaking. πŸ’–


except where otherwise noted, Class::ErrorHandler is Copyright 2004 Benjamin Trott, cpan@stupidfool.org. All rights reserved.


okay that's the whole page bestie, you're officially an error-handling expert πŸ₯³

and for the cute sign-off: this lil beauty came from perl v5.36.0, dated 2023-01-22, and it's filed under Class:...Handler(3pm) in your friendly neighbourhood manual. go forth and handle your errors, icon. πŸ¦‹πŸ’œπŸŽ€

πŸ“¦ where this page comes from