|There's more than one way to do things|
Turning a script into a moduleby eyepopslikeamosquito (Canon)
|on Jul 17, 2003 at 05:02 UTC||Need Help??|
eyepopslikeamosquito has asked for the
wisdom of the Perl Monks concerning the following question:
At work, we have a number of long-running scripts. It has been suggested we turn them into modules.
These long-running scripts tend to have a similar structure: they write a running commentary of what they are doing to stdout (via print); they write anything fishy to stderr (via warn); and they die if they encounter a nasty error.
I am eager to learn good ways, general guidelines even, re using print, warn and die inside Perl modules. Error handling, in particular, seems a tricky area of module design. General guidelines and/or examples of well-designed CPAN modules I can study are most welcome.
For the sake of definiteness, suppose we want to call the module in such a way that it never dies and so that all output emitted by the module is written to a log file and to STDOUT simultaneously. I suppose we could try something like this:
Here we have a PrintHandler attribute (use print by default, allow user to override) and an errstr method to return the last error encountered by the module. We could further try this:
Here we added new WarnHandler and ErrMode attributes, where ErrMode is modelled after Net::Telnet's errmode attribute.