in reply to Perl dbi disconnect question...

Destruction of the connection handle will disconnect it, so you don't need the explicit disconnect. It might be a good idea to call rollback ( and to call commit before disconnect in the success branch). Your db may not have full transaction support, but the calls will do no harm and ease portability.

Because &bail_out represents a failure, it would be better to exit with a non-zero value.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Perl dbi disconnect question...
by kiat (Vicar) on Jan 05, 2004 at 01:00 UTC
    Thanks, Zaxo!

    I'm using bail_out to print a host of other messages, some for a successful executing of a query or process. Should I then have exit(0) or exit(1)?

      I think you should refactor so that you handle errors with one functions and success with another. Reserve exit(0) for leaving with your utility having done everything your user should expect. You may want to give exit different values to represent differeent errors. 0+$! is a good value if the reason is an error that sets $!. Or you could call die $!, instead.

      After Compline,
      Zaxo

        Ah, I see. So I should have a different bail out sub when an error occurs during a query execution. Will the following do?

        sub bail_out2 { my $message = shift # output error in html # exit with 1 because it's a failure exit(1); }
        Does exiting with the value of 1 automatically kills the database connection?

        I need to print out the error in html to let the user know that something has gone wrong with the processing of the script. die prints the error to the log which is invisible to the user. Am I right?