Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Use"ing a directory of libraries 3

Status
Not open for further replies.

Nebbish

Programmer
Apr 7, 2004
73
US
Hey Perlies,

A simple question: how do I include all modules in a directory with one command? Lets say I have a directory called /MyModules, which contains an unknown number of libraries. I want all of them. "use MyModules;" obviously doesn't work. Is there a solution? Or do I have to statically include each module in the directory?

-Nick
 
look up use lib

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
I don't want to manipulate the @INC array--I want to literally "use" all modules in a directory. For example, lets say I have

/MyModules/moduleA.pm
/MyModules/moduleB.pm
/MyModules/moduleC.pm

instead of doing

use MyModules::moduleA;
use MyModules::moduleB;
use MyModules::moduleC;

I want something cleaner, like:

use MyModules; # Which obvious doesn't work because it'd be looking for a MyModules.pm file, but you get the idea.

I'm pretty sure you misunderstood me, but if use lib has some additional functionality I'm unaware of that may solve this problem let me know.

Thanks for the stab,

Nick
 
Nick, look it up


adding directories to @INC, at compile time, not permanent, for the duration of the instance of the script

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Paul,

Heh, that's the exact documentation I looked at, and I'm still not seeing how it helps. It even says at the top:

"It is typically used to add extra directories to perl's search path so that later use or require statements will find modules which are not located on perl's default search path."

The key part being, "...later use or require statements will find modules...". I want to skip that step altogether! I want to be able to "use lib" a directory and not have to specifically "use" every module in the directory. Or something similar.

I'll rephrase: Let's say I have 100 modules in the directory /MyModules/. I need every one of them in my script, but I don't want to type out

use MyModules::Module1;
use MyModules::Module2;
.
.
.
use MyModules::Module100;

My root question is how do I do all those 100 statements in one line? Doing "use lib MyModules" means I don't have to add the "MyModules::" before each one, but I still have to type out "use Module1; use Module2; etc".

How do I "use" all modules in a given directory?

-Nick
 
Hmmm something like this?

BEGIN{

while (<tst/*.pm>){
/\./; $_=$`;
/\//; $_=$';
eval qq/use tst::$mod or die/;
}

}

exit 0;



Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
no.....

BEGIN{

while (<tst/*.pm>){
/\./; $_=$`;
/\//; $_=$';
eval qq/require tst::$mod; import tst::$mod;/;
}

}

exit 0;



Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I would have thought;
Code:
use lib "MyModules";
for ($i=0;$i++;$i<100) {
  eval "use MyModule$i;";
}

I thought once the directory has been added to the search path, you wouldn't have to explicitly name the directory each time

Could be wrong though
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Neat - though I'd want to generate a list of modules from the files actually present in the directory rather than numerically I think.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Paul--Yes, Mike got to what I was asking. Your code is still quite helpful, though, thank you.

Mike--Didn't know about the while(<folder/*.pm>) thing...reeaally nice to have under my belt. Much less cumbersome than using the FileFind module to browse directories. It turns out my modules are actually in two directories (dirA/dirB/modules.pm), so your code didn't quite work but I tweaked it appropriately and it's cruising fine now. The only thing I don't understand: why use require and import rather than use?

Thanks,

Nick
 
yeah that's pretty slick, I've never seen that while(<folder/*.pm>) technique.. I'll have to remember that one
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top