G'day dd-b,

I think one of the main issues you may be having here is terminology. At the end of the second paragraph of "perlsub: DESCRIPTION" (my emphasis):

"Often a function without an explicit return statement is called a subroutine, but there's really no difference from Perl's perspective."

You're making a distinction between "function refs" and "code refs": they're really just the same thing.

$ perl -E ' use Scalar::Util "reftype"; sub routine { 1; } my $x = \&routine; say $x; say ref $x; say reftype $x; say sub { 1; }; ' CODE(0xa0004a3c0) CODE CODE CODE(0xa0007e370)

In your follow-up post, you made reference to this type of syntax (which I've abstracted):

function { code } args

Before reading on, you may want to review "perlsub: Prototypes" (noting what the '&' means in a prototype) and prototype(). Also be aware that subroutines/functions do not necessarily have a prototype.

You'll find that syntax in a number of builtin functions; e.g. grep, map & sort. There's a plethora of examples in the builtin module List::Util. There are also many examples in CPAN modules; such as Test::Exception which you've identified. Here's some example prototypes:

$ perl -E 'use List::Util; say prototype "List::Util::reduce"' &@ $ perl -E 'use Test::Exception; say prototype "Test::Exception::throws +_ok"' &$;$

There are two other places where you'll often see '{ ... }': anonymous blocks (see ++hippo's reply) and hashref constructors (with which I'll assume you're familiar).

— Ken


In reply to Re: Aren't there code refs as well as function refs? by kcott
in thread Aren't there code refs as well as function refs? by dd-b

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.