views:

135

answers:

4

Consider that I have 100 Perl modules in 12 directories. But, looking into the main Perl script, it looks like 100 use p1 ; use p2 ; etc. What is the to best way to solve this issue?

+3  A: 

Put all the use statements in one file, say Mods.pm:

package Mods;

use Mod1;
use Mod2;
...

and include the file in your main script:

use Mods;
eugene y
still fine . is there any other way to do it . and Thanks :-)
joe
+2  A: 

I support eugene's solution, but you could group the use statements in files by topic, like:

package Math;

use ModMatrix;
use ModFourier;
...

And of course you should name the modules and the mod-collections meaningful.

Hinek
+7  A: 

It seems unlikely to me that you're useing all 100 modules directly in your main program. If your program uses a function in module A which then calls a function from module B, but the main program itself doesn't reference anything in module B, then the program should only use A. It should not use B unless it directly calls anything from module B.

If, on the other hand, your main program really does talk directly to all 100 modules, then it's probably just plain too big. Identify different functional groupings within the program and break each of those groups out into its own module. The main reason for doing this is so that it will result in code that is more maintainable, flexible, and reusable, but it will also have the happy side-effect of reducing the number of modules that the main program talks to directly, thus cutting down on the number of use statements required in any one place.

(And, yes, I do realize that 100 was probably an exaggeration, but, if you're getting uncomfortable about the number of modules being used by your code, then that's usually a strong indication that the code in question is trying to do too much in one place and should be broken down into a collection of modules.)

Dave Sherohman
+1  A: 

Putting all of the use statements in a separate file as eugene y suggested is probably the best approach. you can minimize the typing in that module with a bit of meta programming:

package Mods;
require Exporter;
our @ISA = 'Exporter';

my @packages = qw/Mod1 Mod2 Mod3 .... /;  
     # or  map {"Mod$_"} 1 .. 100  if your modules are actually named that way

for (@packages) {
    eval "require $_" or die $@;  # 'use' means "require pkg; pkg->import()" 
    $_->import();                 # at compile time
}

our @EXPORT = grep {*{$Mods::{$_}}{CODE}} keys %Mods::; # grab imported subs
#or @EXPORT_OK
Eric Strom