in reply to coderefs and (&) prototypes
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 (\%)).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: coderefs and (&) prototypes
by JadeNB (Chaplain) on Jul 28, 2009 at 21:20 UTC | |
by ikegami (Patriarch) on Jul 28, 2009 at 21:23 UTC |