Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: [RFC] Review of module code and POD

by bliako (Monsignor)
on Apr 01, 2021 at 11:50 UTC ( [id://11130673]=note: print w/replies, xml ) Need Help??


in reply to [RFC] Review of module code and POD

*I* *usually* have an init() method called automatically in new() to do any initialisation, e.g. connect and read DB. This has the advantage of being able to re-initialise any time I like after instantiation by just calling $obj->init().

I am still undecided (or haven't read enough) on how to propagate errors, error messages and error codes to caller all the way up. The method you are using by saving the *last* error message will need some tweaking when you are nesting sub calls (and if you do that it will soon result to a paper-pushing system). Perhaps simply append to $error with obligatory clearing it when starting a-fresh. Alternatively throw an exception which is the "modern" thing to do but that imposes a whole new style which I don't see often in Perl modules (arbitrary judgement), see e.g. Exception::Class. Sometimes I return a complex data structure with error code, error message, data etc. but I only do that for complex subs.

I appreciate Perl more when I have to work with Java and its super-strict typing.:

sub work { my $something_wrong = 1; if( $something_wrong ){ #return Error->new(0, "because ..."); return bless [0, "because ..."] => 'Error' } # edit: changed to a hashref to show returning different types # it need not be a ref at all, e.g. can send a hash too #return [1,2,3] return {1=>2, 3=>4} } my $ret = work(); if( ref($ret) eq 'Error' ){ die "error: @$ret" } print "got some results:".Dumper($ret);

Replies are listed 'Best First'.
Re^2: [RFC] Review of module code and POD
by Bod (Parson) on Apr 02, 2021 at 21:17 UTC
    I am still undecided (or haven't read enough) on how to propagate errors, error messages and error codes to caller all the way up

    As you can tell from my code - I am also undecided!
    This was something that I hoped people would comment on and I wasn't disappointed.

    I am fairly sure that I don't need to go as far as implementing full blown Exceptions, especially for modules that will only ever be used internally. This is the first time I have tried setting an 'error' field within the module to provide a verbose message and it seems to work quite well. Having said that, I would like to adopt more consistency both in this module and for other modules going forward.

    I do like the idea of an init(); method although I'm not sure of a situation where I would want to reconnect to the database within the same session.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found