Dekan's Solace

Perl Tips

I've created this documentation because I've been programming with Perl constantly and extensively for the past four years. wish to help up-and-coming Perl programmers achieve their finest.

The vitals

use strict;

Always, always, always, in all of your code, always use strict;.

You use this pragma by having a line at the top of your code:

use strict;

This module enforces prepending data types with their proper labeling; this means prepending scalar names with $, arrays with @, and hashes with %. It also enforces scoping all of your variables using the my or local operator.

You will find that good distributed Perl code includes the use of this pragma.

Read up more about this in perldoc strict.

the -w flag

Always, always, always run your Perl code under perl's -w flag.

You can do this for scripts which load themselves with a #! line at the top by making it look like this:

#!/usr/bin/perl -w

Of course, your path to perl might vary. This flag will catch a lot of mistakes; one popular one is catching the use of a single 'equals' operator, =, inside of an if statement, where you almost always want a 'double equals' operator, ==. You'll find this an invaluable timesaver.

You will find that good distributed Perl code runs with this flag.

Read up more about perl's run-time flags such as -w in perldoc perlrun.

Really useful documentation

Perl function documentation

To access help about Perl functions, use perldoc perlfunc. To access help about a specific Perl function, use:

perldoc -f function

I even create an alias func to perldoc -f because I use this so often.

Miscellaneous Perl documentation

Running perldoc perl will give you a listing of all the other wonderful documentation perl comes with.

Specifically useful documentation

Some of this stuff is in the camel book, Programming Perl, but it might be updated with your Perl distribution.

perldoc perlfaq
Answers to common questions
perldoc perlmodlib
Descriptions of standard Perl modules, and how to create and find others
perldoc perlsec
Writing safe Perl code
perldoc perltrap
Traps that the unwary and those coming from other languages encounter
perldoc perlstyle
Perl style guidelines

Really useful standard modules

use Fatal;

Use this module to catch any calls and die if they return false. This is especially handy to catch system calls that fail such as:

open FILE $filename;


chdir $directory;

if they return false, so you don't have to bother writing statements like

open FILE $filename or die $OS_ERROR;

all the time.

For more information, see perldoc Fatal.

use diagnostics;

This module is a beauty. If you are having compile errors that are giving you short error messages that you don't understand, use this module to give you a much more verbose output. Note, however, that including this module slows down compilation considerably; it's advisable to only put it in when you have to.

Read more in perldoc diagnostics.

use IO::File; and use DirHandle;

These modules give you a better grip on file and directory handles by putting them inside a scalar (a $ variable). This is instead of having them 'hang free' in variable "globs", which you create when you make a call such as:

open FILE $filename;

When you use these modules, you can make nice-looking calls such as:

my $handle = new IO::File $filename;

Using these will help you lexically scope the namespace of your filehandles, and make it more understandable when you pass filehandles to functions.

You can get more documentation on these modules with perldoc IO::File and perldoc DirHandle.

use English;

This module 'translates' the awful looking and impossible to remember Perl global variables such as

into the much more readable variables:

I hope no further persuasion is necessary :)

Read up about these in perldoc English or perldoc perlvar.

Really useful CPAN modules

use Class::MethodMaker;

There is so many good things I can say about this module; it totally revamped my Perl object-oriented programming style. Basically, this package writes your methods for you; it can create data-member get/set methods, constructors, initialization methods, and more. This module is similar to Class::Struct but twenty times more powerful and nice to use. It is available from CPAN.

Really useful books

Programming Perl

This is considered to be the definitive reference book for Perl.

Perl Cookbook

This book is a compilation of extremely useful and well-documented Perl routines. If you're wondering "How can I do this well?", this is the book to reference.

Object Oriented Perl

This book filled in large gaping gaps in my Perl knowledge concerning object-oriented programming. It revamped my approach to module creation. This book is often overlooked by others as it is not published by O'Reilly, but I consider it a must for anyone who is serious about Perl.

This page has been translated into Spanish language by Maria Ramos from