my $res = eval { do_whatever() }; if ($@) { # handle error ... } # use $res here;