Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Including text files in Modules

by Miguel (Friar)
on Nov 04, 2004 at 17:51 UTC ( [id://405236]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed Monks,

Is there any simple and practical way to include txt files in a module?

This is my problem:

I have a package named Foo::Foo1::Foo2::FooPackageName This reads 3 text files (data sources)

my @files = qw/file1 file2 file3/; foreach my $file (@files) { open( INFILE, '<', './lib/Foo/Foo1/Foo2/' . $file . ".txt" ) or die "$0 : ERROR : could not open file '" . $file . ".txt' : $!\n"; [CODE HERE] }

Now, when I do a perl Makefile.PL, make, make test and make install everything runs fine. (The MANIFEST also includes those 3 files)

Then, I can see the files in the right directory (/usr/lib/etc...)

But, when I try to run some program using that module it complains about not getting the files.

What am I doing wrong or missing here?

---------------- UPDATE -----

Maybe I wasn't clear enough before.
Let me put it this way:

Q1. Is it possible to include txt files (as data sources) inside a Perl package? (not to dump the contents as some kind of variables; the idea is to keep those files intact)

Q2. How do I do that in a portable way? this is, no matter what system is running the module, those files are always accessible (probably in the same directory where the module is)

------------------ UPDATE -----

Miguel

Replies are listed 'Best First'.
Re: Including text files in Modules
by borisz (Canon) on Nov 04, 2004 at 17:57 UTC
    The path is wrong. You look for ./lib that means lib inside the current directory.
    Boris
      That _seems_ to be the problem.

      But if I delete the 'lib' part 'make test' will complain.

      Anyway, if I delete 'lib', and don't run any test, the script still complains: "No such file or directory".

Re: Including text files in Modules
by fglock (Vicar) on Nov 04, 2004 at 18:58 UTC

    Find out where FooPackageName was loaded from, and then use this information as a base for finding your text files:

    use Foo::Foo1::Foo2::FooPackageName; my $file = "file.txt"; my $path = $INC{"Foo/Foo1/Foo2/FooPackageName.pm"}; $path =~ s/FooPackageName.pm/$file/; print "Full path to $file: $path \n";

    (I use this inside the Inline::Parrot module, in order to find out where the "pir" files were installed)

Re: Including text files in Modules
by Anonymous Monk on Nov 05, 2004 at 10:23 UTC
Re: Including text files in Modules
by tfrayner (Curate) on Nov 05, 2004 at 16:58 UTC
    Hi,

    I've just had to figure this out myself recently. The following feels like a bit of a hack, but it does work:

    use File::Spec # NB. File::Spec isn't actually required, # but it's a good cross-platform solution my @module_dir_array = File::Spec->splitpath(__FILE__); my $filename = File::Spec->catpath(@module_dir_array[0,1],'file.txt'),
    It uses the __FILE__ literal which always points at the current filename (see perldoc perldata). So you can put your file.txt file in the same directory as your module.pm and the module should always be able to find it.

    Tim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405236]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found