Welcome to the wonderful world of Perl! Today, you will learn how to make a sample program, called "hello.cgi".
The first thing that you should learn about Perl is the way it stores information. There are three basic ways that you
can store information using Perl; the first and easiest is in strings. A string is any one set of alphanumeric characters
(letters and numbers and punctuation). Here are a few examples of strings:
Here are a few examples of illegal strings:
These are the rules for naming strings:
Now that you know how to name a string, you need to know how to store data into it.
Here are examples:
The basic syntax for storing data into a string is:
Pretty easy, isn't it?
You'll notice that there is a semicolon (;) following every statement. This is because that for perl to know when one command ends and another begins, you have to tell it by putting a semicolon there. There are a few exceptions, but you'll learn about them later.
One neat thing about strings is that you can put text or numbers into it! If you say $hello = "Hello"; then it'll have text in it. If you say $test = 2; then it'll have the number 2 in it. You can also store one string into another. In the examples above, you saw this:
$yellow = "orange";
Now $yellow as well as $yellow2 has "orange" stored in it.
Sometimes, you may want to put two strings together. Lets say that you have one string with "Joe" and another with "Superman", and you wanted to form one string that says "Joe is Superman." You would do this by doing something like this:
$sentence = $name . " is " . $name2;
Now $sentence contains "Joe is Superman". The period (.) is called the string concatenation operator when it is used like this.
If you have stored a number into a string, then you can apply almost any math function to it. Look at this program and see it you can figure out what the output will be:
$test = 2;
The output would look like this:
Instead of all of the separate print statements, you could use something like this:
print "$test \n $grapes \n $bananas\n";
You can use variables inside of quotes, if you like. The advantage of this is that you can easily put other text right beside the variable.
This would output:
Mr. Sam is a good citizen.
There is another way to print text.
print 'Mr. Sam is a good citizen.';
This prints what you would expect, but what about this?
print '$person is a good citizen.';
$person is a good citizen.
The single quotes don't change the variables! They print the string as it is inside of the two single quotes.
This may not make very much sense right now, but as you get more advanced, you will learn the reason behind this.
Here is a table of several different math operations that perl understands.
Here is a sample program that you can write:
That's it! There's your first program.
In the first line you'll notice that there is an odd command, "#!/usr/local/bin/perl", all this does is it tells the operating system how to run the script.
---Note: If you are not using a Unix system, you will have to install Perl onto your system.---