Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

My tip which seems small but is actually big is use eval blocks for error checking.

Here is an example which happens to use DBI. DBI implements tons to error checking which uses die if you ask it to.

#up top my $dbh=DBI->connect("DBI:Pg(RaiseError=>1,AutoCommit=>0),dbname=xyzzy +"); #later eval { my($sth); $stmt="SELECT cnam_oficial FROM evocrs WHERE evocrs_id=?"; $sth=$dbh->prepare($stmt); $sth->execute($ec); $sth->bind_columns( \( $cn ) ); $sth->fetch; # $dbh->commit; #if we were writing some records instead of readin +g }; if($@) { # $dbh->rollback; #if we were doing transactions and writing data print STDERR "Could not retreive course name: $stmt: $@\n"; print "Could not retrieve course name\n"; exit; }

I delclare $stmt in a scope where the eval and the error block can both see it for reporting. The eval saves having to say or die "message" on every line of your code. The RaiseError tells DBI to die on any database error. I put the commit inside the eval as the last line because that will not be reached if there is any problem since a die will jump right out of the eval without doing the rest of the block. The rollback goes in the $@ block which is executed if the eval fails.

This makes your code nice and clean and results in errors being trapped always. The DBI module is very nice about this since RaiseError causes a die for all errors.


In reply to Re: best practice by dga
in thread best practice by George_Sherston

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 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?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found