in reply to Aren't there code refs as well as function refs?

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

Replies are listed 'Best First'.
Re^2: Aren't there code refs as well as function refs?
by dd-b (Pilgrim) on Mar 04, 2023 at 03:53 UTC

    Prototypes kept going! I kind of went off them a long time ago, they seemed to not be where things were going. Also--I notice they don't affect indirect and other common kinds of calls; this would look hugely likely to cause immense confusion!

    I can of course dig into the places where I wonder what's going on. That code tends to be over my head, though, so it's a deep dive.

    Perl is always an adventure!

      I avoid prototypes in my code, except for the empty prototype '()' to create "Constant Functions".

      In v5.36.0, the experimental status of subroutine Signatures was removed. You may find this to be a better method to declare parameters. Do note that prototypes and signatures are not different syntaxes for the same thing: follow the link for details. This is something I do like and have been using in all of my personal code since Perl v5.36.0 was released.

      Not that I'm recommending it but, just so you know, you can use both a prototype and a signature with the same subroutine. If you do this, use of the :prototype() attribute is advised to avoid ambiguity.

      — Ken

        I avoid prototypes in my code, except for the empty

        Likewise...

        When I first came across prototypes in Perl they sounded extremely useful. However, I have come to realise that they are never really necessary at all - or if they are, I have not come across the use case. The complications they add to understanding and maintaining the code is not worth the tradeoff.