# excuse the sloppy code... sub log_and_die { my $msg = shift; # suppose $fh is an already defined, opened # file handle. of course, you can open one here # as well... print $fh $msg; die $msg; } DBI->connect( ... ) || log_and_die( $! )