Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
PerlMonks  

Re: Tricks with DBI

by coolmichael (Deacon)
on Mar 11, 2001 at 03:58 UTC ( #63549=note: print w/ replies, xml ) Need Help??


in reply to Tricks with DBI

Besides, you can always catch the die in an eval; just make sure that you handle the errors rather than ignoring them.
I haven't seen anything like this before. Could you post an example? Thanks.

Michael
who just became a monk


Comment on Re: Tricks with DBI
Re (tilly) 2: Tricks with DBI
by tilly (Archbishop) on Mar 12, 2001 at 08:43 UTC
    This is documented in eval. Here is how it works:
    eval {call_function_that_may_die()}; if ($@) { # This is your error case print "Caught a die saying '$@'\n"; }
      Instead of using die, you can unset $dbh->{RaiseError} locally and use the other C-like error check.

      Ths is usefull when you have a SQL-statement that can fail, but where you don't want to die because of the failure. (I have used it when dropping temporary tables, that perhaps aren't there and other suchlike tings).

      The code looks like this:

      my $dbh=DBI->connect(....{RaiseError=>1}) or die... my $sth=$dbh->prepare(...); { local $dbh->{RaiseError} = 0; $sth->execute; if ($sth->Errstr) { # handle the error } } # $dbh->{RaiseError} is back to normal here
      The neat thing about setting $dbh->{RaiseError} with local is that it is automagically set back to whatever it was when you leave the block, however that is done - even if it is by way of a die, that is caught in an eval somewhere else.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-04-23 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls