http://www.perlmonks.org?node_id=757077

jacques has asked for the wisdom of the Perl Monks concerning the following question:

I would like to import a function like so:
use MyModule qw(MyFunction {parameter => 'HelloWord'});
So that the function is imported and then called with a parameter set in the use statement. Is this possible?

Replies are listed 'Best First'.
Re: How can I import a function and set a parameter at the same time?
by ELISHEVA (Prior) on Apr 12, 2009 at 08:22 UTC

    Not usually. use XXX qw(a b c) is short for BEGIN { require XXX; XXX->import('a','b','c'); } Most modules that allow importing inherit from Exporter and use the default definition provided by that module. Exporter::import(...) simply imports the symbols "a", "b", "c" into the current package's namespace. It does not have any provision to for calling the functions after importing them. For more information, see use, import and Exporter.

    If you want to call a function after importing it, you have two options. If the module you want to "use" is your own module, you could give that module a custom import(...) function.

    For third party modules, you can't change the import function, but you can use a begin block to add additional behavior. Instead of use XXX, you would write something like this:

    BEGIN { require XXX; XXX->import('MyFunction'); MyFunction('Hello World'); }

    In either case your code will run in a BEGIN block, during the compilation phase. For more information on BEGIN blocks, see perlmod.

    Best, beth

Re: How can I import a function and set a parameter at the same time?
by ikegami (Patriarch) on Apr 12, 2009 at 08:35 UTC
    Why won't the following satisfy your needs?
    use MyModule qw( MyFunction ); MyFunction('HelloWord');

    Let us know that, and we'll help you solve your actual problem.

      Maybe the function must be run immediately upon importing, before anything else runs, already during the compilation phase?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        In that possible but odd case, the following would do:
        use MyModule qw( MyFunction ); BEGIN { MyFunction('HelloWord'); } # Execute as soon as compiled.
        You are on the right track. I want it to work like fatalsToBrowser which also captures compilation errors. Unfortunately, that subroutine doesn't accept arguments (you are not suppose to call it yourself), but I want the same convenience to turn it on or off with a single comment. For example:
        # On use CGI::Carp qw(fatalsToBrowser); # Off # use CGI::Carp qw(fatalsToBrowser);
        I think the solution lies in creating my own custom import sub. I realized this right after posting. Thanks everyone for the thoughts!!
Re: How can I import a function and set a parameter at the same time?
by shmem (Chancellor) on Apr 12, 2009 at 12:15 UTC

    For successfully using your notation, the import() routine of the imported module must be aware about and know what to do with the additionally supplied hashref, so, no per default.

    My question is, what for? If the function is to be called once after importing, use a BEGIN block after the use MyModule to do so. If MyModule is your module, modify the import() routine so as to call the function immediately with the parameter passed in the hashref, if it's present.

    But if you want to set a default parameter for that imported function for every call, you could say:

    use MyModule qw(MyFunction); BEGIN { my $subref = \&MyFunction; # already imported *{__PACKAGE__.'::MyFunction'} = sub { $subref->('HelloWord', @_) } +; }