Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Keep It Simple, Stupid
 
PerlMonks  

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


Comment on Re: Recommendation for dynamically "using" modules
Select or Download Code
Re^2: Recommendation for dynamically "using" modules
by McA (Deacon) 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
    McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043648]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-04-23 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (555 votes), past polls