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;