Scripting With Perl

Let’s examine the first line of Perl that you wrote earlier:

print "Hello, world!\n";

The print command or function takes the text in the quotes (known as a string of characters) and displays it. Be sure to put a semicolon at the end of each Perl statement; if you forget one, it gets quite confused and prints error messages that can in turn confuse you!

You’ve probably noticed already that the \n and \t weren’t printed on the screen. The backslash indicates an escape character that should be handled in a special way. A \n indicates that a new line should be started at this point. Similarly, a \t tells Perl to jump ahead to the next tab stop, which is useful if you want to show columns of information. Note that the print command doesn’t insert any line breaks on its own, even when a program finishes; you have to tell it to do so explicitly through \n.

A program that prints out exactly what we’ve written isn’t very exciting. Perl, like most programming languages, allows us to use placeholders, or variables, to store values; we can manipulate these variables and then display them. For example, we can define a variable called $TemperatureToday to store today’s temperature:

my $TemperatureToday;

The keyword my is used to declare the variable for the first time. Variables that contain a single value are known as scalar variables and are identified with a dollar ($) symbol. We’ll discuss other types of variables later in this chapter. We can assign a value to this variable; ...

Get Learning MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.