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


In reply to Re: Sub prototypes... by converter
in thread Sub prototypes... by vladb

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.