in reply to How do I return false and set an error in special variable $!

RMGir explained the problem. Here's a solution:

for(qw(1 4 haha)){ eval { usenumbers($_) } or die($@); } # Returns true on success. # Throws an exception on error. sub usenumbers { my $var = shift; $var=~/^\d+$/ or die('Invalid argument: Expecting digits'); print "Yes, [$var] has numbers."; return 1; }

A variation of the above where the function doesn't need to return true:

for(qw(1 4 haha)){ eval { usenumbers($_) }; die($@) if $@; } # Throws an exception on error. sub usenumbers { my $var = shift; $var=~/^\d+$/ or die('Invalid argument: Expecting digits'); print "Yes, [$var] has numbers."; }