Prototypes have no effect on whether a subroutine returns a scalar or a list. Use wantarray
if you want to know in what context a subroutine was called.
eval &$func;
The line above first calls the subroutine referred to by $func
, in scalar context, then converts its return value into a string and executes that string as a Perl program.
sub f (&) {
my ($func) = @_;
eval { &$func }
}
This will do what you apparently want (call the subroutine referenced by $func
, in the same context (scalar or list) in which f
itself was called, within an eval
block to catch runtime errors).
sub f (&) {
my ($func) = @_;
eval { $func->(@_) }
}
This is more modern style, with the ->
operator.