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

#Somehow, we figure out the name of a module we need: $module_name = 'mymodule.pm'; #Then load it: eval 'require $module_name'; #And then try to make use of its vars and subs: $module_name->import(); #fails (of course)
$module_name isn't an object, so import does not apply to it. How to I get a handle on the relevant object upon which to call import? require just returns bool.

Nobody seems to do this; it's not in the camel, cookbook, here, or anywhere on the Internet (the whole Internet!), so perhaps this is the wrong approach. What I want is to load a module who's name I do not know until we get rolling, and indeed can never be a bare word, as is usual practice. Then have access to the goodies in the loaded file, which is normally achieved with import(). Any ideas?
Sincerely,
Aleksander

Replies are listed 'Best First'.
Re: import() when require-d module name is variable
by chromatic (Archbishop) on Jul 18, 2009 at 17:54 UTC

    The package name is not mymodule.pm, so of course the import() call fails. Convert the file path to a package name:

    (my $package_name = $file_path) =~ s{/}{::}g; $package_name =~ s/\.pm//;

    Then you can call import() on it.

Re: import() when require-d module name is variable
by shmem (Chancellor) on Jul 18, 2009 at 21:12 UTC
    eval 'require $module_name';

    Nope. Between single quotes, variables are not interpolated. You would have to say

    eval qq{require '$module_name'};

    But then, to import() - as chromatic pointed out - you have to strip the .pm suffix.

Re: import() when require-d module name is variable
by f00li5h (Chaplain) on Jul 18, 2009 at 22:15 UTC

    eval qq{ use $module_name }; die "can't $module_name because: $@";

    Then perl does all the "finding it" magic ... and you still get to decide the module name at run time...

    You could also loop over many alternate modules and ignore failures until you find one you want, but for that you might want Module::Find or similar too ...

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Re: import() when require-d module name is variable
by CountZero (Bishop) on Jul 19, 2009 at 07:12 UTC
    (...) on the Internet (the whole Internet!)

    Then CPAN is not on the Internet?

    require UNIVERSAL::require; my $module_name = 'Regexp::Common'; $module_name->use(qw /comment number/); my $test = '123.456'; print $test =~ $RE{num}{real} ? 'A number' : 'Something else';
    UNIVERSAL::require does all you want.

    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