Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Require all modules in lib ?

by ZlR (Chaplain)
on Jun 07, 2005 at 10:10 UTC ( [id://464206]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed Monks of the Monastery,

I'm sure it's a classic problem .
I've read Loading all modules under a directory and How to dynamically load modules but still i can't make it work .

Suppose i have a lib directory : c:\tmp\test\testlib holding two modules : mod1.pm and mod2.pm . These module are in a package called Test and each one defines a function "test1" and "test2" .

These two modules are required in another file, testlib.pm, residing in the same directory

This testlib.pm is in turn used by a script run.pl that is in c:\tmp\test\script\ :

use lib 'c:/tmp/test/' ; use testlib::testlib; Test::test() ;
In testlib.pm, if i do :
require testlib::mod1 ; require testlib::mod2 ;
... Everything works fine .

But this implies that i add every new module manualy, so i tried to dynamically require the module files.
It breaks with "undefined subroutine" in run.pl as if the modules were not required :

foreach ( <*.pm> ) { next if /testlib/ ; $_ =~ s/\.pm// ; my $val = "testlib::" . $_ ; eval "require $val" ; }
I do this in testlib.pm instead of the two requires. I tried many variations like keeping the .pm or using instead or requiring or specifying full path in the require instead of the :: style, everything fails .

Can anyone explain what's hapenning ?

Replies are listed 'Best First'.
Re: Require all modules in lib ?
by tlm (Prior) on Jun 07, 2005 at 10:38 UTC

    I don't know what's happening, but it is always wise to check $@ after an eval; that may give you some idea of what's going on. Alternatively you can do something like this:

    eval "require $val; 1" or die $@;
    or even just this
    eval "require $val" or die $@;
    since the require should return true (the first form is arguably clearer, though).

    the lowliest monk

      Hello tlm,

      It doesn't raise any other warning .

        I tried to reproduce your set-up from your description, and it works like a charm for me, so unless there is some difference between Windows and Unix that is relevant to this problem, or your code is significantly different from mine, I have no clue what could be the cause of the errors you're having. I include all the code below; maybe by comparing it with yours you will to figure out where the problem is. In my case, all the files live in /tmp/test/testlib.

        the lowliest monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found