Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Load all modules in directory

by batkins (Chaplain)
on Dec 10, 2003 at 19:56 UTC ( #313839=note: print w/replies, xml ) Need Help??


in reply to Load all modules in directory

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?

Replies are listed 'Best First'.
Re: Re: Load all modules in directory
by mcogan1966 (Monk) on Dec 10, 2003 at 20:24 UTC
    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.

Re: Re: Load all modules in directory
by ttcuberat (Sexton) on Dec 10, 2003 at 20:33 UTC
    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".
      The require needs a file name and the import needs a package name. So you need to figure out how to require "/home/tboyd/lib/site_perl/5.6.1/Tools/ModuleA.pm" but import "Tools::ModuleA".

      You might want to see the thread about import.pm on perl5-porters for ideas about doing this or reasons not to.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-12-09 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?