in reply to Re: coderefs and (&) prototypes
in thread coderefs and (&) prototypes

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.
and then
\&cb is guaranteed to return a code ref, so it's allowed for (&). By extension, this includes \&{ EXPR }.

I don't understand these two together. There's no way to guarantee that EXPR in \&{ EXPR } will evaluate to a code-ref either, and, if it doesn't, the extra \& noise isn't going to prevent it from blowing up at run-time—so why put the programmer through that extra syntactic hassle, if it doesn't buy him or her any extra safety?

Replies are listed 'Best First'.
Re^3: coderefs and (&) prototypes
by ikegami (Patriarch) on Jul 28, 2009 at 21:23 UTC

    the extra \& noise isn't going to prevent it from blowing up at run-time

    The whole point of parameter validation is to blow up. Why would you want to prevent it?

    Without the "extra noise", it wouldn't blow up. That's the problem.

    sub f0 { my ($cb) = @_; '...' } sub f1(&) { my ($cb) = @_; '...' } f0(\&abc); # OK No error f0(\&{ maybe_coderef() }); # OK Error caught at run-time (if any) f0("abc"); # XX Error uncaught f0(maybe_coderef()); # XX Error uncaught (if any) f1(\&abc); # OK No error f1(\&{ maybe_coderef() }); # OK Error caught at run-time (if any) f1("abc"); # OK Error caught at compile-time f1(maybe_coderef()); # OK Error caught at compile-time (if any)

    Update: Added example