|Problems? Is your data what you think it is?|
Re: Sub prototypes...by converter (Priest)
|on Dec 22, 2001 at 04:21 UTC||Need Help??|
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:
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.
Lines 16-18 should probably be written: