in reply to Re: Perl dbi disconnect question...
in thread Perl dbi disconnect question...

I need a little clarification below:
my $dbh = DBI->connect( 'DBI:driver:database:host', 'user', 'pass', {RaiseError => 1, HandleError => \&bail_out}, );
With the settings above, does it mean that I don't need a 'or' to catch a failed query. See code below:
my $dbh = DBI->connect( 'DBI:driver:database:host', 'user', 'pass', {RaiseError => 1, HandleError => \&bail_out}, ); my $sql = qq{ SELECT COUNT(*) FROM profiles WHERE username=? }; # Original #my $result = $sth->execute($username) # or bail_out('Error executing SELECT'); # Modified # Note that 'or' clause is removed my $result = $sth->execute($username); $dbh->disconnect;
Relatedly, do I need to explicitly call disconnect within bail_out with your settings (i.e. RaiseError => 1 and HandleError => \&bail_out)?

That is:

sub bail_out { my $dbh = shift; $dbh->disconnect or bail_out(msg => "Cannot disconnect from the database"); # prints html message exit(0); }
Thanks for your great help :)

Replies are listed 'Best First'.
3Re: Perl dbi disconnect question...
by jeffa (Bishop) on Jan 05, 2004 at 15:55 UTC

    That's right, bail_out will called for you. As for disconnect ... i don't explicitly call it unless i absolutely have to. As noted other replies to this question, you really don't have to. You definetly do not want to call bail_out itside of itself. As always, refer to the manual when in doubt: (just search for HandleError)

    use Carp; ... sub bail_out { my $message = shift; confess ($message); }
    Should do the trick for now ... oh, don't worry about passing the message argument when you call bail_out, because you don't even have to call bail_out (but you still can if you need to). Also ... do you really need handle the errors? Have you tried letting DBI do it for you? Just specify RaiseError without any HandleError subroutine ...

    And you are always welcome. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)