Sunday, January 20, 2013

Running Different Version of Perl on Mac

Running Different Version of Perl on Mac

Upgrading the installed version of perl on a mac can create problems.

An alternative is to use Perlbrew. It allows other installations to be installed and used. New packages can be added to the active perl, without adding it to all perl installations.

Install with
curl -kL http://install.perlbrew.pl | bash

Once installed, initialize in .bash_profile with
source ~/perl5/perlbrew/etc/bashrc

Mac does not have a make command by default. It must be installed before installing any perl installations. It can be found in Xcode and then the command line tools.

I was able to get the command line tools without installing Xcode from
https://developer.apple.com/downloads/index.action?name=for%20Xcode%20-

Find the available perl installations with
perlbrew available

Install a version with
perlbrew install perl-5-16.2

Next, I tried to install cpanminus with
cpan App:cpanminus

It did not install. After switching to perl-5.16.2, it installed.

I am now able to install modules using cpanm into the current perl selected via perlbrew.

------------

I have created a bash script that allows me to switch to a different perl and create an alias named local for the new perl. I also have created a sym link in /usr/local/bin to the local alias. This allows me to change to any perl that I want and not have to change the shebang in the perl files. I use
#!/usr/local/bin/perl


#!/bin/bash

strip() {
    echo -e $1 | tr -d '\r'
}

#$1 is (out) selection, $2 is prompt, $3 is list of choices                                                         
readSelect () {
    local L_CHOICE L_LIST=`strip "$3"`

    if [ -n "$L_LIST" ]; then
        PS3="$2"
        select L_CHOICE in $L_LIST
        do
          if [ -n "$L_CHOICE" ]; then
            break;
          fi
          break;
        done
        eval "$1=\"$L_CHOICE\""
    else
        error "Nothing to select"
    fi
}

choices=`perlbrew list | grep -v "(" | sed -e 's/\* //g' -e 's/ (/_(/g' -e 's/^[[:space:]]*//'`
readSelect perl "Select perl installation: " "$choices"

echo "Setting $perl"

perlbrew switch $perl

perlbrew alias delete local
perlbrew alias create $perl local

No comments:

Post a Comment

Followers