Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

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

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:

sub tee_print { print MYLOGFILE $_[0]; print $_[0] } eval { local $SIG{__WARN__} = \&tee_print; my $h = MyModule->new(PrintHandler => \&tee_print); $h->method($params) or tee_print("oops: " . $h->errstr()); }; if ($@) { tee_print("error: $@") }

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:

my $h = MyModule->new(PrintHandler => \&tee_print, WarnHandler => \&tee_print, ErrMode => 'return'); $h->method($params) or tee_print("oops: " . $h->errstr());

Here we added new WarnHandler and ErrMode attributes, where ErrMode is modelled after Net::Telnet's errmode attribute.


In reply to Turning a script into a module by eyepopslikeamosquito

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2021-12-05 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?