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

I have written a module, call it 'MyModule.pm'. The module sometimes needs to read a file 'myfile' that is associated with the module. I want to install the file in the same directory as the module, and have the module read it from there if it needs to. There actually could be up to a few hundred of these files, and I would typically need to read only one or two of them.

I am wondering if there is a simple way to find the file location so that I can open it. Here is what I am doing, and it works, but it looks strange to me, like maybe I am missing a more obvious or general technique.

my $module_fn= $INC{'MyModule.pm'}; my ($vol, $dir, $fn)= File::Spec->splitpath($module_fn); my $the_modules_file= $dir.'myfile';
Is there a better way?

I need something that works with all the typical ways of using the module, for example having it in a directory specified in $PERL5LIB.

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
•Re: Finding a file associated with a module
by merlyn (Sage) on May 08, 2004 at 10:08 UTC
    If you're actually in the file to which you want to find other relative files, simply look at the value of __FILE__. For example, the directory in which your module is contained is:
    use File::Basename; my $location_in_space = dirname(__FILE__);

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Finding a file associated with a module
by PodMaster (Abbot) on May 08, 2004 at 06:52 UTC
    The only thing you're missing is
    my $the_module_file = File::Spec->catpath( $vol, $dir, 'myfile' );
    Aside from that, if you can use the module, you can always find it's directory, $PERL5LIB or PERLLIB or ...

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Finding a file associated with a module
by Abigail-II (Bishop) on May 08, 2004 at 08:33 UTC
    Solve the problem at install time. At that moment you know exactly where the module (and hence the file) is going to be installed - so just hardwire its location. Sure, hardwiring locations has a lot of disadvantages, but considering you've already committed to installing the file in a specific location, the damage has already been done.

    Abigail

Re: Finding a file associated with a module
by eXile (Priest) on May 08, 2004 at 15:33 UTC
    Hi,

    I know perldoc -l <module>gives you the exact filename (including directorypath) of the module that will be used, taking into account trickery you can do like $PERL5LIB. I'd suggest to take a look at the perldoc-source for this (it's of course written in perl).

Re: Finding a file associated with a module
by VSarkiss (Monsignor) on May 09, 2004 at 17:08 UTC

    Do you have control over the file contents? If so, you may be able to just place it in the module file itself, in a DATA section. As I mentioned, this presumes you have complete control over the file.