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 | |
by kcott (Archbishop) on Mar 04, 2023 at 07:26 UTC | |
by Bod (Parson) on Mar 04, 2023 at 12:07 UTC |