Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Forgetfulness and 6-months-from-now-you

by oakbox (Chaplain)
on Jan 12, 2018 at 10:51 UTC ( [id://1207148]=perlmeditation: print w/replies, xml ) Need Help??

Can anyone explain to me WHY I must constantly forget and relearn that:
my $sth = $dbh->prepare("SELECT * FROM Something"); $sth->execute(); if($sth->err()){ die $sth->errstr(); }
is functionally equivalent to
my $sth = $dbh->prepare("SELECT * FROM Something"); $sth->execute(); if($dbh->err()){ die $dbh->errstr(); }
???

Because every couple of years I will become frustrated with typing out the error check on the various statement handles.
And then look into how I can make that easier.
And RE-LEARN that I could have been checking the database handle and just copy/pasting that over and over.

So frustrating, a real smack-to-the-forehead kind of moment.

And I think it ties in with why I write code the way I do. I WRITE IT OUT. I take the time to format and indent and line up the code because I have learned 6-months-in-the-future-me will really appreciate it and I don't want him cussing at me, retroactively.

Just needed to vent a little bit, please return to your regularly scheduled activities.

Replies are listed 'Best First'.
Re: Forgetfulness and 6-months-from-now-you
by hippo (Bishop) on Jan 12, 2018 at 11:29 UTC

    Now don't take this the wrong way, but did you know that you can set RaiseError and PrintError when you initiate the connection? That way you don't need to check for (and die on) errors every time because DBI will do it for you. It might just save you a lot of typing.

      I don't take it the wrong way :)

      Most of my work is generating content for an API, which wants specifically formatted error handling (which I conveniently left out of my post). I want to die in a clean, and parse-able, way so that the programmer on the other end can either a) figure out what they did wrong or b) communicate with me effectively about what happened. I also use a lot of Mark Overmeer's excellent Log::Report module to make sane logs for internal analysis.

      I COULD override die, I guess, and generate something that way. But my laziness doesn't extend that far.

        I COULD override die, I guess, and generate something that way. But my laziness doesn't extend that far.

        I would probably be that lazy:

        my $dbh=DBI->connect(..., {RaiseError => 1}); # later: if (eval { # use $dbh # perhaps the entire API handling 1; }) { # report success to the API } else { my $error=$@; # Post-Process $error if needed # report $error to the API }

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found