You are on page 1of 26

Editor's Note: This article was originally written after the release of Mac OS X 10.

2 Jaguar, but the


information is still relevant even with the release of Panther. Perl 5.8 is included in the installation
of Mac OS X v10.3 Panther, but you may want to install your own version if you plan to customize
it; or if you are still running Jaguar.

The newest release of Apples operating system, OS X v10.2 (Jaguar) is filled with many welcome
improvements and additions. The integrated Address Book, improved Mail, System Preference
tweaksall good things. But what really excited me was the new stuff under the hood: bash,
Python, gcc 3, Ruby. About the same time Jaguar was in development, work was finishing up on my
beloved Perl, version 5.8.1.

A quick trip to Jaguars Terminal showed me that this version didnt make it into the default install:

[cpu:~] user% perl -v


This is perl, v5.8.1-RC3 built for darwin-thread-multi-2levelAs is almost always the case with
UNIX-based systems, there is a workaround. So, if youre looking to get Perl 5.8 up and running on
Jaguar, Im here to tell you how.

Preparation
If youve installed shell applications before, you know how the process usually goes: download the
source, configure the source, compile the source, and install the source. Perl is no different, but does
require some patience; compiling the package could take you an hour or more, depending on the
speed of your machine.

Some caveats to consider before starting:

You need to have Apples Development Tools installed. To learn more about these and how to
download the latest set for free, visit the Development Tools page on ADC.
If you use Apaches mod_perl, youll need to recompile it.
The process described below was performed on a fresh install of Mac OS X v10.2, along with the
Developer Tools. These instructions may not work perfectly on previous versions of Mac OS X.
If, after the installation, you get warnings about missing symbols, you probably have an old version
of Perl (or parts of one) in /Library/Perl. These undefined symbols existed in pre-5.8.1 versions. For
more information on this issue, check out perldelta.
OS X users that have installed Fink may experience various symbol errors when executing certain
Perl operations. More on this can be found in the macosx@perl.org archives.
The Perl install docs state: [Perl 5.8.1] is not binary compatible with releases of Perl prior to 5.8.1.
If you have built extensions (i.e., modules that include C code) using an earlier version of Perl, you
will need to rebuild and reinstall those extensions. Pure Perl modules without XS or C code should
continue to work fine without reinstallation. This means that after installation, some of your
modules (like HTML::Parser, XML::Parser, etc.) may not work out of the box without re-
compilation.
Downloading Perl 5.8.1
The first step is to download Perl 5.8.1 itself. You can find it on any number of CPAN mirrors
worldwide. I opened up the Terminal and entered the following commands (you can replace the
URL below with one that reflects your closest mirror):

[cpu:~] user% cd /usr/local/

[cpu:~] user% sudo mkdir src


[cpu:~] user% sudo chown root:staff src
[cpu:~] user% sudo chmod 775 src
[cpu:~] user% cd src
[cpu:~] user% curl -O ftp://ftp.cpan.org/pub/CPAN/src/perl-5.8.1.tar.gz
[cpu:~] user% tar zxvf perl-5.8.1.tar.gz

[cpu:~] user% cd perl-5.8.1These instructions created a /usr/local/src directory with adequate


permissions to move around. The curl -O command downloaded a copy of Perl 5.8.1 (about a 10
MB file) into this directory. Next, I extracted the archive into a directory called perl-5.8.1, and then
moved into it.
Backing Up
Since Perl is already installed in OS X (and perhaps already tweaked and configured to your liking),
you may want to think about backing up your existing installation. Perl is predominantly located in
three places: the private system library in /System/Library/Perl, the local libraries in /Library/Perl,
and the binaries (like perl, pod2html, h2xs, etc.) under /usr/bin. Depending on your needs, you may
also have modules installed under /Network/Library/Perl. If youre worried about things going
wrong, you may want to save copies of these directories.

The Install Directory


Most shell applications offer you the choice of where to install, and Perl is no different. Some users
install large applications in a directory called /opt, others follow Finks behavior and drop them into
/sw, and still others follow the standard /usr/local. Ill be using the default Perl layout for OS X
which is determined by hints/darwin.sh. This file is included as part of the Perl distribution and is
utilized during our initial configuration. Opening that file in a text editor, well see that a default
install is defined as:

# Default install; use non-system directories


prefix='/usr/local'; # Built-in perl uses /usr
siteprefix='/usr/local';
vendorprefix='/usr/local';
usevendorprefix='define';

# Where to put modules.


privlib='/Library/Perl'; # Built-in perl uses /System/Library/Perl
sitelib='/Library/Perl';
vendorlib='/Network/Library/Perl';If we dont specifically tell Perl to be installed elsewhere, the
binaries will be installed into /usr/local/bin, and our Perl modules (our @INC) will be set for
/Library/Perl. This means that Apples system library, /System/Library/Perl, wont be utilized (and,
if you were fickle enough, could be deleted). If you want the Perl modules installed in the exact
same locations as Apples current Perl 5.6.0, then youll want to assign an install prefix of /usr,
which hints/darwin.sh associates with the following directories:

# I'm building/replacing the built-in perl


siteprefix='/usr/local';
vendorprefix='/usr/local';
usevendorprefix='define';

# Where to put modules.


privlib='/System/Library/Perl';
sitelib='/Library/Perl';
vendorlib='/Network/Library/Perl';Installation
When installing, its always a good idea to make sure youre starting from a fresh build directory.
This ensures that theres no old configuration data or libraries hanging around and messing up your
installation. Heres the command (and dont worry if this is your first time messing around with Perl
this command wont hurt anything):

rm -f config.sh Policy.shIf you want to experiment with different Configure options, then be sure to
run the following to start afresh (this command duplicates the above deletion, along with cleaning
out stuff created during a Configure):

make distcleanConfigure and Options


Now comes the first part of your great journey with Perl 5.8.1: the configuration. Perl needs to
know hundreds of bits of information to be correctly compiled for your architecture. Thankfully, the
Configure script handles most of this for you. Before moving on, however, you need to set an
environment variable:

setenv LC_ALL C
echo "setenv LC_ALL C" >> ~/.tcshrcIf youre curious about what those commands are doing, then
youll want to check the INSTALL file, as well as man perllocale. OS X has one of the necessary
environment variables set (LANG), but without the above, youll get annoying error messages each
time you try to run a script. The above assumes the tcsh shell; if you prefer bash, use echo export
LC_ALL=C >> ~/.bash_profile instead.

Now, enter one of the following commands, depending on where you want Perl installed:
# you want to use the default Perl directories:
./Configure -de -Dperladmin=email@addr.com -Dcf_email=email@addr.com

# you want to install Perl the same way Apple did:


./Configure -de -Dprefix=/usr -Dperladmin=email@addr.com -Dcf_email=email@addr.comThe
only difference between these two lines is the -Dprefix flag, which determines where Perl should be
installed (the rest of this article assumes you ran the first one). The -de helps the configuration go
more quickly by assuming the defaults for most of the questions, as well as not prompting you for
further configuration of the generated config.sh. The -Dcf_email and -Dperladmin flags set the
email addresses for the installer and administrator of this Perl installation, respectively. These flags
are optional, as the Configure will automatically do its best to guess them.

You may also want to look at -Dotherlibdirs. With this option included in the Configure statement,
you can add more library directories to Perls @INC. For example, if I wanted each of my users to
have their own Perl library, I could use -Dotherlibdirs=~/Library/Perl. If I log in as morbus, Ill be
able to stick my own modules in /Users/morbus/Library/Perl without interfering with the rest of my
Perl installation. Of course, if I didnt use -Dotherlibdirs, Id be able to duplicate the same effect in
my code with use lib.

Compiling, Testing and Installing


Thankfully, the Configure command is the most brain-consuming part of the installation. Once the
Configure is run (it took two minutes on my dual 400mhz G4), youll be home free. It will usually
finish by saying, You must run make, so go ahead and follow its orders:

[cpu:~] user% makeThis command took about 15 minutes on my machine. It should complete with
the following: Everything is up to date. Type make test to run test suite. Again, the install knows
best, so run:

[cpu:~] user% make testThe testing process is a common part of a Perl-related installation. Youll
see a large number of tests run, most returning a status of OK, and some skipped due to our
platform. You should only get two failures, both related to Berkeley DB:

ext/DB_File/t/db-btree...............#
# This test is known to crash in Mac OS X versions 10.1.5 (or earlier)
# because of the buggy Berkeley DB version included with the OS.
#
FAILED at test 0This test, as well as db-recno, fails because OS X ships with an old version of a
common database library (Berkeley DB, version 1.x). Perl gives us more advice concerning these
failures:

# You can safely ignore the errors if you're never going to use the
# broken functionality (recno databases with a modified bval).
# Otherwise you'll have to upgrade your DB library.I wont be showing you how to upgrade your
Berkeley DB library, but bug reports concerning this have been filed both at OpenDarwin.org and
Apple (#2923078 and #3021743). The make test took about ten minutes on my G4 and finished by
saying: Failed 2 test scripts out of 657, 99.70% okay.

And now, the final step:

[cpu:~] user% sudo make installIf you ran the first Configure command above, youll see a
progression of files installing into /Library/Perl (the Perl modules), /usr/local/bin (the Perl binaries)
and /usr/local/share/man/ (Perl-related documentation). This should finish in less than five minutes.

Testing Your Installation with CPAN


Now that youve installed the new Perl, its easy enough to make sure it worked with perl -v (you
can also check the configuration with perl -V). But a stronger, more certifiable way to test it is by
running one of the more common Perl programs: CPAN, the Comprehensive Perl Archive Network.
As its name suggests, CPAN is a huge repository of Perl code, and also the primary means of
installing and updating Perl modules.

Modules have been written for every conceivable purpose, from interfacing with database
applications to rendering graphics to parsing various types of specialized files. Fresh installations of
Perl come with a number already installed; you can find them by browsing through the /Library/Perl
directory. But if none of the included modules meet your needs, then CPAN is your next step.

Installing a new Perl module by hand, while not extraordinarily difficult, can be a bit of a hassle. It
involves downloading and unpacking the module, configuring it, and building it, all the while
making sure there are no unresolved dependencies or conflicts between modules. If youre the sort
of person who enjoys that kind of process, go right ahead. There is an easier way to go, however.

CPAN.pm is a Perl module whose purpose is to automate the downloading and building of modules.
It takes care of the tricky situations where certain modules require other modules to run, and those
modules in turn require additional modules, and so forth. It also supports the building and
installation of bundles, which are groups of related modules that work together to handle a
particular set of functions.

To start CPAN, enter the following:

[cpu:~] user% sudo perl -MCPAN -eshellIf this is the first time youre running the module, youll
be asked a series of questions about how your system is set up. In most instances, the default
answers will work, but make sure that the network setup questions are answered correctly so that
CPAN.pm can get online and properly fetch new modules. The answers you give will be saved in
~/.cpan/CPAN/MyConfig.pm. If your setup changes, or you think you answered a prompt
incorrectly, you can change your settings by opening that file with a text editor and modifying the
appropriate lines.

Eventually, youll see something like this:

cpan shell -- CPAN exploration and modules installation (v1.61)


ReadLine support available (try 'install Bundle::CPAN')

cpan>Go ahead and install this Bundle::CPAN thing. Perl modules that are closely related to each
other (like all Digest modules, all Net modules, etc.) are packaged into bundles, creating quick
combined installs. To install Bundle::CPAN, just use this command:

cpan> install Bundle::CPANUltimately, thats all you need to know about installing modules for
your shiny new Perl (and any previous version of Perl, actually). Weve both verified that our Perl
5.8.1 installation is working smoothly and improved our CPAN capabilities. As the above command
executes, youll notice CPAN downloading, extracting, testing, and installing modules and their
prerequisites.

Another useful CPAN command is simply r. Once run, CPAN will search through your Perl
modules, comparing your installed versions with the latest versions available. When finished, itll
show you a compiled list:

cpan> r
Package namespace installed latest in CPAN file
Math::BigFloat 1.35 1.36 T/TE/TELS/math/Math-BigInt-1.61.tar.gz
Math::BigRat 0.07 0.08 T/TE/TELS/math/Math-BigRat-0.08.tar.gz
Pod::Text 2.2 2.21 R/RR/RRA/podlators-1.24.tar.gzTo get up to date, its a simple
matter of issuing install Math::BigFloat, install Math::BigRat, etc.

Another useful CPAN command is force, which will unconditionally execute a command,
regardless of other results. If you recall the caveats at the beginning of the article, modules that have
been built with XS or C code under 5.6.0 wont work under 5.8.1, forcing you to recompile and
reinstall. But what if you already have the latest version of, say, HTML::Parser? CPAN complains:

cpan> install HTML::Parser


HTML::Parser is up to date.Thats where force comes in. Simply prepend it to any CPAN command
and itll execute unconditionally (i.e., whether the module is already installed, tests fail, prerequisite
libraries cant be found, etc.):

cpan> force install HTML::Parser


Running install for module HTML::Parser
.
.
.Web Applications
The most common use of Perl, and one of its strong points, is CGI: processing and outputting data
for Web applications.

Before you can use Perl for CGI scripts, you need to start the Apache Web server. If you havent
done so already, go into the Sharing pane under System Preferences and click the button to turn
Web Sharing on. After a few seconds, Apache will start running in the background. You can
abra
mm
abra
mm
abra
mm

You might also like