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

Getting Module Names Dynamically and using it

by vishi83 (Pilgrim)
on Dec 07, 2005 at 10:22 UTC ( #514780=perlquestion: print w/replies, xml ) Need Help??

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

Hi Wise monks !!

I hav a problem in using Modules dynamically.. I have written a script which will fetch all the module names (*.pm) from a particular directory and its sub-dirs .
Now I hav all these names in an array and i need to call a method table_name defined in those modules.

Heres the pseudo-code form of my script .. step 1 : Getting all .pm filenames from a directory that satisfies a p +articular condition. ( in an array ) step 2 : Traverse through the array of filenames and use the module dy +namically

Something lik this ;

This is jus a snippet of my script; my @tables; foreach my $modulenames ( @avail ) { my $object = "Project::$modulenames"; use $object; my $objvalue = $object->table_name(); push (@tables, $objvalue); } print "@tables";

The Problem i hav now is I can't use my modules dynamically something like use $object ;

Thanks in advance !!!


A perl Script without 'strict' is like a House without Roof; Both are not Safe;

Replies are listed 'Best First'.
Re: Getting Module Names Dynamically and using it
by broquaint (Abbot) on Dec 07, 2005 at 10:36 UTC
    If you already have the filenames then just use them in conjunction with require e.g
    require $_ for @modules;
    Then you can execute the class methods like so:
    my @tables = map $_->table_name, @module_names;


      I don't think the latter part of that will work -- the OP said that the list is of filenames, e.g. "lib/Foo/Bar/", not package names, e.g. "Foo::Bar::Baz". And if the list were of package names, they would need to be converted to filenames to be used with require in the way you suggest, or instead be called in an eval:

      # if @modules contains package names, not filenames eval "require $_" for @modules;

      The OP might want to look at Module::Require or UNIVERSAL::Require and see if either would be applicable/helpful for this situation.


      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Getting Module Names Dynamically and using it
by sh1tn (Priest) on Dec 07, 2005 at 10:37 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://514780]
Approved by gube
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-10-27 18:57 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (257 votes). Check out past polls.