# my test code catch { print "unphooey again\n"; } ;
Sorry, this doesn't make sense! Your completely misunderstanding the aim of catch(), it's basically syntactic sugar, the logic is in try().
You can also write
for absolutely the same functionality, but writing "catch" instead of "sub" makes the use of try() clearer.try {... some code to try ... } sub { ... some code to catch }
sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { local $_ = $@; &$catch; } } sub catch (&) { $_[0] } try { die "phooey"; } sub { /phooey/ and print "unphooey\n"; };
UPDATE: the use of sub instead of catch is even better, since it allows a a prototype sub try (&&) which catches syntax errors at compile-time. (ATM the second argument to try() isn't necessarily a coderef!)
An & requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma.(IMHO to avoid misparsing of literal hashrefs)
so passing a code reference as a second argument requires an explicit "sub" or just something mimicking a sub like this "catch()" function.
sub try (&$) would be much better for avoiding false uses and for better intrinsic documentation.
sub try (&$) { my($try,$catch) = @_; eval { &$try }; if ($@) { local $_ = $@; &$catch; } } sub catch (&) { $_[0] } try { die "phooey"; } catch { /phooey/ and print "unphooey\n"; };
Where can I report improvements to perldoc?
Cheers Rolf
In reply to Re: pass code block to function (PROTOTYPES)
by LanX
in thread pass code block to function (PROTOTYPES)
by jeanluca
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |