PERL & CPAN Intro for Newbies

So you want to learn a unix scripting language that you'll be able to use in any situation? Perl is your answer !

This is a little intro to installing CPAN modules. If you don't know what CPAN is, check out http://search.cpan.org/. Basicly, it is a massive archive of perl libraries that will allow you to do pretty much anything. (Once I wrote a shoutcast-style streamer in perl with a web front end, automatic down sampling and ogg->mp3 conversion, but that is a whole other story.)

Using CPAN is easy.

-su-2.05b# perl -MCPAN -e shell

/usr/local/lib/perl5/5.8.2/CPAN/Config.pm initialized.

...

The -M flag tells perl to load a perl module. -MCPAN is the same as putting 'use CPAN;' in your script.

The -e flag tells perl to execute a command. In this case, the CPAN module exports a function called 'shell'.

When you first run the CPAN shell, it'll ask you a bunch of questions about where to find a mirror, and what tar program to use, etc. You can use defaults if you don't know the answer to some of the questions.

Once you are at the cpan> prompt, you can start issuing commands. '?' will list all the commands.

cpan> ?

Display Information
 command  argument          description
 a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
 i        WORD or /REGEXP/  about anything of above
 r        NONE              reinstall recommendations
 ls       AUTHOR            about files in the author's directory

Download, Test, Make, Install...
 get                        download
 make                       make (implies get)
 test      MODULES,         make test (implies make)
 install   DISTS, BUNDLES   make install (implies test)
 clean                      make clean
 look                       open subshell in these dists' directories
 readme                     display these dists' README files

Other
 h,?           display this menu       ! perl-code   eval a perl command
 o conf [opt]  set and query options   q             quit the cpan shell
 reload cpan   load CPAN.pm again      reload index  load newer indices
 autobundle    Snapshot                force cmd     unconditionally do cmd

Lets start with a simple search. Lets say I want to find a module that will let me encrypt and decrypt messages using a blowfish cipher. I would use one of the search commands 'a, b, d, m or i' (Author, Bundle, Distribution, Module or Any). Since I know I want a module, I'll use 'm'.

cpan> m /blowfish/
Module          Crypt::Blowfish (D/DP/DPARIS/Crypt-Blowfish-2.09.tar.gz)
Module          Crypt::Blowfish_PP (M/MA/MATTBM/Crypt-Blowfish_PP-1.12.tar.gz)
Module          Net::SSH::Perl::Cipher::Blowfish (D/DR/DROLSKY/Net-SSH-Perl-1.25.tar.gz)
3 items found

This tells me there are 3 modules with the name 'blowfish' in them. Crypt::Blowfish and Crypt::Blowfish_PP are one of the ones I want. But which one should I use?

By convention, some module that are written in pure perl (no extra binary code) have a _PP postfix. Pure Perl modules are often slower then their binary counterparts, but are often more compatible and easier to install as they have no other dependancies (not even a C compiler).

So I'll decide to go with the slower, easier to install version since it'll only be used to in my small program for encrypting and decrypting small amounts of data.

cpan> install Crypt::Blowfish_PP
Running install for module Crypt::Blowfish_PP
Running make for M/MA/MATTBM/Crypt-Blowfish_PP-1.12.tar.gz

...

Writing /usr/local/lib/perl/5.6.1/auto/Crypt/Blowfish_PP/.packlist
Appending installation info to /usr/local/lib/perl/5.6.1/perllocal.pod
  /usr/bin/make install  -- OK

It's all installed. Now what do I do?

I need to learn the API, so I'll go to http://search.cpan.org and search for the new module I just installed. It leads me to a nicely formatted manual-like page (http://search.cpan.org/~mattbm/Crypt-Blowfish\_PP-1.12/Blowfish_PP.pm\)

Now, I am not going to write a simple encrypt/decrypt tool for you (unless you really bug me to), needless to say, it's easy to do. Infact, less then 15 lines of code should do it.

The point is, CPAN is easy to use, the modules are often great, and most importantly, its fun because you can do alot with very little.