#! /usr/bin/perl use strict; use warnings; use 5.010.000; my $data = { foo => [ { bar => 123 }, ], }; # keep this declaration outside of the eval-block my $val = "not defined"; # this with error eval { $val = $data->{foo}->{bar}; }; warn "$@\n" if $@; # check say "I'm alive and val is: $val"; # this hopefully without error eval { $val = $data->{foo}->[0]->{bar}; }; warn "$@\n" if $@; # check again say "I'm still alive and val is: $val";