sub function1 { my $bSuccess=1; # flag in case $e is undef even though we died my $e; # for saving and returning $@ when $@ isn't wiped out my $db; # declare outside of eval since you want to return this eval { $db = DBI->connect("DBI:mysql:database=$sql_database;host=$ +sql_host;port=$sql_port", $username, $password,{'RaiseError' => 1}); return 1; #guarantee true return if no death } or do { # we get here if _and only if_ we die # save fragile $@ - prevent further opportunities to wipe out $e=$@; # flag failure so we know we died even if $@ is undef $bSuccess=0; }; return $bSuccess ? $db : $e; #### sub callDBI { my ($aParams, $crOnError) = @_; my $db; eval { $db = DBI->connect(@$aParams,{'RaiseError' => 1}); return 1; } or do { my $e=$@; if (defined($crOnError)) { $db = $crOnError->($aParams, $e); } else { $@=$e; die; # rethrow } }; return $db; }