Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Recommendation for dynamically "using" modules

by tobyink (Abbot)
on Jul 11, 2013 at 08:14 UTC ( #1043648=note: print w/replies, xml ) Need Help??

in reply to Recommendation for dynamically "using" modules

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

Replies are listed 'Best First'.
Re^2: Recommendation for dynamically "using" modules
by McA (Priest) on Jul 11, 2013 at 08:34 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043648]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-03-20 06:33 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (248 votes). Check out past polls.