On Fri, 10 Jun 2011, Sherm Pendley wrote:
> But the question is, should it be done for PERL5LIB? That affects
> *all* Perls, and if you've included the path to modules compiled for
> (say) 5.12, but you're running 5.10, those modules won't work.

Ah, yes, sorry, lost track of the real topic of the thread.

I think a better way to modify your @INC is on a per-installation
basis.  For Apple's Perl you have the AppendToPath and PrependToPath
mechanism, e.g.

$ cat /Library/Perl/5.10.0/AppendToPath 

There is no PrependToPath file by default, but you can create one
yourself, and all directories listed in there will be put at the
front of @INC, just as if you put them into PERL5LIB.

If you build your own Perl, then you may want to ./Configure it
with -Dusesitecustomize.  That way you can modify @INC in a
perl/site/lib/sitecustomize.pl file at runtime.

ActivePerl uses this mechanism to add a per-user install directory
to @INC:

$ cat /usr/local/ActivePerl-5.14/site/lib/sitecustomize.pl 
# ~/Library/ActivePerl-5.14 is the default location for PPM install
# So make sure we look for modules there
if (my $home = (getpwuid($<))[7]) {
    my $lib = "$home/Library/ActivePerl-5.14/lib";
    unless (grep { $_ eq $lib } @INC) {
        # Insert $lib just ahead of 'site/lib' so that overrides
        # via $ENV{PERL5LIB} or 'perl -I...' still works
        (my $site = __FILE__) =~ s,/sitecustomize\.pl\z,,;
        my $i = $#INC;
        $i-- while $i > 0 && $INC[$i] ne $site;
        splice(@INC, $i, 0, $lib);

This sitecustomize.pl script is a little more complicated because
it inserts the directory behind the ones specified with PERL5LIB
and -I, but before the builtin ones:

$ PERL5LIB=~/mylib perl -E 'say for @INC'

So again, you can customize your @INC setup for each Perl installation
independently without resorting to global environment variables, which
will just get in the way at the wrong time...


Reply via email to