The print statment in the catch subroutine definition at line #10 is confusing you (and probably everybody else).
The catch subroutine is only called once, and its only purpose is to return the sub ref passed to it, which is then passed as an argument to try in @_. An anonymous
subroutine composer could be used in place of the call to
catch for the same effect:
try {
...
} sub {
print "FAILED!\n";
}
The subroutine reference passed to try in @_
is evaluated at line #4. The eval block catches the
exception caused by the die statement in line #13 and
sets $@ to the exception string, "foobar\n" (it's worth
noting that if the argument to die includes a newline,
line number information will not be included in the
exception string).
Line #5 checks for an eval exception and if true,
localizes $_ and sets it to the value of the eval
exception, then calls the anonymous subroutine
returned by catch, which just happens to
be stored in a lexical variable named "$catch". Since
the anonymous subroutine is called from the same scope
as the localized $_, that copy of $_ is visible to
the anonymous subroutine, not the global $_.
This is where you've missed the purpose of this code:
the anonymous subroutine returned by catch
should check the value in $_ for the specific
exception set by the anonymous subroutine executed
by eval, then take the appropriate action if
the "tried" code failed.
Think of "try/catch" as "try something, and if it fails,
recover gracefully (if possible)".
Now that you've read this and hopefully understand the
code a little better, go back and read the example code
in the perlsub manpage "Prototypes" section. It should
make a lot more sense now.
1 sub try (&@) {
2 print "try();\n";
3 my($try,$catch) = @_;
4 eval { &$try };
5 if ($@) {
6 local $_ = $@;
7 &$catch;
8 }
9 }
10 sub catch (&) { print "catch();\n"; $_[0] }
11
12 try {
13 die "foobar\n";
14 print "OK";
15
16 } catch {
17 print "FAILED!\n";
18 };
Lines 16-18 should probably be written:
16 } catch {
17 /^foobar$/ and print "FAILED!\n";
18 };
conv
|