Learn Perl with this easy Perl tutorial.

Perl is a programming language used for scripting.  It is one of the easiest languages to learn and use, and it's also one of the most powerful and versatile languages.  Most of the dynamic content that you see on the internet (message boards, etc) is written in Perl.  I know Perl pretty well, and everything I know, I learned from the internet, mostly perldoc.com.  My little tutorial will show you how to get up and running, and some of the basics; I recommend you proceed to perldoc after that.

If you're using Windows, then you can run Perl programs by installing ActivePerl which is free.  (And you should also make sure that your file extensions are turned on.)  If you're using Linux, you already have Perl built-in

Once you have some form of Perl installed, whether on Windows or Linux, you can run perl scripts by just typing perl myscript.pl at a Command Prompt or shell prompt.




Here's a little perl script that shows the format of a script and how to print output to the screen.  Type this into a text editor like Notepad:

#!/usr/bin/perl

# All perl scripts on Unix/Linux start with a line like the one above.  If you
# are using ActivePerl on Windows, it will be different... it will look like
#!c:\Perl\bin\perl.exe
# 
# Also, note that these are "comments," because they are behind a pound sign.

use strict;  # Put this in every script you write, for good measure.

print "Just another Perl h4x0r.\n"; # \n makes a new line (like pressing
                                    # Enter), as in other languages.

Now save that file as myscript.cgi or myscript.pl -- perl scripts end in either a cgi or a pl (that's p L, not p one) extension.  Then open up a Command Prompt or shell prompt and type perl myscript.cgi to run the script.




This script shows how to use variables and do some comparisons:

#!/usr/bin/perl
use strict;

my $name = "Anthony";
my $friend = "anthony";
if($name eq $friend)
{
	print "$name is the same as $friend.";
}
elsif( lc($name) eq lc($friend) )
{
	print "$name and $friend are the same except different case.";
}
else
{
	print "$name is not the same as $friend.";
}

if( !(5 == 5) )
{
	print "Silly rabbit!";
}

eq tests whether two strings are equal.  (To test whether two numbers are equal, use == instead.)  In the example, $name is different from $friend because A is different from a.  So the second print statement is the one that will get executed.  That's because lc() converts the variables to lowercase before comparing them, and they are equal when they're both all lowercase.

The last if() tests whether 5 is NOT equal to 5.  That's what the exclamation point means -- it means do{whatever} if the if() statement is NOT true.  Since 5 equals 5, the print statement will not execute.




This script will write some text to a file, then read from the file, and print some of it to the screen:

#!/usr/bin/perl
use strict;

my $file = "testfile.txt";

# Note that for output, the open command uses a greater-than sign.
# Using just one greater-than will erase the contents of the
# file before opening it, if the file already exists.  Use
# two (>>) if you want to append your data to the end of an
# already-existing file.
open(OUT,">$file") or die "$0: couldn't open the file $file: $!\n";
flock OUT, 2;      # lock the file so that no other scripts will open
seek OUT, 0, 0;    # until we're done with it.  And seek to the beginning
                   # of the file.

print OUT ("This is just some text that I'm writing into
a file on my computer.  So I'll type this boring nonsense
and then we'll see what happens, ok?");

close OUT or die "$0: couldn't close $file: $!\n";

# Note that for input, the open command uses a less-than sign.
open(IN,"<$file") or die "$0: couldn't open $file: $!\n";
flock IN, 2;
seek IN, 0, 0;

# This while() loop will go through the file one line at a time,
# storing the line in the variable $_ each time the loop starts.
while(<IN>)
{
	if($_ =~ /w/) # If the current line contains the letter w...
	{
		print $_; # ...then print the line.
	}
}

close IN or die "$0: couldn't close $file: $!\n";

Printing to a file is just like printing to the screen, except you must put the filehandle (OUT, in this example) right after the word print.  Note the "or die..." statements: they tell the script to die (that is, exit) if the previous command fails.  If so, the message in quotes will be displayed.  $0 (that's $ zero) contains the name of the running script, and $! contains the error message.

Note especially the line if($_ =~ /w/).  That if( ) contains a regular expression, which is the heart of Perl's power and glory.  A regular expression is a statement that lets you test whether a variable or text string matches a given pattern -- in this case, the pattern is simply the letter w.  Regular expressions can (and do) get much more complex and powerful than this; they can contain lots of symbols for various kinds of matching, they can extract text from a match and store it in special variables for you, they can change the text within a string... Once you master regular expressions, you'll be a Perl master.  Perldoc's regular expressions quick-start is the place to learn them.




Learn More.

Now you have the basics down, and as you can see, it's pretty easy even to do powerful things like reading and writing files.  To learn more:

  • Visit perldoc.com and go to the FAQ section.  There you will learn almost everything you'll ever want to know about Perl (I did).  Start with perlfaq4 and read straight through to perlfaq9 (or just refer to them when you need them).  Also, go to the POD section to read more in-depth tutorials on specific issues.  Everything on perldoc.com is written in a straightforward (and often humorous) manner, and it's heavily example-based.

  • Visit my other Perl page where you can see some tricks and tips.

  • Poke thorugh my various webmaster scripts to see how things are done.
  • Contact me if you'd like help with anything.