4 # This library is no longer being maintained, and is included for backward
5 # compatibility with Perl 4 programs which may require it.
6 # This legacy library is deprecated and will be removed in a future
9 # In particular, this should not be used as an example of modern Perl
10 # programming techniques.
12 warn( "The 'exceptions.pl' legacy library is deprecated and will be"
13 . " removed in the next major release of perl." );
15 # Here's a little code I use for exception handling. It's really just
16 # glorfied eval/die. The way to use use it is when you might otherwise
17 # exit, use &throw to raise an exception. The first enclosing &catch
18 # handler looks at the exception and decides whether it can catch this kind
19 # (catch takes a list of regexps to catch), and if so, it returns the one it
20 # caught. If it *can't* catch it, then it will reraise the exception
21 # for someone else to possibly see, or to die otherwise.
23 # I use oddly named variables in order to make darn sure I don't conflict
24 # with my caller. I also hide in my own package, and eval the code in his.
26 # The EXCEPTION: prefix is so you can tell whether it's a user-raised
27 # exception or a perl-raised one (eval error).
32 # if (&catch('/$user_input/', 'regexp', 'syntax error') {
33 # warn "oops try again";
37 # if ($error = &catch('&subroutine()')) { # catches anything
39 # &throw('bad input') if /^$/;
43 local($__code__, @__exceptions__) = @_;
44 local($__package__) = caller;
45 local($__exception__);
47 eval "package $__package__; $__code__";
48 if ($__exception__ = &'thrown) {
49 for (@__exceptions__) {
50 return $__exception__ if /$__exception__/;
52 &'throw($__exception__);
57 local($exception) = @_;
58 die "EXCEPTION: $exception\n";
62 $@ =~ /^(EXCEPTION: )+(.+)/ && $2;