Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Sub prototypes...

by converter (Priest)
on Dec 22, 2001 at 04:21 UTC ( #133914=note: print w/replies, xml ) Need Help??

in reply to Sub prototypes...

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 };


Replies are listed 'Best First'.
Re: Re: Sub prototypes...
by vladb (Vicar) on Dec 22, 2001 at 12:15 UTC
    converter your reply helped me understand this to every tiny detail, numerous thanks for your effort my fellow monk! ;-)

    I didn't expect someone to put up so much effort and explain an answer to my question so thoroughly.

    I feel a little dumb now though.. that I failed to understand the concept heh. Actually, it was my first try at subroutine prototypes.

    Say, I figure the code could be rewritten as follows:
    try({die "foobar\n"; print "OK";12}, catch {print "FAILED!\n";} );
    *laugh* I guess my struggle for Java-like OO-ish exception handling hit back and confused me beyound disbelief. lol ;-)

    cheers, vladb

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://133914]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-01-22 23:11 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.