Perl fails @INC (again) after OS X upgrade

After upgrading to Mac OS X 10.9 on my main Mac account today, as happens every time I upgrade Mac OS X, my Perl scripts didn’t work.  Again,

Can't locate My/Module.pm in @INC (@INC contains:/Library/Perl/blah, /System/Library/Perl/blah, blah, blah…

Sigh.  The problem is that @INC is hard-coded into Perl to contain only system directories, and so the installers for third-party Perl modules that I’ve downloaded from the Comprehensive Perl Archive Network (CPAN) put modules that I need into these system directories, which are wiped by Apple’s installers when you upgrade Mac OS X.

It doesn’t seem to bother the Linux geeks.  But in Mac OS X, we try to to keep all of our stuff in our Home directories.  (We also don’t like kill several weekends per year reinstalling our systems from scratch.)

Rather than getting them off of my Time Machine disk and installing them back into the same stupid place, this time I put them in my Home directory,

~/Library/Perl/

and also I put all of the modules into that folder directly, foregoing the 5.105.125.16 etc. subfolders.  It doesnt make any sense to me that these modules are associated with specific Perl versions.  Im only running one version of Perl, and if I need to update a module, I need to update a module.  Old versions of modules, if thats what those folders are for, dont do me any good.

Now three other things are necessary to make this work.

First, in ~/.bash_profile, add the line

export PERL5LIB=~/Library/Perl

This adds ~/Library/Perl/ to @INC whenever Perl runs under the bash shell in Terminal.  (For it to be effective, after saving .bashrc with that line, you need to open a new Terminal window.)

Second, and this is kind of a pain, whenever I install a new Perl module from CPAN from now on, I’m going to need to move it from /System/Library/Perl/wherever into my new ~/Library/Perl/ folder, or else it will be wiped again the next time I upgrade OS X.  But Id rather do the extra effort up front because the days when I upgrade OS X are busy enough, and also my Home folder gets more backup attention, as it should.

Third, and I discovered this after day 6, any AppleScripts which invoke Perl scripts that need my modules using the do shell script command wont work.  To fix a nonworking AppleScript, prepend the PERL5LIB environment variable.  That is, replace

do shell script myCommand

with

set  myCommand to "export PERL5LIB=~/Library/Perl ; " & myCommand
do shell script myCommand

* * *

It is true that, like most innovations, I’m exchanging one collection of pros and cons for a different collection, but I still like this new way better.

I have not done a “clean" install of Mac OS X since I first installed it 11 years ago, except on a few occasions, before Time Machine, when my hard disk had failed.

UPDATE 2014-08-16.  I updated a few days ago to OS X 10.10 Yosemite on my main Mac.  Happily, for the first time ever, all of my Perl scripts seem to just work”. I’m leaving it this way!

Jerry Krinock