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

chdir and relative paths in @INC

Thread Next
From:
Chris Dolan
Date:
March 17, 2006 09:45
Subject:
chdir and relative paths in @INC
Message ID:
9ADC4977-3F26-4152-BAA3-2B60273CEB4F@clotho.com
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 ----
test.pl
Foo.pm

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

---- Foo.pm ----
package Foo;
1;


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


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
-- 
Chris Dolan, Software Developer, Clotho Advanced Media Inc.
608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703
vCard: http://www.chrisdolan.net/ChrisDolan.vcf

Clotho Advanced Media, Inc. - Creators of MediaLandscape Software  
(http://www.media-landscape.com/) and partners in the revolutionary  
Croquet project (http://www.opencroquet.org/)



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About