Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I am writing a module to support an internal project, and I've come to the point where I need to design the error system. In general, when module subroutines encounter an error, they must return the error string (which is actually specified outside my control) so that the calling code can decide what action to take for that specific error.

The error strings are modelled after network protocol communication, i.e. they begin with a number, a space, and then contain the error text '220 Cannot connect to service'. The programming model I generally use is "Return true on success, False on failure". Obviously (as I confirmed in a CB session with tye,theorbtwo and others) I cannot return error strings that are logically false, but I'd like to preserve the illusion. ;-)

Imagine code looking somewhat like this:

unless ( $object->method('param') ) { ## somehow get $errstr my ($errcode) = split ' ', $errstr; &$erract{$errcode}($errstr); } ## %erract contains such things as ('220' => sub { die $_ });

My debate comes in the 'somehow get $errstr' area. I could do what DBI does, and set $Module::errstr, but I've always personally disliked that approach, and the development section rules here strongly eschew globals in "includes" (It's not a Perl shop, so I interpret that to be "modules" for my purposes).

Ideally, the above chunk of code would leave the error string in $_, so that this would work:

unless ( $object->method('param') ) { my ($errcode) = split ' ', $_; &$erract{$errcode}($_); }

The closest way I can think of to implement that is to use die to throw and catch exceptions, like:

eval { $object->method('param') }; if ($@) { my ($errcode) = split ' ', $@; &$erract{$errcode}($@); }

But, I think I may have screwed myself out of being able to do that -- I use exceptions for internally fatal errors, and I am loathe to call any of the spec errors "fatal".

Is there another way to use this general approach to dealing with errors? I'm looking to end up with calling code that is clean and understandable, even for Perl neophytes; but, of course, it's most important that it works well.

Thanks in advance for any ideas, links, reading resources, etc. related to this.

Update {

Thanks to dragonchild and monkey_boy, I have solved the problem by extending Class::Base, which roughly implements dragonchild's suggestion. Thank you to all the monks who helped me reason this one out!

Anima Legato
.oO all things connect through the motion of the mind

In reply to Handling non-fatal method error strings without exceptions or globals? by legato

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [talexb]: Nice article on Perl by Ovid on TechBeacon: https://techbeacon .com/perl-not-dead -it-was-early-web- novices-gave-it- bad-name

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2017-09-19 17:59 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (226 votes). Check out past polls.