Click here to get back home

Including text files in Modules

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Including text files in Modules Santinho 11-04-2004
Posted by Santinho on November 4, 2004, 9:23 pm
Please log in for more thread options

Greetings,


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)

The problem is:

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?

TIA



Posted by Gunnar Hjalmarsson on November 4, 2004, 11:18 pm
Please log in for more thread options
Santinho wrote:
> But, when I try to run some program using that module it complains
> about not getting the files.

It probably looks for them in the wrong directory, maybe based on the
directory in which the program is located. What exactly does the error
message tell you?

Something like this might work:

my ($moddir) = $INC =~ m|(.+)/|;
foreach my $file (@files) {
open INFILE, '<', "$moddir/$file.txt" or die ...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Posted by Santinho on November 4, 2004, 11:18 pm
Please log in for more thread options

Thank you sooooo very much!
I've been stuck on this for 2 days!



On Thu, 04 Nov 2004 22:18:05 +0100

> Santinho wrote:
> > But, when I try to run some program using that module it complains
> > about not getting the files.
>
> It probably looks for them in the wrong directory, maybe based on the
> directory in which the program is located. What exactly does the error
> message tell you?
>
> Something like this might work:
>
> my ($moddir) = $INC =~ m|(.+)/|;
> foreach my $file (@files) {
> open INFILE, '<', "$moddir/$file.txt" or die ...
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl


Posted by Gunnar Hjalmarsson on November 4, 2004, 11:30 pm
Please log in for more thread options
Gunnar Hjalmarsson wrote:
> Something like this might work:
>
> my ($moddir) = $INC =~ m|(.+)/|;
> foreach my $file (@files) {
> open INFILE, '<', "$moddir/$file.txt" or die ...

Btw, since you want a portable solution, you'd better use File::Basename
rather than a regex for extracting the module directory.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Posted by Brian McCauley on November 5, 2004, 6:25 pm
Please log in for more thread options


Gunnar Hjalmarsson wrote:

> Santinho wrote:
>
>> But, when I try to run some program using that module it complains
>> about not getting the files.
>
> It probably looks for them in the wrong directory, maybe based on the
> directory in which the program is located. What exactly does the error
> message tell you?
>
> Something like this might work:
>
> my ($moddir) = $INC =~ m|(.+)/|;

If you are doing this inside the file Foo/Foo1/Foo2/FooPackageName.pm
itself then it would be simpler just to use __FILE__.



Similar ThreadsPosted
BerkeleyDB: Graduating from text files; tutorial? September 25, 2004, 6:18 am
Any modules to parse ELF files? August 7, 2007, 5:09 pm
Peer-reviewed CPAN modules files wanted August 21, 2006, 3:13 am
Can't use fopen in C after including Perl Interpreter October 4, 2005, 11:35 pm
Portable dot-files (hidden-files) ? November 5, 2007, 1:20 am
Printing of PDF Files July 12, 2004, 4:55 pm
Attaching files with exe. June 6, 2008, 1:35 am
Module(s) for generating PDF files November 28, 2004, 8:56 am
Compare huge XML Files February 23, 2005, 2:01 am
MakeMaker and data files March 22, 2006, 8:02 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap