Stevens said:
Hi,
I'm looking for some advice on programming style ... is is considered bad
practice to die within your own functions as opposed to using a bare return
statement? What about displaying error messages within perl functions, is
this frowned (as it tends to be in unix systems programming) upon?
Unconditionally dying in library routines is bad style anywhere. The only
place for it is bug catching (that is, dying under conditions that cannot
happen when the program is correct).
Now one could argue that Perl has eval(), and hence dying is never truly
unconditional, but that solution should be reserved for cases where a
function *does* die inappropriately. If the user has to undo things a
routine has done, the routine is doing (and assuming) too much, that goes
for dying as for other services.
The pragmatic "warnings" module offers a solution in that it allows the
user to control the behavior (ignore, warn, die). However, I hesitate
to recommend it unconditionally. The control it gives is somewhat
flakey, and a warning can consume considerable time even if it is
turned off. Both problems have to do with the fact that "warnings"
uses "Carp" to do the error attribution.
However, the spirit of "warnings" is the way it should be. It's the
end users who ought to control behavior under error conditions (with
reasonable defaults, so they don't *have* to). Only they know what the
consequences are if something goes wrong.
Anno