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.
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
myor
localoperator.
You will find that good distributed Perl code includes the use of this pragma.
Read up more about this in perldoc strict.
Always, always, always run your Perl code under perl's -wflag.
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
perlmight vary. This flag will catch a lot of mistakes; one
popular one is catching the use of a single 'equals' operator,
=, inside of an
ifstatement, 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
-win
perldoc perlrun.
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 functo perldoc -fbecause I use this so often.
Running perldoc perlwill give you a listing of all the other wonderful documentation perl comes with.
Some of this stuff is in the camel book, Programming Perl, but it might be updated with your Perl distribution.
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;
or
chdir
$directory;
if they return false, so you don't have to bother writing statements like
open FILE
$filenameor die $OS_ERROR;
all the time.
For more information, see perldoc Fatal.
use
diagnostics;
This module is a beauty. If 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::Fileand 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:
$INPUT_RECORD_SEPARATOR
$CHILD_ERROR
$OS_ERROR
I hope no further persuasion is necessary :)
Read up about these in perldoc Englishor perldoc perlvar.
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.
This is considered to be the definitive reference book for Perl.
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.
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.