Starting with Perl

Getting Perl

You may already have perl -- if you're on a Unix, Linux, or BSD computer, then try typing perl -v

If you have a recent Linux distribution, try running your package manager or software manager, and seeing if Perl is on the list of things that you can install

For example, Debian users will have something like apt-get perl, while Mandrake will have rpmdrake

If you use MS-Windows, you can get an '' (ActivePerl), from the people at Activestate.

If you want to download Perl, you can get it from the main site at www.perl.com.

Running programs

Perl programs are just text files, to run them, they don't need compiling, you can just distribute them in source-code form, and they get run by the Perl interpreter

You can make this happen explicity by typing perl.com myscript.pl into a command-line, or implicitly by associating the file with the perl interpreter. On linux, you do this by putting #!/usr/bin/perl on the first line of your script, and make the file executable. (chmod u+x myscript.pl), while on Windows this is done automatically when you install ActivePerl

Writing programs

#!/usr/bin/perl
# Hash lines are comments
$DollarSymbols = "variables";
$VariablesCanHoldNumbers = 3.1415;
@AtSymbols = ("Arrays","or","lists");

foreach $Count (1..10)
{
  print "You can embed variables into strings, Count is $Count  \n";
}

# Try reading a file
open(INPUT, "<", "filename") || die("you can specify an exit message if the open() fails\n");
while($NextLine = <INPUT>)
{
	chomp $NextLine;  # to remove the newline character at the end
	if(substr($NextLine, -1) eq "?") # if line ends with a question mark
	{
		print "Line: $NextLine\n";
	}
}

Using Modules

Before you ever write a new program in Perl, go and search CPAN, the Complete Perl Archive Network -- chances are that something very similar already exists that you can use

If you end up with a program that requires one or more modules to run (they're listed with use ModuleName in the Perl script), you'll need to install modules too

In Linux, there're instructions for installing modules. On Windows, there's a tool called PPM (Perl Package Manager, just type ppm at a dos prompt), where you can type "install ModuleName" to install a module.

For simplicity, you may be able to do perl -MCPAN -e 'install ModuleName', which is the equivalent of PPM for unix-like systems

Learning more

As with all things, books are good. Anything written by Larry Wall is worth reading if you want to learn Perl (he wrote the language), as is anything published by O'Reilly on the subject. A great book for starting in the Perl Cookbook (amazon it), which includes lots of snippets of code that show you what you can do, and show you examples of elegant simple programs.

For help, you might try the Perl Mongers (guess the domain name), perldoc.com, the HTML documents installed in your computer's Perl directory, or just searching Google. If you want to convince your company to start using Perl, there's a "perl success stories" area on perl.com, and of course, there's a huge backlog of articles and tutorials on perl.com which are worth reading. Visit perl.com occassionally to read articles, especially during Advent (Dec-1 onwards)