27 Jun 2011

Comment your code with Smart::Comments

Normally we comment our code so that humans (including ourselves) can
understand it later on. However with ``Smart::Comments'' it becomes
possible to have comments which are not only useful in comprehending our
code, but also in *debugging* our code.

With ``Smart::Comments'' enabled, when a special comment sequence is
seen (``###'' by default) debugging output is produced:

use Smart::Comments;

### [] Running backups...

my $backup_dir = get_backup_host();

### Backing up files to: $backup_dir

foreach my $file (@manifest) { ### Copying files... % done
backup($file, $backup_dir);
}

### [] Backup complete...

With ``Smart::Comments'' enabled, output similar to the following would
be seen:

### [Fri Feb 25 12:14:34 2011] Running backups...

### Backing up files to: '/mnt/backup'

Copying files........ 64% done

### [Fri Feb 25 12:17:57 2011] Done...

One of great advantages of ``Smart::Comments'' is that the ``use
Smart::Comments'' line can simply be removed (or commented away) when
done, and all our smart comments are then simply ignored by Perl.
There's no requirement that ``Smart::Comments'' be installed on the
production systems, and no risk that that it may interfere with your
code.

You can read more about ``Smart::Comments'' on the
CPAN .

26 Jun 2011

local::lib

Work with local modules via local::lib

``local::lib'' helps solve the problem when a Perl package on your
machine isn't the version you need it to be. It allows you to create
your own directory of Perl packages into your own, or your team's
directory.

use local::lib;

# or

use local::lib '/path/to/team/directory';

``local::lib'' also sets up some useful shell commands which add our
preferred include directories to ``PERL5LIB'' in our environment. If our
code is not running under taint, we don't even need to add the ``use''
line to our code to use these newly installed modules.

App::perlbrew

Experimenting with new versions of Perl and modules

Many developers have a chicken-and-egg problem when it comes to
upgrading their version of Perl. On one hand, the system version of Perl
cannot be upgraded because it might break existing scripts, on the the
other hand it has been difficult to install your own version of Perl to
see whether an upgrade is safe. The same happens with modules.


Pick your Perl version with App::perlbrew

Use ``App::perlbrew'' to install and switch between multiple versions of
Perl. ``perlbrew'' is easy to install, even if you don't have system
administration privileges, and very easy to use. Once you've installed
and initialised it, you can install whichever version of Perl you wish
to use.

perlbrew install perl-5.12.2
perlbrew install perl-5.10.1

``perlbrew'' will then fetch, compile and install that version of Perl
in your home directory (or other nominated location) and you then you
can just start using that version (``perlbrew'' will do all the path
magic required to make this your default version of Perl).

If you have more than one version of Perl installed by ``perlbrew'' you
can list the verions out:

perlbrew list

and switch between them:

perlbrew switch perl-5.12.2

You can also ask ``perlbrew'' to switch itself off so that you can go
back to using the system Perl.

perlbrew off

Maintain your own CPAN with CPAN::Mini

If you wish to have a portable, off-line CPAN mirror, or wish to maintain the state of CPAN exactly as it was at a particular date, then ``CPAN::Mini'' is the way to go:

cpanmini -l ~/local-cpan \
-r http://search.cpan.org/CPAN

We can even use this with ``cpanm'':

cpanm --mirror ~/local-cpan --mirror-only Test::Most

``cpanmini'' only includes the most recent versions of the CPAN modules
(rather than their whole history) and as a consequence is only a few
Gigabytes big.