in reply to pass code block to function (PROTOTYPES)

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

Replies are listed 'Best First'.
Re^2: pass code block to function (PROTOTYPES)
by Corion (Patriarch) on Jul 15, 2009 at 14:34 UTC

    Preferrably as patches (for example via git format-patch), as mail to perl5-porters@perl.org, or via the perlbug utility, which will send the mail for you. perlrepository (alternatively http://perl5.git.perl.org/perl.git/blob/HEAD:/pod/perlrepository.pod) has more information on how to set up a git mirror of bleadperl and how to format a patch but the basic workflow is:

    1. git clone http://perl5.git.perl.org/perl.git bleadperl
    2. Modify the document you want to modify, in your case, perlsub in pod/perlsub.pod
    3. Commit your change(s) by using git commit -a -m "Make perlsub please LanX" or, better, a more descriptive name.
    4. Create a patch file by using git format-patch origin - this should leave you with one (or more) files 0001-make-perlsub-please-lanx.patch, 0002-...
    5. Send these to perl5-porters@perl.org, preferrably with a second description of your rationale behind creating the patch, and what the patch changes.