Why does a (&) prototype only accept blocks or sub{} but not scalar coderefs?
It does. In fact, that's the only thing it accepts. The real question is why doesn't it accept arbitrary expressions that return a code ref. And the answer is that there's no way to guarantee that the expression will return a code ref. Rather than adding code to the optree to do run-time type checking, prototypes only allow operators that are guaranteed to return a value of the appropriate type.
For example,
The arg for (\@) must start with a @.
The arg for (\%) must start with a %.
sub {} is guaranteed to return a code ref, so it's allowed for (&).
\&cb is guaranteed to return a code ref, so it's allowed for (&). By extension, this includes \&{ EXPR }.
&foo, on the other hand, is a sub call. It can return anything. It's not allowed for (&) (or (\@) or (\%)).
In reply to Re: coderefs and (&) prototypes
by ikegami
in thread coderefs and (&) prototypes
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |