Perl 5: Learning to Read and Write
Let's say you walk into work one Monday morning ready to continue your work as a page layout designer for the upscale Stitch Magazine. As you take your first sip of coffee your phone rings and it's the president of the company on the other line. He starts rambling on and on about some brainstorm that he and the upper management team had over the weekend about taking the magazine and putting it up on the Web for millions to see!
You start to tell him, "Uh sir, are you sure you dialed the right extension? I really don't know how to do that." So, of course, he tells you to figure it out. How hard can it be? For the rest of the week you study the wonderful and challenging world of HTML and page layout design with such books as Essential Photoshop 5 for Web Professionals, and learn the intricacies of setting up a Web site for Stitch.
After you've spent a couple of weeks learning HTML and designing an online version of your company's magazine, you have a pretty nice-looking site and decide to call your boss and brag about it. When he gets back to you he says, "Great, great! But we need more! We need to have a user response form, a message board, a place where people can vote on questions posed in articles, search engines, monkeys flying across the screen and juggling the images around." You tell him you'll jump right on it, knowing that bonuses are just around the corner.
Being one of those brilliant people, you pick up this book, Essential Perl 5 for Web Professionals, and thumb through it. This book will teach you how to do everything your boss wants you to do to the company's site and more (with the exception of his reference to monkeys, but you think you'll figure out what the heck he was talking about later).
To start this chapter off, you're going to have to learn a few functions. After you learn some of these you will be able to work with the scripts that follow.
New Features
print is used for exactly what it says, to print information to STDOUT (standard output) or an alternate output. This can be what is printed to the screen, a file, or another program.
Variables
With Perl there are only three types of variables, unlike other languages, which may have many types.
- Scalar
- Array
- Associative array
A scalar is a single value that is assigned information, whether it be an integer or a sentence. A scalar variable starts with a $ to distinguish itself from the other types of variables such as the array, which uses a @, or the associative array, which uses a %.
Syntax
$someValue = 5; $someName = "This sentence is assigned to the variable to the left";
Quotes
There are two types of quotes you will be dealing with: single ('') and double ("").
Single quotes
These are very literal quotes. If you would like to print out a variable and you put it between single quotes, the actual variable name will be printed instead of the actual value.
Syntax
print 'I'm thinking of the number: $number';Result: I'm thinking of the number: $number
What you see between the single quotes is what you getWYSIWYG.
Double quotes
Double quotes are a little friendlier than the single quotes in that they aren't quite as literal. If the scalar variable $number were assigned the value 5, here's what the result would look like:
Syntax
print "I'm thinking of the number: $number";Result: I'm thinking of the number: 5
Double quotes will also allow you to use special characters like the newline character \n to start a new line, whereas single quotes will actually just print \n.
NOTE
You probably noticed the semicolon (;) at the end of some of these lines. This is to let Perl know that the end of that statement has been reached and it's time to start a new one.
open
The open function allows you to open a file to read, write, or append to. You can also use this function to open a process such as sendmail through a pipe (|), which you will get a look at in the next chapter.
Syntax
Opening a file:
open(FILEHANDLE, "FILENAME") ;
Opening a process:
open(FILEHANDLE, "|/usr/sbin/sendmail");
FILEHANDLES
FILEHANDLES are used as unique identifiers, or labels, when accessing other files when opening, closing, editing, or doing anything with those files. Their names usually appear in uppercase letters; this is not necessary, as they will work in lowercase as well. As a proper rule, however, you should use uppercase.
Syntax
open(ARTICLE1, ">article1.txt");
When referring to article1.txt you will use the ARTICLE1 as the FILEHANDLE.
Syntax
print ARTICLE1 "this will be added to article1.txt";
NOTE
You can give FILEHANDLEs any name you wish, other than those used by the Perl language.
close
Whenever you open a file or a process, it is usually good practice to close it when you are finished using it. This is not required, but not doing so could produce some problems.
Syntax
Closing a file:
open(FILEHANDLE, "FILENAME") ; close(FILEHANDLE);
Closing a process:
open(FILEHANDLE, "|/usr/sbin/sendmail"); close(FILEHANDLE);
while
With the while statement, we take a variable that has earlier been assigned a value and test it to see whether it is true or not. This is called the condition. If the condition of the variable is true, then the statement will be executed. If the condition of the variable is false, then the statement after the while will be ignored and we move on to the next part of the code.
Syntax
while(condition) { ( statements ); }
The special $_ variable
Perl has a special variable for use in many different operations, such as the current line of a loop that is reading the contents of a FILEHANDLE, or a number of regular expression matches. In the case of a loop (while, foreach, and so forth), Perl automatically copies the current line being read into $_. This can save a lot of typing when using other operations when one or more parameters are $_. You will learn how we use $_ in the many examples in this book.
Escape Sequences
These are sequences of characters that consist of a backslash (\) followed by one or more characters that perform certain duties, as shown in Table 11. The most common one you will see is \n, which formats a new line. When you use the backslash, the character that follows it performs some action. If you want to actually print the character \, then you simply throw a \ in front of that: \\.
TABLE 11 Escape Sequences
Escape Sequence |
Description |
\a |
Bell or a beep |
\b |
Backspace |
\e |
Escape |
\f |
Form feed |
\l |
Force the next character into lowercase |
\L |
Force all following characters into lowercase |
\n |
Newline |
\r |
Carriage return |
\t |
Tab |
\u |
Force the next character into uppercase |
\U |
Force all following characters into uppercase |
\v |
Vertical tab |