[Chicago-talk] A question of 'die'ing

Steven Lembark lembark at wrkhors.com
Tue Jan 30 07:34:28 PST 2007

> What are your thoughts on a library that will die if it is given
> invalid input?  Would use such a library if it required you to put
> eval blocks around it each time?

Exceptions are a reasonable model. The catch is that if
someone uses the thing without realizing it dies then
they can accidentally kill code above it. In that case
you have to eval just about everything with checks for
$@. This gets worse if you use blessed exceptions since
evary eval has to check for ref $@ and bracnch accordingly.

Fatal is one good example: it saves you quite a bit of time
but unless your caller knows you use it they might not be
ready for the exception. That leaves every call to your
code in an eval with sanity checks on $@. After a while it
gets annoying.

One way out of this is Sub::ForceEval, which allows you to
enforce eval's without having to write the eval's and if
$@ logic on each block:

  use Sub::ForceEval;

  sub foo :ForceEval

will guarantee that foo is eval-ed and if there isn't an
eval on the call stack above it then you get a nastygram
and undef back to the caller.

You can pass a function/method to the use or sub lines to
pass $@ to a munger/log/constrct handler. This guarantees
that anything escaping the code that uses your module gets
an appropriate type of exception.

Steven Lembark                                         85-09 90th Street
Workhorse Computing                                  Woodhaven, NY 11421
lembark at wrkhors.com                                      +1 888 359 3508

More information about the Chicago-talk mailing list