Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Include Modules Automatically?

by meetraz (Hermit)
on Jan 05, 2009 at 18:58 UTC ( [id://734244]=perlquestion: print w/replies, xml ) Need Help??

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

I have my own library of modules which function sort of like plug-ins:

use MyPlugins::ModuleA; use MyPlugins::ModuleB; use MyPlugins::ModuleC; (etc)

This list is now a couple pages long, and tedious to maintain. Is there an easier way to just automatically include all the modules installed in a certain directory (e.g. MyPlugins) without specifying them manually somewhere?

Or do I have to resort to a File::Find hack?

Replies are listed 'Best First'.
Re: Include Modules Automatically?
by Corion (Patriarch) on Jan 05, 2009 at 19:04 UTC
Re: Include Modules Automatically?
by kennethk (Abbot) on Jan 05, 2009 at 19:41 UTC

    The following module will import every module in directory Foo and export everything the submodules export, and is a potential security risk for pathological file names:

    package Foo; use strict; BEGIN { my @modules = (); opendir DIRHANDLE, 'Foo'; while (my $file = readdir DIRHANDLE) { if ($file =~ /\.pm$/) { $file=~s/\.pm//; push @modules, $file; } } closedir DIRHANDLE; require Exporter; our @ISA = qw(Exporter); our @EXPORT = (); for my $module (@modules) { eval 'use Foo::'.$module; { no strict 'refs'; push @EXPORT, @{"Foo\:\:$module\:\:EXPORT"}; } }; } 1;

    Update: Forgot to close the directory handle.

Re: Include Modules Automatically?
by FunkyMonk (Chancellor) on Jan 05, 2009 at 19:59 UTC
    Try Toolkit. It lets you keep a list of modules to use in a file.
Re: Include Modules Automatically?
by jasonk (Parson) on Jan 06, 2009 at 21:01 UTC

    When they are all similarly named like this, I tend to go for the Module::Find approach:

    package MyPlugins; use Module::Find (); Module::Find::useall( __PACKAGE__ );

    Although you need a bit more than that if the modules are exporting things.


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://734244]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found