develooper Front page | perl.par | Postings from March 2006

chdir and relative paths in @INC

Thread Next
Chris Dolan
March 17, 2006 09:45
chdir and relative paths in @INC
Message ID:
I've stumbled on a case where PAR and perl behave a little  
differently.  I don't think this is a bug to fix, but perhaps a  
caveat worth documenting.  Mostly I'm sharing it for amusement.

If a perl program's @INC contains relative paths and calls chdir  
before loading a module, the program's behavior can be different from  
a PAR-packed executable based on that program.  This is because 'pp'  
typically gleans the module loads without executing code.  Consider  
this highly contrived example:

---- MANIFEST ----

---- ----
use lib qw(.);
chdir '..';
require Foo;
print "OK\n";

---- ----
package Foo;

Then run:
% pp -o test
% perl
Can't locate in @INC (@INC contains: . [snip...]) at  
line 3.
% ./test

I stumbled across this while writing a daemon that calls chdir('/')  
to avoid tying up any mounted filesystems (as recommended in perlipc)  
and noting that the program failed under /usr/bin/perl, but succeeded  
as a PAR executable.  So I ended up writing my code like this to make  
both cases succeed:

          for (@INC) {
             if (! ref $_ && -d $_ && !File::Spec- 
 >file_name_is_absolute($_)) {
                $_ = File::Spec->rel2abs($_);
          # Move to root so filesystems can unmount if needed
          chdir File::Spec->rootdir();

Chris Dolan, Software Developer, Clotho Advanced Media Inc.
608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703

Clotho Advanced Media, Inc. - Creators of MediaLandscape Software  
( and partners in the revolutionary  
Croquet project (

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About