Chapter 3. Perl Scripts

Image

Now it is time to write a Perl script and execute it. By the time you have finished this chapter, you should understand each of the following lines (each line is an entity unto itself):

#!/usr/bin/perl

# This statement should print the sum of three numbers

$n = localtime;  print "$n ";

perl -c myscript

(Might be a runaway multi-line "" string...
Argument "6dogs" isn't numeric in addition (+) ...

Before we get started, please take note that each line of code, in most of the examples throughout this book, is numbered. The output and explanations are also numbered to match the numbers in the code. These numbers are provided to help you understand important lines of each program. When copying examples into your text editor, don’t include these numbers, or you will generate many unwanted errors!

3.1 Getting Started

There are several components to creating and running a Perl script that bear explanation before you can do any real scripting. This section will introduce some of the important steps and concepts you will need to get started. The following example illustrates four steps in creating and running a Perl script. At the end of this section, each line of the program called tryme.plx will be explained, including:

1. The optional startup line (UNIX)

2. Comments

3. The executable statements in the body of the script

4. The execution of the script in both UNIX and Windows

3.1.1 Finding a Text Editor

Since you will be using a text editor to write Perl scripts, you can use any of the editors provided by your operating system or download more sophisticated editors specifically designed for Perl, including third-party editors and Integrated Development Environments (IDEs). Table 3.1 lists some of the editors available.

Image

Table 3.1 Types of Editors

3.1.2 Naming Perl Scripts

The only naming convention for a Perl script is that it follow the naming conventions for files on your operating system (upper-/lowercase letters, numbers, and so forth). If, for example, you are using Linux, filenames are case-sensitive, and since there are a great number of system commands, you may want to add an extension to your Perl script names to make sure the names are unique. You are not required to add an extension to the filename unless you are creating modules, writing CGI scripts if the server requires a specific extension, or have set up Windows to expect an extension on certain types of files. By adding a unique extension to the name, you can prevent clashes with other programs that might have the same name. For example, UNIX provides a command called test. If you name a script “test,” which version will be executed? If you’re not sure, you can add a .plx or .pl extension to the end of the Perl script name to give it its own identity.

And of course, give your scripts sensible names that indicate the purpose of the script rather than names like “foo,” “foobar,” or “testing.”

3.1.3 Statements, Whitespace, and Linebreaks

Perl is called a free-form language, meaning you can place statements anywhere on the line and even cross over lines. Whitespace refers to spaces, tabs, and newlines. The newline is represented in your program as " " and must be enclosed in double quotes. Whitespace is used to delimit words. Any number of blank spaces are allowed between symbols and words. Whitespace within strings is preserved when enclosed in single or double quote; otherwise, it is ignored.

For example, the following expressions are the same:

5+4*2

is the same as

5          +         4       *    2;

And both of the following Perl statements are correct, even though the output will show that the whitespace is preserved when quoted.

print "This is a Perl statement.";

   print "This
              is
                also
                  a Perl
                      statement.";

Even though you have a lot of freedom when writing Perl scripts, it is better to put statements on their own line and to provide indentation when using blocks of statements (we’ll discuss this in Chapter 5, “What’s in a Name?”). Of course, annotating your program with comments, so that you and others will understand what is going on, is vitally important. See the next section for more on comments.

3.1.4 Strings and Numbers

Perl strings are characters enclosed in quotes, either single or double quotes (back quotes are used in command substitution). A string can consist of a single character, multiple characters, numbers, or any combination of these. You can think of words as a set of characters delimited by space or punctuation and a group of words as a sentence, but as long as you enclose characters, words, or sentences in a set of matching quotes, it is just a simple string. Some characters in a string may have special meaning; such as a $ or @ or . The way the string is quoted (that is, double or single quotes) determines how Perl will interpret the string (see Section 4.3.1, “Quotes Matter!”).

For example,

"This is a string of 50 characters"

and so is this:

'This is also a string with more characters'

Without quotes, a word is called a bareword. In fact, if you don’t quote a string, you can expect to get a warning message such as “Bareword found where operator expected.”

Perl numbers can be represented as integers (for example, 4, 56, 123) or floating-point numbers (for example, 3.45, 12.1, .66). They can also be represented using different bases such as octal, hexadecimal, binary, and so forth. We will discuss this in Chapter 4, “Getting a Handle on Printing.”

3.2 Filehandles

A filehandle is a bare word or variable representing the place where Perl gets input, sends output, or sends errors. When you start up a Perl script, you normally inherit three file streams from the parent process, normally the shell. They are STDIN, STDOUT, and STDERR. STDIN is tied to your keyboard where you type input that will be received by your script. The output, STDOUT, will initially be sent to your terminal screen in these first examples, and later to files or pipes with user-defined filehandles. The Perl built-in functions print and printf send output to the STDOUT, your screen, as a default.

3.3 Variables (Where to Put Data)

Variables are fundamental to all programming languages. They are data containers whose values may change throughout the run of the program, whereas literals or constants remain fixed. An example of a constant would be the value of PI, the number of seconds in a minute, and so forth. Perl stores strings and numbers in variables, which are storage areas in the program’s memory.

Perl differentiates between storing a single item or a list of items; that is, a variable called a scalar can hold only one value, such as a single number or single string of text, whereas a variable called an array or another type called a hash (associative arrays), can store lists, such as a list of names, files, colors, addresses, and so forth.

This is a scalar:

$name = "John";
$n = 200;

This is an array:

@colors = ("red", "green", "yellow");

This is a hash:

%student = ("Name" => "Joe Blow",
            "Subject" => "Perl",
            "Grade" => "A"
           );

For now, we will start with scalar variables. When you see a dollar sign preceding a variable name, think ONE, think scalar. Only one string or one number will be stored there. For example:

$ch = "M";   # One string
$answer = "Yes";   # One string
$string = "This is a string of beads";   # One string
$number = 3.45;   # One number
$age = 23;   # One number

If you want to think in terms of monetary values, then the dollar sign must be protected from interpretation by either using a backslash or single quotes; for example,

$money='$5.00';
$cash = "$100";

3.3.1 What Is Context?

“You misinterpreted what I said. You took the whole thing completely out of context.”

Perl has functions and operators that behave in a certain way, depending on how you use them, called context. The major two types of context are scalar or list. A function may return a scalar or a list depending on how you use it. A variable may accept one value or a list of values. You may have either string or numeric context depending on what operators you are using. For example, if you use a +, Perl assumes that you want to add numbers, as in 5 + 12; but if you say, for example:

"5cats" + "dogs"

the plus sign operator will automatically try to convert its operands to numbers. "5cats" will be converted to the number 5, and since there are no initial numbers in the string "dogs", it will be converted to the number 0. The string portion is discarded, and now the + operator can add the numbers as 5 + 0. If, on the other hand, you were trying to concatenate two strings, such as "hot" and "dog" to "hotdog", you would use Perl’s concatenation operator, the dot: "hot" . "dog". This operator wants strings. If you say, 55 . 44, Perl will convert the 55 to "55" and the 44 to "44" resulting in "5544". (Perl will warn you about mishaps like this if you indicate to use warnings at the top of your script.)

There are other types of context, such as Boolean, void, or interpolative context. We will talk about "context" in much more depth throughout this book, but it is important to get an early introduction because it effects much of what you do in Perl and can cause unexpected results when you use operators or functions in the wrong context.

3.3.2 Comments

You may write a very clever Perl script today and in two weeks have no idea what your script was supposed to do. If you pass the script on to someone else, the confusion magnifies. Comments are plain text that allow you to insert documentation in your Perl script with no effect on the execution of the program. They are used to help you and other programmers maintain and debug scripts. Perl comments are preceded by a # mark. They extend across the line, but do not continue onto the next line.

Perl does not understand the C language comments /* and */ or C++ comments //.

3.3.3 Perl Statements

Perl executable statements, similar to English sentences, make up most of the Perl script. A statement is an expression, or a series of expressions. Perl statements can be simple or compound, and a variety of operators, modifiers, expressions, and functions make up a statement, as shown in the following example. Simple statements must end in semicolons.

print "Hello, to you! ";
$now = localtime();
print "Today is $now. ";
$result = 5.5 * 4 / 2;
print "Good-bye. ";

3.3.4 Using Perl Built-in Functions

A big part of any programming language is the set of functions built into the language or packaged in special libraries (see Appendix A, “Perl Builtins, Pragmas, Modules, and the Debugger”). Perl comes with many useful functions—independent program code that performs some task. When you call a Perl built-in function, you just type its name, or optionally you can type its name followed by a set of parentheses. All function names must be typed in lowercase. Many functions require arguments—messages that you send to the function. For example, the print function won’t display anything if you don’t pass it an argument, such as the string of text you want to print on the screen. If the function requires arguments, then place the arguments, separated by commas, right after the function name. The function usually returns something after it has performed its particular task. For example, the built-in sqrt function returns the square root of a give number.

$n = sqrt 25;   # The value '5' is returned and assigned to the scalar $n

In Example 3.3, we call two built-in Perl functions, print and localtime. The print function takes a string as its argument and displays the string of text on the screen. The localtime function, on the other hand, doesn’t require an argument and returns the current date and time. Both of the following statements are valid ways to call a function with an argument. The argument is “Hello, there. ”.

The localtime() function returns the date and time. What the date and time look like depends on context (that is, what type of return value is expected from localtime, such as whether it is a list or scalar).

$now = localtime;   # $now is a scalar; the returned value from
                    # localtime is scalar in context.
print $now;


(Output)

Wed Jan 15 10:29:36 2014

@now = localtime;  # @now is an array; return type from
                   # localtime is list in context.
print @now;

(Output)

3930101501143140

3.3.5 Script Execution

At the beginning of this section (Example 3.1) we introduced a sample script to give you an idea of what a script looks like and how it is executed. The script was created in a text editor such as one of those listed in Section 3.1.1. (Don’t use a word processor such as Microsoft Word or Notepad, as you may get unusual characters in your output). Once the lines of the script were entered, the file was saved with the name tryme.plx. Now saved, you would normally go to the command line (either UNIX/Windows), and type at your prompt (in this example, the dollar sign is the prompt):

$   perl tryme.plx

where perl is the name of the perl interpreter and tryme.plx is the name of the script, passed to the interpreter as an argument. When you execute a Perl script, it takes just one step on your part, but internally the Perl interpreter takes two steps. First, it compiles the entire program into byte code, an internal representation of the program. After that, Perl’s byte code engine runs the byte code line by line. If you have compiler errors, such as a missing semicolon at the end of the line, misspelled keyword, or mismatched quotes, you will get a syntax error.

You can execute Perl script directly at the UNIX command line if the #! startup line, commonly called the shebang line, is included as the first line in your script file and the script has execute permission.

$ ./tryme.plx

A note about the shebang line: The first line of the script contains the #! symbols (called the shebang line), followed by the full pathname of the file where the Perl executable resides. This tells the UNIX kernel what program is interpreting the script. An example of the startup line might be

#!/usr/bin/perl

or

#!/usr/bin/env perl

the latter which allows the user’s environment to select which Perl to run, 5.18+.

It is important that the path to the interpreter is entered correctly after the shebang (#!). Perl may be installed in different directories on different systems. Most Web servers will look for this line when invoking CGI scripts written in Perl. Any inconsistency will cause a fatal error. To find the path to the Perl interpreter on your system, type at your UNIX prompt:1

1. Another way to find the interpreter would be: find / -name '*perl*' -print;

$ which perl

Mac OS is really just a version of UNIX and comes bundled with Perl 5.16 (as of this writing). You open a terminal and use Perl exactly the same way you would use it for Solaris, Linux, *BSD, HP-UX, AIX OSX, and so forth.

Win32 platforms don’t provide the shebang syntax or anything like it.2 For Windows,3 you can associate a Perl script with extensions such as .pl or .plx and then run your script directly from the command line. At the command-line prompt or from the system Control Panel, you can set the PATHEXT environment variable to the name of the extension that will be associated with Perl scripts:

2. Although Win32 platforms don’t ordinarily require the shebang line, the Apache Web server does, so you will need the shebang line if you are writing CGI scripts that will be executed by Apache.

3. File association does not work on Windows 95 unless the program is started from the Explorer window.

SET PATHEXT=.pl;%PATHEXT%

Again, the simplest way to execute your script is to pass the script as an argument to the perl interpreter:

$ perl scriptname

3.4 Summing It Up

Now, let’s get a line-by-line explanation of the script that was introduced at the beginning of this section. You should have a better picture of how the script is set up and executed.

3.4.1 What Kinds of Errors to Expect

Expect to make errors and maybe lots of them. You may try many times before you actually get a program to run perfectly. Knowing your error messages is like knowing the quirks of your boss, mate, or even yourself. Some programmers make the same error over and over again. Don’t worry. In time, you will learn what most of these messages mean and how to prevent them.

After the program passes the compile phase (that is, you don’t get any syntax errors or complaints from the compiler), then you may get what are called runtime, or logical, errors. These errors are harder to find and are probably caused by not anticipating problems that might occur when the program starts running. Or it’s possible that the program has faulty logic in the way it was designed. Runtime errors may be caused if a file or database you’re trying to open doesn’t exist, a user enters bad input, you get into an infinite loop, or you try to illegally divide by zero. Whatever the case, these problems, called “bugs,” are harder to find. Perl comes with a debugger that is helpful in determining what caused these logical errors by letting you step through your program line by line. (See Appendix A, “Perl Built-ins, Pragmas, Modules, and the Debugger,” specifically Section A.6, “Debugger.”)

3.5 Perl Switches

Although most of your work with Perl will be done in scripts, you can also execute Perl at the command line for simple tasks, such as testing a function, a print statement, or simply testing Perl syntax. Perl has a number of command-line switches, also called command-line options, to control or modify its behavior. The switches discussed next are not a complete list (again, see Appendix A), but will demonstrate a little about Perl syntax at the command line.

When working at the command line, you will see a shell prompt. The shell is called a command interpreter. UNIX shells such as Korn and bash display a default $ prompt, and C and tcsh shell display a % prompt. The UNIX, Linux (bash and tcsh), and Mac OS shells are quite similar in how they parse the command line. By default, if you are using Windows, the MS-DOS command-line prompt displays a $.4 The Win32 shell has its own way of parsing the command line. Since most of your Perl programming will be done in script files, you will seldom need to worry about the shell’s interaction, but when a script interfaces with the operating system, problems will occur unless you are aware of what commands you have and how the shell executes them on your behalf.

4. It is possible that your command-line prompt has been customized to contain the current directory, history number, drive number, and so forth.

3.5.1 The -e Switch (Quick Test at the Command Line)

The -e switch allows Perl to execute Perl statements at the command line instead of from a script. This is a good way to test simple Perl statements before putting them into a script file, but the shells for UNIX and MS-DOS don’t always parse the command line in the same way, as shown in Example 3.6.

3.5.2 The -c Switch (Check Syntax)

As we demonstrated earlier in this chapter, the -c switch is used to check the Perl syntax without actually executing the Perl commands. If the syntax is correct, Perl will tell you so. It is a good idea to always check scripts with the -c switch. This is especially important with CGI scripts written in Perl, because error messages that are normally sent to the terminal screen are sent to a log file instead. (See also the -w switch in Chapter 4, “Getting a Handle on Printing.”)

3.5.3 The -w Switch (Warnings)

Even though your script may pass the compile test, when you run it, something seems strange; for example, perhaps a variable doesn’t have a value or an operation isn’t producing what you expected. The -w switch sends warning messages if you have, for example, misused operators, or if you have variables used only once, or scalar variables used before they are set, or references to undefined filehandles, and so forth.

A warning is just that: a warning. It doesn’t mean that your program has syntax errors; it means you are doing something questionable and your output might reflect that. It is a good practice to use warnings in your scripts rather than at the command line. We’ll have more about warnings and the use of the warnings pragma in Chapter 4. (See perldiag documentation for more detail.)

1  $sum = "dogs" + "5cats ";  # trying to add two strings;
                               # should be numbers
2  print "The sum is $sum. ";

3  $name="Jack";
4  print "I know $nime well. ";  # Misspelled variable;
                                  # should be $name, not $nime
(At the Command line)
perl -w  context.plx
Name "main::nime" used only once: possible typo at context line 4.
Argument "5cats " isn't numeric in addition (+) at context line 1.
Argument "dogs" isn't numeric in addition (+) at context line 1.
The sum is 5.
Use of uninitialized value $nime in concatenation (.) or string at context
line 4.
I know  well.

3.6 What You Should Know

1. How do you set up a script?

2. How do you name a script?

3. How are statements terminated?

4. What is whitespace?

5. What is meant by free form?

6. What is a variable?

7. What is a built-in function?

8. What is the #! line?

9. What is meant by scalar or list context?

10. How do you make a script executable?

11. Why use comments?

12. How do you execute a Perl script if not using the shebang line.

13. What command-line option lets you check Perl syntax?

14. What is the -w switch for?

3.7 What’s Next?

If you can’t print what your program is supposed to do, it’s like trying to read the mind of a person who can’t speak. In the next chapter, we discuss Perl functions to print output to the screen (stdout) and how to format the output. You will learn how Perl views words, whitespace, literals, backslash sequences, numbers, and strings. You will learn how to use single, double, and backquotes and their alternative form. We will discuss here documents and how to use them in CGI scripts. You will also learn how to use warnings and diagnostics to help debug your scripts.

Exercise 3: Getting with It Syntactically

1. At the command-line prompt, write a Perl statement that will print

Hello world!!
Welcome to Perl programming.

2. Execute a Perl command that will display the version of the Perl distribution you are currently using.

3. Copy the program sample from Example 3.1 into your editor, save it, check the syntax, and execute it.

4. Fix errors in the following script. Use the -c and the -w switches, and execute it.

# This is a comment
to explain what I'm tying to do.
$name=John Doe
print Welcome to Perl!, $nime."
$today=localtime;
print 'The time is $today ';

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.51.153