1N/A# exceptions.pl
1N/A# tchrist@convex.com
1N/A#
1N/A# This library is no longer being maintained, and is included for backward
1N/A# compatibility with Perl 4 programs which may require it.
1N/A#
1N/A# In particular, this should not be used as an example of modern Perl
1N/A# programming techniques.
1N/A#
1N/A#
1N/A# Here's a little code I use for exception handling. It's really just
1N/A# glorfied eval/die. The way to use use it is when you might otherwise
1N/A# exit, use &throw to raise an exception. The first enclosing &catch
1N/A# handler looks at the exception and decides whether it can catch this kind
1N/A# (catch takes a list of regexps to catch), and if so, it returns the one it
1N/A# caught. If it *can't* catch it, then it will reraise the exception
1N/A# for someone else to possibly see, or to die otherwise.
1N/A#
1N/A# I use oddly named variables in order to make darn sure I don't conflict
1N/A# with my caller. I also hide in my own package, and eval the code in his.
1N/A#
1N/A# The EXCEPTION: prefix is so you can tell whether it's a user-raised
1N/A# exception or a perl-raised one (eval error).
1N/A#
1N/A# --tom
1N/A#
1N/A# examples:
1N/A# if (&catch('/$user_input/', 'regexp', 'syntax error') {
1N/A# warn "oops try again";
1N/A# redo;
1N/A# }
1N/A#
1N/A# if ($error = &catch('&subroutine()')) { # catches anything
1N/A#
1N/A# &throw('bad input') if /^$/;
1N/A
1N/Asub catch {
1N/A package exception;
1N/A local($__code__, @__exceptions__) = @_;
1N/A local($__package__) = caller;
1N/A local($__exception__);
1N/A
1N/A eval "package $__package__; $__code__";
1N/A if ($__exception__ = &'thrown) {
1N/A for (@__exceptions__) {
1N/A return $__exception__ if /$__exception__/;
1N/A }
1N/A &'throw($__exception__);
1N/A }
1N/A}
1N/A
1N/Asub throw {
1N/A local($exception) = @_;
1N/A die "EXCEPTION: $exception\n";
1N/A}
1N/A
1N/Asub thrown {
1N/A $@ =~ /^(EXCEPTION: )+(.+)/ && $2;
1N/A}
1N/A
1N/A1;