use File::Find; my $dir; find( sub { $dir = $File::Find::name if ( /hello\.txt/ ); }, '/tmp/test' ); #### use File::Find; my $dir; if (!eval { find( sub { die "match: $File::Find::name\n" if ( /hello\.txt/ ); }, '/tmp/test' ); 1 }) { my $e = $@; die $e if !( ($dir) = /^match: (.*)/ ); }