Re: Load all modules in directory
by batkins (Chaplain) on Dec 10, 2003 at 19:56 UTC
|
As others have, suggested, require is the answer. However, in order to get the full functionality of use, you should call import on the package you just required.
BEGIN {
@mods = glob("/home/cuberat/mods/*");
for (@mods) { require $_; $_->import; }
}
Also, I'm not sure if this is intentional, but globbing those files leaves the .pm on, which may not be what you want. Try adding
s{.pm}{};
to your for loop before the require.
Are you sure it was a book? Are you sure it wasn't.....nothing?
| [reply] [d/l] [select] |
|
Also, I'm not sure if this is intentional, but globbing those files leaves the .pm on, which may not be what you want.
Yes yes yes yes yes yes.
Always verify that the data you are trying to use is of the correct format. I would recommend doing a print to <STDOUT> the elements of @mods to see if you are acutally getting what you need, and not getting something that is causing your code to choke.
| [reply] |
|
Hmm...I tried the above without success. It looks like the problem is that the functions in the Module are not being pulled into the current package
.
The require and import proceed without failure, but as soon as the function is called, I get
Undefined subroutine &main::webcast_header called at multi_reporter.pl
+ line 133
.
Here is the current non-working thing that I'm trying:
BEGIN {
$mod_path="/home/tboyd/lib/site_perl/5.6.1/Tools/";
unshift @INC, $mod_path;
my @mods = glob("$mod_path/*");
for (@mods) {
s/.*\/(\w+\.pm)$/$1/gi;
require $_;
$_->import;
}
}
The module that is being called works just fine under a "use" directive. Why might the import not be working?
-C
P.S. removing the .pm will make this thing barf on the "require". | [reply] [d/l] [select] |
|
| [reply] |
Re: Load all modules in directory
by caedes (Pilgrim) on Dec 10, 2003 at 18:58 UTC
|
You can't 'use' a filename, but you can 'require $filename'. That should work.
| [reply] |
Re: Load all modules in directory
by hardburn (Abbot) on Dec 10, 2003 at 19:00 UTC
|
Use require instead of use and it might work (be sure to prepend the directory path, though).
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
|
I've tried all these things and just can't get the damn thing to work. Here is the latest version (which still fails miserably)
BEGIN {
$mod_path "/home/tboyd/lib/site_perl/5.6.1/Tools/";
my @mods = glob("$mod_path*");
for (@mods) {
require $_;
}
}
Again...my intention is to load all the .pm files in that directory at compile time.
Any other thoughts?
| [reply] [d/l] |
|
BEGIN {
my $mod_path = "/home/tboyd/lib/site_perl/5.6.1/Tools/";
my @mods = glob("$mod_path*");
for (@mods) {
require $_;
}
}
It should be noted that require is different from use in that use calls the module's import method and require does not. So, this could cause problems later on if you expect symbols to be imported (or these modules do something else nice for you in import).
You may also want to change your glob to end in *.pm if you only want those files as there could be other files in the directory that aren't perlish. | [reply] [d/l] [select] |
|
|
|
require X;
require Y;
require Z;
Unfortunately I don't have anywhere to test that right now.
| [reply] [d/l] |
|
|
|
Re: Load all modules in directory
by pginmd (Initiate) on Dec 10, 2003 at 22:10 UTC
|
The simplest thing I see is:
BEGIN
{
# get list of modules
my @mods = glob("/home/cuberat/mods/*");
for( @mods )
{
s/\.pm$//i; #strip .pm from end of file
eval "use $_"; #use the module!\
}
}
| [reply] [d/l] |
|
I concur with the "eval use".
I have an application which currently uses this system and it works perfectly. You may want to check $@ for problems during the execution of the eval, but otherwise all is well.
In my usage, this technique allows me to include OS-specific modules and code while leaving the main application oblivious to the OS-specific differences. I am basically using $^O ($OSNAME) to decide what modules to load with "eval use".
The application in question is a custom build system. I have a function that is called "build_installer" that is really implemented 5 different ways on different platforms, all without any sort of if blocks. I simply implemented the function 5 different times in 5 different modules and pick the right one at runtime based on the OS the user is running. This way, the Win32 version can use Win32 modules without caring about not being able to run those under Linux, etc.
| [reply] |
|
Don't do that.
String eval is inefficient.
You get the same results with
eval {
require $modname;
$modname->import();
};
| [reply] |
Re: Load all modules in directory
by PodMaster (Abbot) on Dec 10, 2003 at 18:46 UTC
|
Hi.
use is a function, so `perldoc -f use' (that should clear everything up).
MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" | I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). | ** The third rule of perl club is a statement of fact: pod is sexy. |
| [reply] |
Re: Load all modules in directory
by AcidHawk (Vicar) on Dec 11, 2003 at 13:50 UTC
|
just a thought.. but what is to stop you putting in malicious.pm in /home/cuberat/mods/ which does a rm -rf type thing..?
-----
Of all the things I've lost in my life, its my mind I miss the most.
| [reply] [d/l] [select] |