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:

require Alzabo::MethodMaker; Alzabo::MethodMaker->import( name_maker => \@name_maker );
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:
package Alzabo::MethodMaker; sub import { ... } \&import;
and use it like this:
(require Alzabo::MethodMaker)->( name_maker => \&name_maker, );
or you could require the subroutine to be declared in-line:
use Alzabo::MethodMaker( name_maker => sub { ... }, );
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.

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:

use Alzabo::MethodMaker qw( makeMethods ); makeMethods( name_maker => \&name_maker, );
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).

In any case, good luck.

                - tye

In reply to Re: Code references as use() parameters (well) by tye
in thread Code references as use() parameters by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.