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

Hi, I have a doubt if this can be implemented . When we have a module say Employee.pm with subroutines say add,find,save,list when we use Employee.pm we can load all or specific functions using Export and Export_ok Similiarly instead of telling Perl interpretor with several statements of use will a single statement use all the modules with in a directory. Eg If i have 10 -15 modules in a directory instead of telling 10-15 use can a simplified command to tell perl interpretor load all the modules under this directory be implemented. Any clues on the above? Regards

Replies are listed 'Best First'.
Re: Loading all modules under a directory
by displeaser (Hermit) on May 30, 2005 at 12:16 UTC
    Hi,

    You can load if you create all the modules under the same namespace. You can use the all pragma (see all.

    An example.

    use strict;
    use warnings;

    use all Win32::;

    # This will load all the Win32 modules that you have installed onto your system.

    Hope this helps some.

    Displeaser.
Re: Loading all modules under a directory
by davido (Cardinal) on May 30, 2005 at 16:06 UTC

    use doesn't really allow you much in the way of trickery and flexibility. You cannot "use" modules conditionally, or dynamically create use lists.

    However, as is documented...

    It is exactly equivalent to BEGIN { require Module; import Module LIST; } except that Module must be a bareword.

    So here's another strategy for you. Within a BEGIN block, open the directory in which your modules reside, read it, keep all the *.pm filenames, and then in a foreach loop perform the above "require $_; import $_ LIST;" code.


    Dave

      $_->import Instead of import $_ please =]

      use doesn't really allow you much in the way of trickery and flexibility

      Depends on how you look at it. Subroutine references in @INC provides a fair bit of trickery and flexibility.

      ihb

      See perltoc if you don't know which perldoc to read!

Re: Loading all modules under a directory
by brian_d_foy (Abbot) on May 30, 2005 at 19:41 UTC

    There are several "plugin" modules on CPAN, so you can look at how they do things. :)

    Besides the other advice already given, if you have the list of file names of all the modules that you want to install, you can simply require() each file name. With a little more work you can turn that file name into a namespace and then call its import() routine. That's essentially what all does.

    --
    brian d foy <brian@stonehenge.com>
Re: Loading all modules under a directory
by jmanning2k (Pilgrim) on May 31, 2005 at 16:04 UTC
    Sounds like you're looking for Module::Pluggable. Theres a decent writeup at http://perladvent.org/2004/6th/. If you put all your modules in a MyPlugins directory you can do:
    use Module::Pluggable search_path => ['MyPlugins'] require => 1; ## The following is not tested... BEGIN { ## Do the Export and Export_OK into local namespace foreach my $plugin (__PACKAGE__->plugins()) { $plugin->import(); } }
    This might also work but again is untested:
    use Module::Pluggable search_path => ['MyPlugins'] instantiate => 'imp +ort';

    ~J
Re: Loading all modules under a directory
by salva (Canon) on May 30, 2005 at 18:36 UTC
    well, as you can see from previous post there are certainly some ways...

    But is it really a good idea to do so? maybe if you elaborate further on why you are trying to load all the modules under some directory we could give you better advice.

    Is it pure laziness? are you working on some extensible architecture where new classes can be introduced just dropping them in the rigth directory?, etc.

Re: Loading all modules under a directory
by dReKurCe (Scribe) on May 31, 2005 at 10:53 UTC
    Grettings You could use the following line of code:
    unshift @INC "/path/to/target/directory";
Re: Loading all modules under a directory
by bschmer (Friar) on Jun 01, 2005 at 11:20 UTC
    I've used something like this several times. It doesn't require having things under the same namespace and it can be enhanced to recurse if desired.
    my $dir = "/path/to/modules"; opendir(DIR, $dir); while (my $mod = readdir(DIR)){ next unless $mod =~ /\.pm$/; # print "Use $mod\n"; eval "use $mod"; if ($@) { print $@, "\n\n\n"; exit(1); } } closedir(DIR);