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

Dear Brothers and Sisters in Perl!

I'm writing a module and this module reads a configuration file (actually a YAML-file) which is stored in a directory one level down the module. I tried to get at it by using the file path ./one_level_down/config.yml, but that fails as this relative path starts at the location of the script useing the module.

Is there a simple way of pulling in the file_path of the module so I can build an absolute path?

I will still have a problem then with the tests, won't I as they use another directory structure?

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Handling config-files in Tests
by kyle (Abbot) on Feb 16, 2007 at 18:10 UTC

    I'm not positive I understand your question, but you might be able to use the %INC hash for that.

    use Data::Dumper; print Dumper( \%INC );

    Prints:

    $VAR1 = { 'warnings/register.pm' => '/usr/share/perl/5.8/warnings/regi +ster.pm', 'bytes.pm' => '/usr/share/perl/5.8/bytes.pm', 'XSLoader.pm' => '/usr/lib/perl/5.8/XSLoader.pm', 'Carp.pm' => '/usr/share/perl/5.8/Carp.pm', 'Exporter.pm' => '/usr/share/perl/5.8/Exporter.pm', 'warnings.pm' => '/usr/share/perl/5.8/warnings.pm', 'overload.pm' => '/usr/share/perl/5.8/overload.pm', 'Data/Dumper.pm' => '/usr/lib/perl/5.8/Data/Dumper.pm' };
      Thank you, it works like a charm in combination with some File::Basename magic to extract the path-part.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Handling config-files in Tests
by dragonchild (Archbishop) on Feb 17, 2007 at 09:56 UTC
    You have an answer that works for now, however you might want to consider that hard-coding the configfile's location relative to the module location is poor. Much better would be to have that as a default location and provide a mechanism, probably through an import() function, that allows for that location to be overridden. Then, you can use that mechanism within your tests.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      That would make it more flexible indeed and I might consider that for a next version. Thanks!

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Handling config-files in Tests (SOLVED)
by xdg (Monsignor) on Feb 17, 2007 at 13:47 UTC

    Not quite an answer to your question, but consider the possibility of using File::ShareDir for this.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Wonderful! I never heard of this module but I immediately checked it, installed it and used it. Its companion module Module::Install solved a few other problems in my Makefile.pl too!

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Handling config-files in Tests
by fenLisesi (Priest) on Feb 17, 2007 at 10:32 UTC
    Would it be possible to put the conf right there in the same file as the MyApp::Config module that returns a config object, in the __DATA__ section?
      Yes, there is no reason the config-data could not be put in the module itself, other than that I personally like the config data to be in a separate file and in the vague hope that I will write a script to automatically fill the conf-file with the necessary data every time the underlying data changes.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        And the fact that putting the config in the module severely limits the usefulness of the module.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?