sub check_foo { if (exists $foo{bar}) { return $foo{bar}; }else{ die "CODE[$the_mysterious_errorcode]\n"; } } # instead of my $value = check_foo(); my $value = eval { check_foo() }; ### see if there was an error if( $@ ){ if( $@ =~ /CODE\[(.+)\]/ ){ my $code = $1; } } ### if no problem with eval, $value is set to $foo{bar}