Commit | Line | Data |
99b89507 |
1 | # exceptions.pl |
2 | # tchrist@convex.com |
3 | # |
4 | # Here's a little code I use for exception handling. It's really just |
5 | # glorfied eval/die. The way to use use it is when you might otherwise |
6 | # exit, use &throw to raise an exception. The first enclosing &catch |
7 | # handler looks at the exception and decides whether it can catch this kind |
8 | # (catch takes a list of regexps to catch), and if so, it returns the one it |
9 | # caught. If it *can't* catch it, then it will reraise the exception |
10 | # for someone else to possibly see, or to die otherwise. |
11 | # |
12 | # I use oddly named variables in order to make darn sure I don't conflict |
13 | # with my caller. I also hide in my own package, and eval the code in his. |
14 | # |
15 | # The EXCEPTION: prefix is so you can tell whether it's a user-raised |
16 | # exception or a perl-raised one (eval error). |
17 | # |
18 | # --tom |
19 | # |
20 | # examples: |
21 | # if (&catch('/$user_input/', 'regexp', 'syntax error') { |
22 | # warn "oops try again"; |
23 | # redo; |
24 | # } |
25 | # |
26 | # if ($error = &catch('&subroutine()')) { # catches anything |
27 | # |
28 | # &throw('bad input') if /^$/; |
29 | |
30 | sub catch { |
31 | package exception; |
32 | local($__code__, @__exceptions__) = @_; |
33 | local($__package__) = caller; |
34 | local($__exception__); |
35 | |
36 | eval "package $__package__; $__code__"; |
37 | if ($__exception__ = &'thrown) { |
38 | for (@__exceptions__) { |
39 | return $__exception__ if /$__exception__/; |
40 | } |
41 | &'throw($__exception__); |
42 | } |
43 | } |
44 | |
45 | sub throw { |
46 | local($exception) = @_; |
47 | die "EXCEPTION: $exception\n"; |
48 | } |
49 | |
50 | sub thrown { |
51 | $@ =~ /^(EXCEPTION: )+(.+)/ && $2; |
52 | } |
53 | |
54 | 1; |