in reply to Code references as use() parameters
That works fine. You can take a reference to a subroutine by name before the subroutine exists. Perl does some interesting tricks to allow this. The problem you will run into is if you want to dereference it while still inside of import().
I can think of several ways around this. The very straight-forward:
or you could make use of the undocumented fact that require actually returns the final "true" value in the module. So write your module like this:require Alzabo::MethodMaker; Alzabo::MethodMaker->import( name_maker => \@name_maker );
and use it like this:package Alzabo::MethodMaker; sub import { ... } \&import;
or you could require the subroutine to be declared in-line:(require Alzabo::MethodMaker)->( name_maker => \&name_maker, );
You could use the poorly documented INIT as broquaint demonstrated. INIT is so poorly documented that I'm not sure of the consequences of this approach but the demonstration makes me worry that the INIT will be run too late if you need methods created before the main script has finished compiling. Certainly some will say this is very unlikely, but then, some would say that the order-of-definition problem that you are running into is very unlikely.use Alzabo::MethodMaker( name_maker => sub { ... }, );
That might be the most appealing solution because it hides so much from the module user so be sure to carefully document when the modules actually get "made" if you go this route.
You could export a routine that makes the methods:
You could document that Alzabo::MethodMaker should appear last in the source code rather than the more traditional first (I can certainly see potential value for a MethodMaker module being able to see what other methods have already been defined).use Alzabo::MethodMaker qw( makeMethods ); makeMethods( name_maker => \&name_maker, );
In any case, good luck.
- tye
|
|---|