Free Perl Tutorials

PERL Basics

I don't want to spend a ton of time on the basics of PERL, as they are about the easiest thing you'll every program, but here are some easy program examples to get you started, and important, yet simple, functions you'll use quite often

PERL print function
Probably the most widely used function in all of PERL programming - at the beginner level anyways. "print" (note: the lowercase p in print, NOT an uppercase P because PERL is case sensitive) displays whatever you want to on your screen. You can print text using the print command like so:

print "My first line of PERL code! YIPPEE!";

You can use print to display full strings like so:
$first = "My first line of PERL code! YIPPEE!";
print $first;

You can print almost anything, as you'll soon see as you progress through the tutorial!

PERL Variables
What is a variable? A variable is a value that can assume any quantity of information. PERL has mutliple types of variables called strings, arrrays, and hashes. Each has its own method of assigning a value to a varaible:

PERL String Variables
String variables in PERL are denoted with the "$" preceding whatever terminology one would like to assign to a variable. The important thing to remember about a string type variable is that it assigns a single value to its entire quantity. Say for example you assign a sentence to a string:

$example = "This is an example of a string variable";


This makes $example equal to This is an example of a string variable

String variables can be pretty much anything: numbers, single word, sentences, file names & extensions, etc.

What if we wanted PERL to look at a longer list of data, and look at each piece of data in that individually as opposed to a sting that just bunches everything together?

PERL Array Variables
Arrays allow you to look at each individual group of information within a piece of data. You can pull individual strings from individual pieces of data. The array variable is denoted with "@" preceding the variable name. Here is an example of an array:


@array = ("earth", "wind", "fire");


Arrays are a great way for oranizing data. You can call individual data from this and set it equal to a particular string to pull individual pieces of the data from the array, merely by selecting the location of the data within the array. The important thing to remember about arrays in PERL is that they begin counting with zero. In other words, if you coun't the above items in the example @array, earth would be in position 0, wind would be in position 1, and fire would be in position 2!

Let's check out some code on how to pick apart this little array


@array = ("earth", "wind", "fire");
$array = @array[1];
print $array;


The "[1]" in our above example sets the string "$array" equal to the 2nd item in the array (remember we start counting arrays from zero). This program would return "wind"!

A quick side note. An easier format for creating arrays would be to use the qw function perl provides. It takes the work out of having to add the commas and quotes around each word. In other words, this:


@array = ("earth", "wind", "fire");


Is the exact same as this:


@array = qw(earth wind fire);


It'll start to save you time in the long run!

PERL Hash Variables
So now that we know what a string variable is and what an array is, we'll look at what a hash is! First, a hash variable is denoted with a "%" sign in front of the variable name. Here is an example of a hash in PERL:

%myhash = ("hey", 1, "my", 2, "first", 3, "hash", 4)

A hash operates pretty much the exact same as an array, the biggest difference is that we can't reference a value by index number in a hash as we can in an array.
You call the values from a hash in a differnt fashion. Rather than displaying the information here, I found an excellent video that describes how to do it in detail!



I hope this helps your basic understanding of PERL!