Databases Reference
In-Depth Information
You can now run your program from the Linux or Mac OS X command line by typing
its name:
$ ./HelloWorld.pl
Hello,
world!
1 2 3
Most Linux distributions and Mac OS X do not look for programs in the current di-
rectory, so the the initial dot and slash ( ./ ) is needed to tell the operating system where
to find the program file. Windows doesn't use the shebang line, but to improve the
portability of your scripts, it's good to include a line such as #!/usr/bin/perl at the top
of any scripts you write. You should follow the instructions of “Installing Perl modules
under Windows” in Chapter 2 and associate your Perl interpreter with the .pl extension.
Windows always looks for the program file in the current directory, so you can simply
type:
C:\> HelloWorld.pl
Hello,
world!
1 2 3
Congratulations! You've just written and executed your first Perl script.
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;
 
Search WWH ::




Custom Search