in reply to detecting errors when a sub returns undef

Something that is quick and light and clean...
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}


All this is, is try and catch (but it works and is fairly clean, and it makes sure if somebody calls your sub, they have to know what to do with the return).

my @a=qw(random brilliant braindead); print $a[rand(@a)];