The misunderstanding
# 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

try {... some code to try ... } sub { ... some code to catch }
for absolutely the same functionality, but writing "catch" instead of "sub" makes the use of try() clearer.
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!)

Some background
The deeper underlying "problem" (or asymmetry) in perl is that prototypes distinguish code blocks according to the position in argument list:

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.

Bad perldoc
What I really don't like about this perldoc example is that the prototype of try sub try (&@) is misleading, since it accepts an unlimited list of arguments!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.