Let's say I have a subroutine that takes a sub as argument.
sub foobar {
my $block = shift;
$block->();
}
Then I can call it like so
foobar(sub { print "Hello :)\n" });
I can also use block syntax if I declare it with an appropriate prototype.
sub foobar(&) {
my $block = shift;
$block->();
}
foobar { print "Hello :)\n" };
Now, suppose that my function doesn't actually exist and is an AUTOLOAD, such as
sub AUTOLOAD {
$AUTOLOAD =~ s/.*:://;
if ($AUTOLOAD eq "foobar") {
my $block = shift;
$block->();
} else {
die("No such function $AUTOLOAD");
}
}
We can still call it with an explicit sub
foobar(sub { print "Hello :)\n" });
And we can eliminate unnecessary parentheses by predeclaring the subroutine.
use subs 'foobar';
foobar sub { print "Hello :)\n" };
But I can't get the block syntax back if my function doesn't actually exist and have a prototype. I'd like to be able to do this
foobar { print "Hello :)\n" };
Assuming I know the name foobar in advance and can predeclare things as needed (as I did with use subs 'foobar'), is there any way I can convince Perl that foobar takes a block argument, despite only having access to foobar through an AUTOLOAD?
Note: This is not production code and never will be. I'm asking out of curiosity and to potentially use tricks like this in programming puzzles / challenges, so readability and maintainability is not a concern.