Messed up Perl

Hal Vaughan hal at halblog.com
Sun Oct 3 16:41:44 PDT 2010


On Oct 3, 2010, at 7:25 PM, Jon Hermansen wrote:

> Better late than never, I wrote this earlier but never sent it along...
>  
> I just found out that on my iMac, the "perl" command is now /opt/local/bin/perl and not /usr/bin/perl.  Now maybe that's not a big issue, since I have a kerbang at the start of all my scripts, but there's more of a problem.
> 
> You probably want to modify your PATH environment variable, or start overriding your shebang line by not running it like this:


On the iMac itself, when not doing anything in MacPorts, do you know if there's a priority set when specifying PATH in something like .bash_profile?  In other words, if I have "/usr/bin/" before "/opt/local/bin", does it search /usr/bin for an executable before searching /opt/local/bin?  That seems like it'd be the ideal situation for me, since a "regular" OS X program would be chosen before anything in /opt if those directories were later in the path.

Or is it a rule that the last directory specified gets priority?

> ./foo.pl

So if I have a shebang "#!/usr/bin/perl" and still run a program from the command line without specifying, it will use the perl executable specified, won't it?

> You can pick and choose which Perl you want to use at runtime by providing the complete path to Perl, so given your program foo.pl, you can run either:
> 
> /usr/bin/perl foo.pl
> /opt/local/bin/perl foo.pl
> 
> and run it with whichever version of Perl you like.
> 
> I ran CPAN to install WWW::Mechanize and it's in /opt/local/libs/perl5/site_perl and I can't include them in my script with "use WWW::Mechanize."
> 
> This is completely normal if you ran /opt/local/bin/cpan and not /usr/bin/cpan, but try to use /usr/bin/perl to execute your program. If you want to install Perl modules for the Apple-provided version of Perl, you need to run CPAN by running /usr/bin/cpan. If you don't mind using MacPorts' perl, why not just 'port install p5-www-mechanize' ? It's already been packaged as port, and you don't need CPAN to use it, plus you get all the awesome functionality that 'port' provides.

Part of it is that I also have a MacMini that's a server on my LAN.  While this script is a special case, in general, I'd rather keep a program as global as possible.  This script could easily be modified to be quite useful at other times in the future, as well and I could see using it on either Mac, or even on a Linux or Windows box in the future, so if I can just stick with "#!/usr/bin/perl" I'd prefer it.  (I know -- Windows is a whole 'nother story!"

> So how do I either return Perl to "normal" so my Mac uses the regular Perl all the time
> 
> Remove /opt/local/bin from your PATH. In a terminal:
> 
> yawn-lappy:~ jhermansen$ echo $PATH
> /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
> yawn-lappy:~ jhermansen$ which perl
> /opt/local/bin/perl
> yawn-lappy:~ jhermansen$ perl -e 'print $ENV{'_'}, "\n";'
> /opt/local/bin/perl
> 
> yawn-lappy:~ jhermansen$ export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin"
> yawn-lappy:~ jhermansen$ which perl
> /usr/bin/perl
> yawn-lappy:~ jhermansen$ perl -e 'print $ENV{'_'}, "\n";'
> /usr/bin/perl

The only issue with that is I wanted to do it permanently and I'm still looking for info on how to control Mac environment variables -- other than setting in .bash_profile, and I like to know if something else will be effected when I make a change.

> FYI, MacPorts has a post-install script that modified your PATH so you may want to revert that, see: http://guide.macports.org/#installing.shell.postflight

Thanks!  I'll look at that.

> More information on $PATH + 'which': http://www.linux-tutorial.info/modules.php?name=MContent&pageid=329
> 
> You can also immediately remove all MacPorts-installed versions of Perl by running 'sudo port -f uninstall perl5 perl5.8 perl5.10 perl5.12' if you so choose.
>  
> or make Perl use the modules in /opt/local/libs/perl5/site_perl?
> 
> You can try this too by modifying the PERL5LIB environment variable, though you may have the least success trying this...
> 
> PERL5LIB="/opt/local/lib/perl5/site_perl"

I tried "echo $PERL5LIB" and got an empty string, so I'm not sure what this does with Perl."

> But you should also know that the MacPorts-provided version of Perl has many other directories by default in @INC, which is an array built from $PERL5LIB in your environment, and paths hard coded into the perl binary.
> 
>   @INC:
>     /opt/local/lib/perl5/site_perl/5.8.9/darwin-2level
>     /opt/local/lib/perl5/site_perl/5.8.9
>     /opt/local/lib/perl5/site_perl
>     /opt/local/lib/perl5/vendor_perl/5.8.9/darwin-2level
>     /opt/local/lib/perl5/vendor_perl/5.8.9
>     /opt/local/lib/perl5/vendor_perl
>     /opt/local/lib/perl5/5.8.9/darwin-2level
>     /opt/local/lib/perl5/5.8.9
>     .
> 
> And you can also modify @INC by putting
> 
> use lib '/opt/local/libs/perl5/site_perl';

So, I take it, Per will see that as a directory and not a module that's misnamed and add it to @INC?  Thanks -- I didn't know that and it's a huge helP!

> in your script. I think you can also do:
> 
> use lib qw[ 
>     /opt/local/lib/perl5
>     /opt/local/lib/perl5/blah ...
> ]; 
> 
> which may work better than directly modifying @INC like Arno mentioned.

I'm not sure why, but my attempt to modify @INC went bad, so I'm not thrilled with it.

> More information on PERL5LIB: http://perldoc.perl.org/perlrun.html#ENVIRONMENT
> 
> Long story, I know... HTH.

Thanks -- it does help and I appreciate all the extra info!



Hal


More information about the macports-users mailing list