sub try (&$) {
my($try, $catch) = @_;
eval { $try };
if ($@) {
local $_ = $@;
&$catch;
}
}
sub catch (&) { $_[0] }
####
try {
die "phooey";
}
catch {
/phooey/ and print "unphhoey\n";
}
##
##
sub identity {
return $_[0];
}
##
##
sub example {
/foo/ and print "foo detected";
}
catch \&example;
catch sub {
/foo/ and print "foo detected";
};
catch {
/foo/ and print "foo detected";
}
##
##
try {
# some code that dies
}
catch {
# pattern match against $_ for die string
}
##
##
try( sub{
# code that dies
},
catch(sub {
# code that checks vs. $_
}
)
);