Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Data files in a .par file

by BooK (Curate)
on Jan 13, 2005 at 15:55 UTC ( [id://422008]=perlquestion: print w/replies, xml ) Need Help??

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

Warning: I'm rather new to PAR, so maybe I'm asking a FAQ (I don't think so).

How can a bundled module load some data files included with it in the .par file?

I'm developping a library for a client, let's call it Bam::Kapow. Some data files are needed, so I put them where I was sure I'd find them: in Bam/Kapow/, just below Bam/Kapow.pm.

Here are the few files (simplified as much as possible):

  • lib/Bam/Kapow.pm
    package Bam::Kapow; use strict; use Carp; use File::Basename; use File::Spec::Functions; our $VERSION = "0.01"; our %Info = (); # load the data file for my $data (qw( motd issue ) ) { my $file = catfile( dirname( $INC{'Bam/Kapow.pm'} ), 'Kapow', $dat +a ); open my $fh, $file or carp "Can't open $file: $!"; $Info{$data} = join '', <$fh>; close $fh; } 1;
  • lib/Bam/Kapow/motd
    Message of the day: PAR rules!
  • lib/Bam/Kapow/issue
    Well, I have this minor issue with PAR...
  • example script.pl
    #!/usr/bin/perl use Bam::Kapow; print "Using Bam::Kapow version $Bam::Kapow::VERSION\n"; print "MOTD: $Bam::Kapow::Info{motd}\n"; print "Got Bam::Kapow from $INC{'Bam/Kapow.pm'}\n";

Creating a self-extracting script with the needed library is rather easy, once you know how to pack those additionnal files:

$ find lib/Bam/Kapow/ -type f > bam_kapow.lst</tt> $ pp -o script.exe -A bam_kapow.lst -I lib script.pl $ PATH= ./script.exe Using Bam::Kapow version 0.01 MOTD: Message of the day: PAR rules! Got Bam::Kapow from /tmp/par-ufwk/cache-e73b24e48419250052360127de57a0 +74/inc/lib/Bam/Kapow.pm

Now, I would like to package this as a .par file, rather than as a self-extractible script. The test script would become:

#!/usr/bin/perl use PAR; use lib 'Bam-Kapow'; use Bam::Kapow; print "Using Bam::Kapow version $Bam::Kapow::VERSION\n"; print "MOTD: $Bam::Kapow::Info{motd}\n"; print "Got Bam::Kapow from $INC{'Bam/Kapow.pm'}\n";

First way to create the .par file and test:

$ zip -r Bam-Kapow.par lib adding: lib/ (stored 0%) adding: lib/Bam/ (stored 0%) adding: lib/Bam/Kapow/ (stored 0%) adding: lib/Bam/Kapow/motd (stored 0%) adding: lib/Bam/Kapow/issue (stored 0%) adding: lib/Bam/Kapow.pm (deflated 40%) $ perl script2.pl perl script2.pl Can't open /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/Kapow/ +motd: No such file or directory at script2.pl line 4 Can't open /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/Kapow/ +issue: No such file or directory at script2.pl line 4 Using Bam::Kapow version 0.01 MOTD: Got Bam::Kapow from /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb4 +3c/a23f3e6c.pm ls /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/ a23f3e6c.pm

This does not seem to work (the files are not extracted). Let's try something else:

$ pp -o Bam-Kapow.par -p -M Bam::Kapow -A bam_kapow.lst -I lib -e 1 $ perl script2.pl Can't open /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/Kapow/ +motd: No such file or directory at script2.pl line 4 Can't open /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/Kapow/ +issue: No such file or directory at script2.pl line 4 Using Bam::Kapow version 0.01 MOTD: Got Bam::Kapow from /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb4 +3c/40599894.pm

Same problem, no surprise here.

Is PAR::read_file() my only option? That means that the module needs to detect if it was loaded with PAR or not (I have a test suite to run, after all), which may lead to more complicated (and buggy) code.

Replies are listed 'Best First'.
Re: Data files in a .par file
by Anonymous Monk on Jan 13, 2005 at 17:52 UTC
    If you look into the directory you used to "nmake test" when installing PAR, you will
    find a contrib directory somewhere underneath. Somewhere under there you will find a
    file called automated_pp_test.pl that has a multitude of tests. I believe the small -a
    and large -A tests are made for what you want to do. You will find tests entitled

    sub pp_test_small_minus_a {
    sub pp_test_large_minus_A {

    I believe the explaination blocks in the file are up to date. You can also use
    perl -d (debugger) and step through the actual test code.

    Good luck.

Re: Data files in a .par file
by rdfield (Priest) on Jan 13, 2005 at 16:50 UTC
    PAR files are just Zip files, have you tried using Archive::Zip to extract the data file?

    rdfield

      Yes, a possible solution is to not use PAR at all! You can replace:
      use PAR; use lib 'Bam-Kapow';
      (a.k.a. use PAR 'Bam-Kapow';) by:
      BEGIN { use Archive::Zip; my $zip = new Archive::Zip('Bam-Kapow.par'); $zip->extractTree('', 'shazam/'); use lib 'shazam/lib'; } END { use File::Path; rmtree 'shazam/'; }
      which produces the output:
      Using Bam::Kapow version 0.01 MOTD: Message of the day: PAR rules! Got Bam::Kapow from shazam/lib/Bam/Kapow.pm
      Of course, it would be nicer if PAR provided an option to do that, something like:
      use PAR 'extract';
      which would make it behave the same way it does when it's wrapped up in an executable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-10-15 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.