Audrey Tang has the implemented the initial working functionality in a powerful new Perl 6 feature, calling sets of methods. Here's how we'll be using it in CGI::Application for Perl 6.
In Perl 5, we used 65 lines of code in a "call_hook" function that took care of running all the plugged-in code at a particular point in the execution process. It would look through up through the inheritance try and execute perhaps multiple bits of plugging functionality at each point. The is one of the only places in CGI::Application where we used an an external module to help, making use of Class::ISA to determine the correct inheritance tree.
In Perl 6, we can replace all that busy work with a one line.
That line looks like this:
$self.*WALK[:ascendant]::$hook_meth([,] =$args);
I say "like" because the WALK part isn't implemented yet, nor is calling the set of multi methods that have the same name. This much works right now, which calls a the set methods named $hook_meth, throughout the inheritence tree:
$self.*$hook_meth([,] =$args);
Here's a more complete snippet:
method call_hook ($self: $hook,\$args?) {
die "Unknown hook ($hook)" if not %!installed_callbacks.exists($hook);
# Hooks must be methods with the name suffix "_hook"
my $hook_meth = $hook~'_hook';
$self.*$hook_meth([,] =$args);
}
As you might guess the syntax related to "$args" is way to pass an arbitrary set of arguments through a wrapper, as I've done here.
Although I've usually been writing about things I can do with Pugs now, I thought this unfinished feature was exciting enough to go ahead and mention. Besides, if I blog about it, it might get implemented faster...
Comments