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 ?


In reply to Require all modules in lib ? by ZlR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.