Mason Firewall

This page printed from: https://www.linuxmonth.com/issue3/articles/perl/perl.html?print=1

Using Perl
by: Jeffrey A. Duffy
Regular Edition

Perl for the Absolute Newbie

Article 1: Running Your First Perl Program

Perl is a programming language usually associated with the Web and CGI programs, but is in fact a powerful general-purpose tool for systems administration, network programming, and a host of other uses. In this article, I'll begin a step-by-step tutorial in Perl, starting with your first Perl program.

Testing Perl

Before going any further, make sure that Perl is installed on your computer by entering the following incantation on your command line:

  perl -e 'print "Perl Works!\n"'

if this prints

  Perl Works!

then you're ready to move on. If not, either the Perl interpreter is not in your path, or you need to install perl. See the excellent perl.com download page for more information on getting Perl for your machine.

Our First Program

Let's begin by jumping right into our first Perl program, the ever-popular 'Hello World'. The source code for the program looks like this:

#!/usr/bin/perl -w

# print our greeting

print "Hello World!\n";
Type this program into a file called named hello.pl. Make the file executable using the command chmod +x hello.pl , and run it by typing ./hello.pl . You should see the message Hello World! appear on your console.

Three Easy Pieces

Now let's pick the program apart. The first line,

  #!/usr/bin/perl -w
is known as the shebang line. This tells the operating system how to deal with the file; in this case, it looks in the /usr/bin directory for a program named perl (the Perl interpreter), and gives the file to it for execution. The -w switch at the end tells the Perl interpreter to turn on verbose warnings, which you always want to do while you're learning Perl. Note that this must be the first line in your Perl program.

The next line,

  # print our greeting
is an example of a Perl comment. Comments start with the # character and are not run as part of the program per se, but give the programmer a way to document what the code is supposed to be doing.

The last line,

  print "Hello World!\n";
is the heart of this program. We start with the function print(), which takes a text string as its argument, and prints it out (usually to the console where the program was invoked). We give print() the string "Hello World!\n". We see here our first example of string interpolation, which is a fancy way of saying that we replace something in the string with something else that it represents.

In this case, the characters \n are replaced with a newline (the cursor goes to the next line after the string prints). There are other interpolated string sequences, including \t for a tab, \b for a backspace, and many more. Note that string interpolation only happens within double quotes; single quotes interpret the string literally, so they won't work.

The last thing that needs mentioning is the semicolon (;) at the end of our print statement. The semicolon denotes the end of a Perl statement, so the interpreter knows when the next statement begins. Forgetting to put the semicolon at the end of a statement is a common error, and will usually prevent your program from even running (in this case it won't since there is only one statement, but that's not the norm). You can split a statement across more than one line, as long as it is terminated with a semicolon.

Your Turn

Experiment with hello.pl by using different text in the quotes, changing the double quotes to single quotes, omitting the \n newline sequence, and using \t or \b instead of \n. Try adding a second print statement of your own, and see what happens if you remove the semicolon at the end of the first one.

In the next issue we'll take a look at how to store data in your Perl programs using variables. Until then, enjoy!