Jay Shirley

I'm building an app to form great habits and achieve your dreams.

I hope you'll give it a try.

The Daily Practice

Getting started with Perl.

This is an opinionated guide to getting started with Perl. This is what I, and many others I work with, generally use. It works very well. This also only covers Unix-like systems. For Windows, well, you’re on your own.

Supply your own Perl.

First off, don’t trust the Perl (or any other compiler/interpreter) that ships with your OS, unless it’s core (like Xcode). Fortunately, Perl makes it very easy to build, manage and maintain your very own perl installations. It does this with perlbrew.

Make sure that you have proper build tools (gcc, GNU Make). If you’re running Debian or Ubuntu, simply install the build-essential package.

Open a terminal and run this (as your user, no sudo’ing here):

curl -kL http://xrl.us/perlbrewinstall | bash

This will install and initialize perlbrew for you. Make sure to add the .bashrc entry as displayed in the output.

echo source ~/perl5/perlbrew/etc/bashrc >> $HOME/.bashrc

(Modify if you aren’t using bash)

It is best at this point to logout and login, to make sure the environment is correct. Alternatively, you can just run:

source ~/perl5/perlbrew/etc/bashrc

Now your environment is correct it is time to install your very own, independent and completely safe perl. First, find out what Perl versions are available:

$ perlbrew available
   perl-5.15.4
 i perl-5.14.2
   perl-5.12.4

The “i” indicates what is installed. In Perl, even number point releases (the 14 in 5.14.2) mean a stable release. An odd (5.15.4) is introducing new features. When they are adequately tested, 5.16 will be released. I recommend taking the highest even number point release, in this case, perl-5.14.2 (which I have installed).

To install, simply run:

perlbrew install perl-5.14.2
perlbrew switch perl-5.14.2

After this, you have your very own Perl! If you ever want to get rid of it and reclaim your disk space, it’s easy. Everything is in ~/perl5. Just remove it!

Verify now that the perl is in your own home directory and the version you requested with:

which perl
perl -v

If those look correct, continue onwards!

Use the CPAN

The best part of Perl is the exhaustive library on the CPAN. The best way to install modules from CPAN is using cpanminus.

Once you’ve got perlbrew setup, you’ll install cpanminus in a similar fashion:

curl -kL http://cpanmin.us | perl - App::cpanminus

This will install the cpanm program and make it available to use (again, in your ~/perl5 directory. Everything is self-contained).

After this, it’s a good idea to start with a test install. Some modules do a lot and are quite big. Others are small. Lets start with a small but useful module:

cpanm Try::Tiny

This module gives very simple exception handling. It should succeed. After that, install what makes Perl really special, Moose.

cpanm Moose

Moose is an API for object oriented code, essentially. It makes writing OO code easy and fun. Once that is done, you can start hacking on Perl.

To see a very simple Moose program, there are a series of guides available as part of the Moose distribution:

As you can see from just those 3 examples, Moose and Perl are quite powerful and expressive. It gives you easy class creation and management, type constraints and an entire meta-object API.

Why bother with Lisp when you have Moose?