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

Hello Testers,

I have a directory structure like this:

./lib/MyModule.pm ./lib/t/test_MyModule.pl ./hisScript.pl ./lib/HisModule.pm ./hisFileReadByHisModule.cfg

hisScript.pl uses hisModule.pm, which reads hisFileReadByHisModule.cfg. No path information is given, so I assume that Perl just looks in the current directory of the script. MyModule.pm uses HisModule.pm

The problem is that when I run my test from the t directory, a HisModule-object is initialised, which tries to find hisFileReadByHisModule.cfg. This fails, which causes my test to fail.

I'd like to know what the best way of working around this is. I could put a link to hisFileReadByHisModule.cfg, but this seems rather kludgy. I suspect my whole approach to testing may have some basic flaw.

Can anyone point me in the right direction?

Thanks, loris

Replies are listed 'Best First'.
Re: Stopping included module breaking test
by blazar (Canon) on May 10, 2005 at 12:27 UTC
    I'm not sure if this is what you want, but I often use FindBin.
Re: Stopping included module breaking test
by dragonchild (Archbishop) on May 10, 2005 at 12:36 UTC
    FindBin is definitely a possibility . . .

    However, items that prove problematic to test general indicate a problem with the architecture. Personally, I like to pass configuration files into a module and have them handle both filenames and filehandles. That way, I can use IO::Scalar and File::Temp to test both options, and the code is more reusable, to boot.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

      Hello dragonchild,

      I am sure you are right about the architecture being a bit ropey and that this should be corrected. However, I am also interested in the question of how to deal with the effects without having (to get somebody else) to fix the root problem.

      Thanks, loris

        FindBin is the correct answer, then. You will have to make this change to the module.

        • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
        • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Stopping included module breaking test
by salva (Canon) on May 10, 2005 at 14:21 UTC
    usually t/, the directory containing the test scripts, doesn't go inside lib/ but on the package root dir.
Re: Stopping included module breaking test
by thcsoft (Monk) on May 10, 2005 at 12:28 UTC
    maybe ExtUtils::MakeMaker and related modules will give you some hints. furthermore a look at 'perldoc perlmod', 'perldoc perlmodlib' etc. might be helpful.

    language is a virus from outer space.