Perl6 RFC Librarian writes:
> This RFC proposes two-stage autoloading: one stage may be registered
> to act when the symbol is encountered at compile time, the other
> when the subroutine is called. Autoloading on the second stage does not
> I<call> the subroutine, only I<loads> it.
You have a beautiful mind, Mr Zakharevich. I'd like to frame it on my
wall.
Actually, I think I'd like to see this extended. I'd like to be able
to hook into the parser so that when it sees a function call:
foo()
I can have it call my subroutine.
foo_handler( $token_stream )
foo_handler can access the token stream that may or may not be part of
the argument list, decide what is, and then return the code to be
executed at runtime (normally just a call to foo()):
sub foo_handler {
return sub { foo() }; # *not* an infinite loop, please
}
The anonymous subroutine returned by the foo_handler() is then inlined
into the optree.
This would let us have constants:
sub foo_handler {
return sub { 42 };
}
Context-coercion:
sub foo_handler {
my $token_stream = shift;
my $arg_1 = $token_stream->next; # assuming passed as strings?
return eval "sub { scalar($arg_1) }";
}
This would give us the ability to have things with both compile-time
and run-time behaviours, as well as being able to remove run-time
behaviours if we wanted.
Perhaps this is no longer the point at which we think of it as
autoloading, but rather as part of the compilation process. Just as
we can intercept strings and regexes with overloaded strings and
regexes, this would let us intercept functions.
Nat