I have a requirement in my current project for a simple yet flexible logging facility. While die/croak are fine for exception handling, logging uncaught exceptions to stderr is often too inflexible -- you may want to write fatal errors to an arbitrary file handle/s and/or send an email, for example.

I'm thinking of rolling my own very simple logging/tracing/carping facility (I'm in good company judging by the very many Log and Carp modules on the CPAN ;-). Here's some sample code to show where I'm coming from.

package MyDie; my $die_handle = \*STDOUT; sub mydie { my $message = shift; # If in eval block, throw exception ... $^S and die $message; # ... otherwise print message to handle and exit. print {$die_handle} $message, "exiting now\n"; exit 1; } 1;
and a test driver:
use MyDie; sub testfn { print "in testfn\n"; MyDie::mydie("whoops\n"); print "end testfn\n"; } eval { testfn() }; $@ and print "caught: '$@'\n"; testfn(); print "end test program\n";

Generally, I prefer functions to throw exceptions rather than return special values. The idea here is to simply call die (throw) if called inside an eval block, otherwise to log the fatal error message and exit.

Since there are many ways to skin this particular cat, I'm interested to hear people's ideas and opinions before charging ahead with this.


In reply to Rolling your own die/carp/croak/logging/exception facility by eyepopslikeamosquito

Title:
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, details, 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, summary, 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.