Lesson 3 - Print Statement
Print

Basic Output is the most important part of any language. Without a print statement, you would never be able to communicate
with the user, other programs, or even log files.

In this topic we will discuss the use of the print statement in PERL. You will learn how to write a basic "Hello, World"
program, and how to write data out to users. You will also learn the specific's of writing data to a web browser. The last
part of this lesson will teach how to escape characters, and which characters need escaping.

A sample print statement to print out "modus operandi" looks like this:
print "modus operandi";

Let's examine the statement. The command portion of the statement, "print," lets perl know that we want to print something.
Next we have the switches for the print command; print expects to be given an output device and then text to output. In our
case, we left the output device blank since it is optional and defaults to STDOUT (which is where we want it anyway) and put
the data we want outputted in quotes.

Without the quotes around our data, perl will not know where to stop outputting and therefore miss the semicolon which tells
perl the statement is completed.

You've now learned how to output data to STDOUT. Lets take a look at write data to a web browser.

Web browsers needs certain headers in order to correctly display data. The only header we need to worry about is
"Content-Type." This header tells the browser how to render the data - as text, html, binary data, an image, etc.. The
browser expects to receive all of its headers, a series of two "\n" characters (newline characters), and then the data to
render.

Most of the time we will want to output our data as either plain text of HTML. Here is an example of standard output in HTML
format:

print "Content-Type: text/html\n\n";
print "<H1>modus operandi</H1>";

If you do not output the Content-Type header first, or you neglect to include two \n's after it, the server will return
Internal Server Error.

In the case that you would like to output to a file or some other device you can simply specify what you wish to write to in
the print command. To write to a file handle called "OUT" your print statement would appear as follows:

open (OUT, "testfile");
print OUT "modus operandi";
close(OUT);

In order to establish the OUT handle you need to use the open command first. This will be discussed in a later topic, for now
you can work with the open and close commands shown above.

The last topic we will discuss is escaping characters. For perl to know where to begin and end the data your are trying to
print, you must enclose it in quotes. However, what do you do if you want to print out a quote? You need to escape it. An
example of a command that will print out `"this is quoted"` follows:

print "\"this is quoted\"";

By printing a `\"` perl will print out a `"`. You can use escape sequences for other uses as well, `\t` will print a tab,
`\n` prints a newline character, `\\` prints a `\` character. Characters that need to be escaped are easily learned, they
are mostly just the variable declaration characters ($, @, %, etc):

print "$14.95";

In the above command perl will print out the contents of the string "$14" and then ".95". If you have not put any data into
$14, you will get the cryptic output of ".95".

In order to tell perl not to do this, you need to escape it. To print out "$14.95" instead of ".95" run the command like
this:

print "\$14.95";

Another option to escaping variables is to use `'` characters instead of `"` characters. By using single quotes like
`$14.95` perl will interpret the string literally and not replace any variables or escape characters like \n.

In this lesson you learned the basic use of the print statement in PERL, how to write data out to users and to a web browser.
You also learned how to escape characters.

 

Lesson 4 - Variables

Home | Lessons | Get Perl | Resources