Beginning CGI Programming with Perl

Learning Perl

Exercise 1.2. Using the scalar variable

In this exercise, you’ll learn about Perl’s simplest variable: the scalar variable. It’s kind of like the shoe box. You can use it effectively to hold all kinds of data, numbers (usually referred to as numeric data), and text (usually referred to as strings or character data). Listing 1.1 takes the Hello World example and personalizes it a bit.

Listing 1.1. Personalizing the Hello World example.

1:  #!/usr/bin/local/perl2:  $first_name = "Eric";3:  $middle_initial = "C";4:  $last_name = "Herrmann";5:6: print "Hello World\n";7: print "This program was written by ";8: print "$first_name $middle_initial $last_name\n";

Now take a moment to examine this program. Lines 2-4 are called assignment statements. The data on the right-hand side of the equal sign (=) is stored in the variable on the left-hand side, just like a shoe box. A variable is created in Perl the first time something is stored in it. The variables in lines 2-4 are called scalar variables because only one thing can be stored in them at a time.

You can store two basic types of data in scalar variables: numbers and text. As described earlier, text data usually is referred to as strings or character data. Numbers, luckily, are referred to as numbers or numeric data. Text data should be surrounded by quotation marks (single quotes or double quotes), as shown in lines 2-4. You learn about the mysteries of the different types of quotation marks in Tutorial 4. For the moment, accept that your string data-the stuff you’re going to put into your shoe box-must have double quotation marks around it.


When using quotation marks to store data, you must begin and end with the same types of quotation marks, and they always must be in matching pairs. The first quotation mark defines where the data to be stored starts, and the second quotation mark defines where the data to be stored stops.

On line 8, the data you stored earlier is printed. This is just a simple look inside the shoe box to show you that the data is still there. Before you go on, take a close look at the variable names in Listing 1.1. Perl is very sensitive about spelling and uppercase versus lowercase letters. When dealing with variables, $First_Name is not the same shoe box as $first_name, or any other mixing of upper- and lowercase letters. Think of Perl’s case sensitivity as different sizes on a shoe box. The box looks similar, but what’s inside is different.


You should establish a style of naming variables that you’re comfortable with and then stick with that style. Different people like different styles, but the most important thing is to use the same style throughout your program. It makes your program easier to read, and it makes your variables easier to find. Here are several styles that are common:

All uppercase: $FIRSTNAME

All lowercase: $firstname

Placing an underscore between words:
$first_name, $FIRST_NAME, $First_Name

Starting each word with an uppercase letter:

Pick a style that you like and use it consistently in your programs. It’s a very simple thing you can do to help keep errors from creeping into your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *