Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Recommendation for dynamically "using" modules

by McA (Priest)
on Jul 11, 2013 at 07:54 UTC ( #1043641=perlquestion: print w/replies, xml ) Need Help??
McA has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I would like to here recommendations how to implement the following kind of function:

myuse('Module::Mine', 'Module::Their');

which should require the modules like use does it and afterwards export the symbols to the caller of myuse. Are there ready to use modules out there? Hints, solutions appreciated.

Best regards

Replies are listed 'Best First'.
Re: Recommendation for dynamically "using" modules
by tobyink (Abbot) on Jul 11, 2013 at 08:14 UTC

    The problem is that myuse() would be executed at run-time instead of compile-time. In which case the symbols are probably less useful. If the symbols being exported are non-code (i.e. exported variables), then use strict will have already killed the script at compile-time because the variables were not declared. Ditto for any subs that are imported that need special parsing (e.g. you want to use them without parentheses, or they have interesting prototypes). Importing is something that really needs to be done at compile-time; hence use, or at a push BEGIN.

    What exactly are you hoping to achieve? If you want to combine many use Foo, use Bar, use Baz statements into a single statement, then look at Syntax::Collector and/or Import::Into. If you want to conditionally load modules, look at if. If you want to select amongst several backend modules implementing the same function, there's Module::Implementation. If you don't need to import symbols at all, and are loading modules that will e.g. be used as classes, then run-time loading is fine, and I'd recommend Module::Runtime, Class::Load, or (if you're in an experimental kind of mood) perhaps Module::Quote.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Hi tobyink,

      thank you for the hints. Especially Module::Implementation could be a way to think about my problem.

      Today it's done via the following:

      my $code = create_use_string(%parameters); eval "$code"; if(my $ex = $@) { die "Something went really wrong: $ex"; }
      while create_use_string is returning a perl code snippet of the kind
      use lib 'path_to_implementation_dependent_on_parameters'; use x; use y;
      You see, everything is exported. That's not nice, but it's the way the current (old) code base is relying on.

      Best regards

Re: Recommendation for dynamically "using" modules (export import into)
by Anonymous Monk on Jul 11, 2013 at 08:00 UTC
      Thanks, Anonymonk! I'm going to steal this list for my talk at YAPC.

      Thank you for that fast reply.

      I totally agree to your statement, especially to

      "[...] exporting is the devil [...]"
      , but probably it can help to do a transition in an existing code base. Sometimes you have to do changes step by step.

      I did find modules for the "loading" part, but not for the "exporting" to a level above the function doing the module load. Import::Into looks interesting.

      Best regards

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043641]
Approved by tobyink
[erix]: perhaps we can have common hindsight at these thing in a few years time
[Your Mother]: Please don't fight, mom and dad!

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-03-19 21:21 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.