http://www.perlmonks.org?node_id=561222

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

Esteemed monks,
This is a recurrent question (Including text files in Modules) but I still don't understand how do I include files in directories and subdirectories inside a package. I've been trying all the possibilities posted in the above thread (and some others) but with no success.

The problem is:
I have a file system structure similiar to this:

My/Module | +------- Files | +----- FilesOfTypeA | | | +----File1 | +----File2 | +----File3 | +----FileN | +----- FilesOfTypeB | | | +----File1 | +----File2 | +----File3 | +----FileN | +----- FilesOfTypeN
There are 1000s of files (TXT files) in these directories.
Some files contain specifications which cannot be modified and others contain perl data structures (extracted from those files).

Now, how do I include these files into the package? I've been trying with File::Spec, Cwd, File::Basename but with no success: 'make test' works but then, when I 'use My::Module', it complains about not finding the files.

Sorry for not posting my code, but I don't have it at hand.

Thank you for your time,
Miguel

Replies are listed 'Best First'.
Re: How to include a lot of files in a module?
by sgifford (Prior) on Jul 14, 2006 at 15:36 UTC
    Is the problem you're stuck on how to figure out what directory My::Module is in, so you can find other files relative to it?

    If so, look at the %INC variable, specifically at $INC{'My/Module.pm'}. Here's a small sample:

    package t63; use File::Basename; my $incpath = __PACKAGE__.".pm"; $incpath =~ s/::/\//g; our $dir = dirname($INC{$incpath})||'.'; print "Dir: $dir\n"; 1;

    You might also want to look at the FindBin module, since it does almost what you want but not quite.

      That was may problem. Thank you. Everything works now.
Re: How to include a lot of files in a module?
by shmem (Chancellor) on Jul 14, 2006 at 14:51 UTC
    Well, I would say that the list of modules you give, well combined, would allow for a smart solution. But there's also some that ease directory traversal.

    Since you can't post code, I can only utter commonplace stuff:

    1. to include perl files, there's do, require, use, all of them can be found in perlfunc.
    2. as for require and use, they convert Foo::Bar::Quux into Foo/Bar/Quux.pm.
    3. all other files you will not want to feed to the interpreter, so you must process them yourself.

    But shurely you know all of these. Another thing - perl's search path could be quite different when doing make test and use My::Module. Try setting the environment variable PERL5LIB to the directory of the My/Module.pm file.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to include a lot of files in a module?
by jhourcle (Prior) on Jul 14, 2006 at 14:54 UTC

    In the example you linked to -- you're giving a relative path.

    Without knowing how you're calling the program (CGI? cron?), I'd assume it's just an issue with a current working directory that's not the same as your script. (this is quite common for perl modules -- you'll want to work relative to __FILE__, or some other similar trick)

    But, without code, the error messages, or more info about the calling context, any help you get is going to be pure speculation.

Re: How to include a lot of files in a module?
by xdg (Monsignor) on Jul 14, 2006 at 15:31 UTC
    Now, how do I include these files into the package?

    What do you mean by "the package"? Do you mean that you want to have these files bundled up with your .pm files into a distribution so that if your module is installed somewhere, the files come along with it and your module can find them automatically?

    If that's what you want, then you need to include instructions to that effect in your Makefile.PL or Build.PL so that those files are installed in your Perl library path. By default, only .pm and .pod files are actually installed. For example, here's a recipe from the Module::Build::Cookbook:

    Adding new file types to the build process Sometimes you might have extra types of files that you want to insta +ll alongside the standard types like .pm and .pod files. For instance, +you might have a Bar.dat file containing some data related to the "Foo:: +Bar" module. Assuming the data doesn't need to be created on the fly, the best place for it to end up is probably as Foo/Bar.dat somewhere in perl's @INC path so "Foo::Bar" can access it easily at runtime. The following code from a sample "Build.PL" file demonstrates how to accomplish 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.

Re: How to include a lot of files in a module?
by stonecolddevin (Parson) on Jul 16, 2006 at 00:47 UTC
    personally, i like my $string = do { local $/; <FILEHANDLE> }; for including files. then, you can just print $string to the screen, or whatever you need to do :-)
    meh.