<?xml version="1.0" encoding="windows-1252"?>
<node id="422008" title="Data files in a .par file" created="2005-01-13 10:55:57" updated="2005-08-08 10:47:41">
<type id="115">
perlquestion</type>
<author id="21732">
BooK</author>
<data>
<field name="doctext">
&lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; I'm rather new to PAR, so maybe I'm asking a FAQ (I don't think so).&lt;p&gt;

&lt;p&gt;How can a bundled module load some data files included
with it in the .par file?&lt;/p&gt;
&lt;readmore&gt;
&lt;p&gt;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 &lt;tt&gt;Bam/Kapow/&lt;/tt&gt;, just below &lt;tt&gt;Bam/Kapow.pm&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;Here are the few files (simplified as much as possible):&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;lib/Bam/Kapow.pm
&lt;code&gt;
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', $data );
    open my $fh, $file
      or carp "Can't open $file: $!";
    $Info{$data} = join '', &lt;$fh&gt;;
    close $fh;
}

1;
&lt;/code&gt;
&lt;li&gt;lib/Bam/Kapow/motd
&lt;code&gt;
Message of the day: PAR rules!
&lt;/code&gt;
&lt;li&gt;lib/Bam/Kapow/issue
&lt;code&gt;
Well, I have this minor issue with PAR...
&lt;/code&gt;
&lt;li&gt;example script.pl
&lt;code&gt;
#!/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";
&lt;/code&gt;
&lt;/ul&gt;
&lt;p&gt;Creating a self-extracting script with the needed library is rather easy, once you know how to pack those additionnal files:&lt;/p&gt;
&lt;code&gt;
$ find lib/Bam/Kapow/ -type f &gt; bam_kapow.lst&lt;/tt&gt;
$ 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-e73b24e48419250052360127de57a074/inc/lib/Bam/Kapow.pm
&lt;/code&gt;
&lt;p&gt;Now, I would like to package this as a .par file, rather
than as a self-extractible script. The test script would
become:&lt;/p&gt;
&lt;code&gt;
#!/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";
&lt;/code&gt;

&lt;p&gt;First way to create the .par file and test:&lt;/p&gt;
&lt;code&gt;
$ 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-51eaa99df530ab0d3b760931a92bb43c/a23f3e6c.pm

ls /tmp/par-ufwk/cache-51eaa99df530ab0d3b760931a92bb43c/
a23f3e6c.pm
&lt;/code&gt;
&lt;p&gt;This does not seem to work (the files are not extracted).
Let's try something else:&lt;/p&gt;
&lt;code&gt;
$ 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-51eaa99df530ab0d3b760931a92bb43c/40599894.pm
&lt;/code&gt;
&lt;p&gt;Same problem, no surprise here.&lt;/p&gt;
&lt;/readmore&gt;

&lt;p&gt;&lt;tt&gt;Is PAR::read_file()&lt;/tt&gt; 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.&lt;/p&gt;

</field>
</data>
</node>
