Chapter 4. Getting a Handle on Printing

Image

When you complete this chapter, you should be able to explain each of the following statements (each statement is an entity unto itself):

printf "%-10s $%d%10.2f %b %x %o ", "Jack", 15,15,15,15,15;

print 'She cried, "I can't help you!"'," ";

$str = sprintf "$%.2f", $sal;
print  qq!u$name, the local time is !, scalar localtime, " ";

use feature qw(say);

say "The sum is ", 5 + 4;
say "No more! "

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!

4.1 The Special Filehandles STDOUT, STDIN, STDERR

In Chapter 3, “Perl Scripts,” we briefly introduced standard I/O. By convention, whenever your program starts execution, the parent process (normally a shell program) opens three predefined streams called stdin, stdout, and stderr. All three of these streams are connected to your terminal by default.

stdin is the place where input comes from, the terminal keyboard; stdout is where output normally goes, the screen; and stderr is where errors from your program are printed, also the screen.

Perl inherits stdin, stdout, and stderr from the shell. Perl does not access these streams directly but gives them names called filehandles. Perl accesses the streams via the filehandle. The filehandle for stdin is called STDIN; the filehandle for stdout is called STDOUT; and the filehandle for stderr is called STDERR. STDERR is a separate stream that sends its output to the screen and allows you to redirect those errors to, for example, an error log in order to find out what went wrong. Later, we’ll use Perl techniques to deal with errors and error messages. (See Figure 4.1.)

Image

Figure 4.1 Reading from STDIN and writing to STDOUT.

In Chapter 10, “Getting a Handle on Files,” we’ll see how you can create your own filehandles, but for now we’ll stick with those that are predefined.

The print and printf functions, by default, send their output to the STDOUT filehandle, your screen. For example:

print "Give me your name";
chomp($name = <STDIN>);
# User is prompted to enter something at the keyboard,
# until he presses the return key. The chomp function removes the newline.
   if ( $name eq "") { print STDERR "You didn't enter anything.
      $name is empty. ";
      exit 1; }
print STDOUT "Hello, $name ";   # A string of output is sent to screen.
print "Hello back to you. ";    # STDOUT is the default for the print
                                 # function.

4.2 Words

When printing, it is helpful to understand how Perl views words. A word is a sequence of characters with a unit of meaning, much like words in English. Perl words are not restricted to just alpha characters, but they cannot contain whitespace unless quoted. A string is a word or words enclosed in matching quotes (for example, “This is the life!”). You can use an unquoted word to identify filehandles, functions, labels, and other reserved words; for example, with print STDERR “Error ”, print is a function, STDERR is a filehandle, and “Error ” is a string. If the word has no special meaning to Perl, it will be treated as if surrounded by single quotes and is called a bareword.

4.3 The print Function

You will probably use the built-in print function more than any of the printing options provided by Perl because it is efficient and easy to use. The print function prints a string or a list of comma-separated words to the Perl filehandle STDOUT. If successful, the print function returns 1.

The string literal adds a newline to the string. You can embed it in the string or treat it as a separate string. (Perl requires that escape sequences like be enclosed in double quotes.) The say function ( version 5.10) is just like print but appends a newline for you. (See Section 4.4.2, “The No Newline say Function,” later in this chapter.)

4.3.1 Quotes Matter!

Since quoting affects the way in which variables are interpreted, this is a good time to review Perl’s quoting rules. It is often difficult to determine which quotes to use, where to use them, and how to find the culprit if they are misused; in other words, it can be a real debugging nightmare.1 To lighten things up a little, Perl offers an alternative method of quoting, but you still have to fully understand quoting rules before the alternative is useful.2 You can use the backslash () to quote a special character such as $ or @ and it behaves as a set of single quotes, as ‘$’ or ‘@’.

1. Barry Rosenberg, in his book KornShell Programming Tutorial, has a chapter titled “The Quotes From Hell.”

2. Larry Wall, creator of Perl, calls his alternative quoting method “syntactic sugar.”

Perl has three types of quotes and all three types have a different function. They are single quotes, double quotes, and backquotes. Quotes come in pairs and must be matched. For example:

print "This is a quoted string. Some characters are special;
e.g., $var, @list and are interpreted within double quotes. ";

print 'This is also a quoted string. All characters are literal within
single quotes; i.e., $, @, and backslash characters are not special';

print "This is an operating system shell command enclosed in back quotes."
, `pwd`;

print "The backslash quotes a single character as in $5.00. It protects
the $ from interpretation";

A pair of single or double quotes may delimit a string of characters. Quotes will either allow the interpretation of special characters or protect special characters from interpretation, depending on the kind of quotes you use.

Single quotes are the “democratic” quotes. All characters enclosed within them are treated equally; in other words, there are no special characters. But the double quotes discriminate. They treat some of the characters in the string as special characters. The special characters include the $ sign, the @ symbol, and escape sequences such as and .

When backquotes surround an operating system command, the command will be executed by the shell, often called command substitution. The output of the command will be returned as a string that can be used in a print statement, assigned to a variable, and so forth. If you are using Windows, Linux, or UNIX, the commands enclosed within backquotes must be supported by the particular operating system and will vary from system to system. (If your program is going to be used on several operating systems, using backquotes will affect its portability.)

No matter what kind of quotes you are using, they must be matched. Because the quotes mark the beginning and end of a string, Perl will complain about a “Might be a multiline runaway string” or “Execution of quotes aborted...” or “Can’t find string terminator anywhere before EOF...” and fail to compile if you forget one of the quotes.

Double Quotes

Double quotes must be matched, unless embedded within single quotes. When a string is enclosed in double quotes, scalar variables (preceded with a $) and arrays (preceded by the @ symbol) are interpolated (that is, the value of the variable replaces the variable name in the string). Hashes (preceded by the % sign) are not interpolated within the string enclosed in double quotes.

Strings that contain string literals (such as , ) must be enclosed in double quotes for backslash interpretation.

A single quote may be enclosed in double quotes, as in “I don’t care!”

Single Quotes

If a string is enclosed in single quotes, it is printed literally (what you see is what you get).

If a single quote is needed within a string, then it can be embedded within double quotes or backslashed. If double quotes are to be treated literally, they can be embedded within single quotes.

Backquotes

UNIX/Windows3 commands placed within backquotes are executed by the shell, and the output is returned to the Perl program as a string, usually assigned to a variable or made part of a print string. When the output of a command is assigned to a variable, the context is scalar (that is, a single value is assigned).4 For command substitution to take place, the backquotes cannot be enclosed in either double or single quotes. (Make note, UNIX shell programmers: backquotes cannot be enclosed in double quotes as in shell programs.)

3. If using other operating systems, such as Microsoft or Mac OS 9.1 and below, the OS commands available for your system will differ.

4. If output of a command is assigned to an array, the first line of output becomes the first element of the array, the second line of output becomes the next element of the array, and so on.

Perl’s Alternative Quotes

Perl provides an alternative form of quoting—the q, qq, qx, and qw constructs.

• The q represents single quotes.

• The qq represents double quotes.

• The qx represents backquotes.

• The qw represents a quoted list of words. (See Table 4.1.)

Image

Table 4.1 Alternative Quoting Constructs

The string to be quoted is enclosed in forward slashes, but you can use alternative delimiters for all four of the q constructs. You can use a nonalphanumeric character for the delimiter, such as a # sign, ! point, or paired characters, such as parentheses, square brackets, and so forth. You can also use a single character or paired characters. For example:

q/Hello/

q#Hello#

q{Hello}

q[Hello]

q(Hello)

Quoting rules affect almost everything you do in Perl, especially when printing a string of words. Strings are normally delimited by a matched pair of either double or single quotes. When a string is enclosed in single quotes, all characters are treated as literals. When a string is enclosed in double quotes, however, almost all characters are treated as literals, with the exception of those characters that are used for variable substitution and special escape sequences. We will look at the special escape sequences in this chapter and discuss quoting and variables in Chapter 5, “What’s in a Name?

Perl uses some characters for special purposes, such as the dollar sign ($) and the at (@) sign. If these special characters are to be treated as literal characters, they may be preceded by a backslash () or enclosed within single quotes (‘ ‘). Use the backslash to quote a single character rather than a string of characters.

It is so common to make mistakes with quoting that we will introduce here the most common error messages you will receive resulting from mismatched quotes and bare words.

Think of quotes as being the “clothes” for Perl strings. If you take them off, you may get a “Bareword” message such as:

Bareword “there” not allowed while “strict subs” in use at try.pl line 3. Execution of program.pl aborted due to compilation errors.

Also think of quotes as being mates. A double quote is mated with a matching double quote, and a single quote with a matching single quote. If you don’t match the quotes, if one is missing, the missing quote has “run away.” Where did the mate go? You may receive an error like this:

(Might be a runaway multi-line “” string starting on line 3)

4.3.2 Literals (Numeric, String, and Special)

When assigning literal values5 to variables or printing literals, you can represent the literals numerically as integers in decimal, octal, or hexadecimal or as floats in floating-point or scientific notation.

5. Literals may also be called “constants,” but the Perl experts prefer the term “literal,” so in deference to them, we’ll use the term “literal.”

Strings enclosed in double quotes may contain string literals, such as for the newline character, for a tab character, or e for an escape character. String literals are alphanumeric (and only alphanumeric) characters preceded by a backslash. They may be represented in decimal, octal, or hexadecimal, or as control characters.

Perl also supports special literals for representing the current script name, the line number of the current script, and the logical end of the current script.

Since you will be using literals with the print and printf functions, let’s see what these literals look like.

Numeric Literals

You can represent literal numbers as positive or negative integers in decimal, octal, or hexadecimal (see Table 4.2). You can represent floats in floating-point notation or scientific notation. Octal numbers contain a leading 0 (zero), hex numbers a leading 0x (zero and x), and numbers represented in scientific notation contain a trailing E, followed by a negative or positive number representing the exponent.

Image

Table 4.2 Numeric Literals

Printing Numeric Literals
String Literals

Like shell strings, Perl strings are normally one or more characters delimited by either single or double quotes; for example, “This is a literal string” and ‘so is this a literal string’. Escape sequences, (single characters that when preceded by a backslash don’t represent themselves) are interpreted only if enclosed in double quotes (see Table 4.3). “This is a literal string with an escape sequence ”.

Image

Table 4.3 Escape Sequences

Printing String Literals
Special Literals

Perl’s special literals _ _LINE_ _ and _ _FILE_ _ are used as separate words and will not be interpreted if enclosed in quotes, single or double. They represent the current line number of your script and the name of the script, respectively. These special literals are equivalent to the predefined special macros used in the C language.

The _ _END_ _ special literal is used in scripts to represent the logical end of the file. Any trailing text following the _ _END_ _ literal will be ignored, just as if it had been commented. The control sequences for end of input in UNIX are <CTRL>+D (04), and <CTRL>+Z (32) in MS-DOS; both are synonyms for _ _END_ _.

The _ _DATA_ _ special literal is used as a filehandle to allow you to process textual data from within the script instead of from an external file. This can be useful in testing samples of data from files rather than working on the entire file. See Chapter 5, “What’s in a Name?” for some examples.

There are two underscores on either side of the special literals. See Table 4.4 for a description of them all.

Image

Table 4.4 Special Literals

Printing Special Literals

4.3.3 Printing Without Quotes—The here document

The Perl here document is derived from the UNIX shell here document. It allows you to quote a whole block of text enclosed between words called user-defined terminators. From the first terminator to the last terminator, the text is quoted, or you could say “from here to here” the text is quoted. The here document is a line-oriented form of quoting, requiring the << operator followed by an initial terminating word and a semicolon. There can be no spaces after the << unless the terminator itself is quoted.

If the starting terminating word is not quoted or double quoted, variable expansion is performed. If the starting word is singly quoted, variable expansion is not performed. Each line of text is inserted between the first and last terminating word. The final terminating word must be on a line by itself, with no surrounding whitespace.

Perl does not perform command substitution (backquotes) in the text of a here document. Perl, on the other hand, does allow you to execute commands in the here document if the terminator is enclosed in backquotes. (Not a good idea.)

Here documents are used extensively in CGI scripts for enclosing large chunks of HTML documents for printing.

here documents and CGI

The following program is called a CGI (Common Gateway Interface) program, a simple Perl program executed by a Web server rather than by the shell. It is just like any other Perl script with two exceptions:

• There is a line called the MIME line (for example, Content-type: text/html) that describes what kind of content will be sent back to the browser.

• The document consists of text embedded with HTML tags, the language used by browsers to render text in different colors, font faces, types, and so forth. The here document avoids using the print function for every line of the program.

CGI programs are stored in a special directory called cgi-bin, which is normally found under the Web server’s root directory.

The following is a simple example of how Perl interacts with a Web server, but “Perl’s CGI.pm performs very well in a vanilla CGI.pm environment and also comes with builtin support for mod_perl and mod_perl2 as well” (see FastCGI. http://perldoc.perl.org/CGI.html#SYNOPSIS in the Perl 5.18 documentation).

To execute the following script, you will start up your Web browser and type in the Location box: http://servername/cgi-bin/scriptname.6

6. You must supply the correct server name for your system and the correct filename. Some CGI files must have a .cgi or .pl extension.

4.4 Fancy Formatting with the printf Function

Perl provides the built-in printf function if you want to format strings or numbers with specific field sizes, left or right field designators, the different number bases, decimal point precision, and so forth. The printf function prints a formatted string to the selected filehandle, the default being STDOUT. It is like the printf function used in the C language and many other languages derived from C. The return value is 1 if printf is successful and 0 if it fails.

The printf function consists of a quoted control string that may include format specifications. The quoted string is followed by a comma and a list of comma-separated arguments, which are simply expressions.

The format specifiers are preceded by a % sign. For each % sign and format specifier, there must be a corresponding argument. (See Tables 4.5 and 4.6.)

Image

Table 4.5 Format Specifiers

Image

Table 4.6 Flag Modifiers

Placing the quoted string and expressions within parentheses is optional.

Flag modifiers are used after the % to further define the printing; for example, %-20s represents a 20-character left-justified field.

When an argument is printed, the field holds the value that will be printed, and the width of the field is the number of characters the field should contain. The width of a field is specified by a percent sign and a number representing the maximum field width, followed by the conversion character. Example 4.19 shows how this works.

Note that if the argument exceeds the maximum field width, printf will not truncate the number, but your formatting may not look nice. If the number to the right of the decimal point is truncated, it will be rounded up; for example, if the formatting instruction is %.2f, the corresponding argument, 56.555555, would be printed as 56.6.

4.4.1 Saving Formatting with the sprintf Function

The sprintf function is just like the printf function, except it allows you to save the formatting to a variable to be printed at some later time. Both functions, sprintf and printf, use the same conversion tables. Variables are discussed in Chapter 5, “What’s in a Name?

4.4.2 The No Newline say Function

The say function is just like the print function. It also prints a comma-separated list of arguments, except it automatically appends a newline to the end of its list, so you don’t have to remember to do it. It may save you a little time when debugging. This feature is not available in versions of Perl before release 5.10, so you must enable it. There are several ways to do this:

use feature 'say';   # See pragmas in the next section

or

use feature ':5.10';

or

use v5.10;

4.5 What Are Pragmas?

A pragma is a special Perl module that comes with each distribution of Perl and hints to the compiler something about how your program code should be compiled. You can use this type of module to help control the way your program behaves such as discovering context errors, global variables, misused references, operators, and to implement new features.

4.5.1 The feature Pragma

The feature pragma is used to load features particular to a newer release of Perl, features that may break older versions.

use feature ':5.10'; # loads all features available in perl 5.10

use v5.10;           # implicitly loads :5.10 feature bundle

use feature qw(say switch state); # Adds functions by name new since 5.10.

Here is a description of the feature pragma from the Perl documentation:

It is usually impossible to add new syntax to Perl without breaking some existing programs. This pragma provides a way to minimize that risk. New syntactic constructs, or new semantic meanings to older constructs, can be enabled by “use feature ‘foo’”, and will be parsed only when the appropriate feature pragma is in scope. (Nevertheless, the “CORE::” prefix provides access to all Perl keywords, regardless of this pragma.)

4.5.2 The warnings Pragma

In Chapter 3, “Perl Scripts,” we talked about the -w switch used to warn you about the possibility of using future reserved words, misspelled variables, misused operators, and a number of other problems that may cause problems in the program. (Often, these warnings are rather cryptic and hard to understand if you are new to programming.)

Larry Wall says in the Perl 5 man pages, “Whenever you get mysterious behavior, try the -w switch! Whenever you don’t get mysterious behavior, try the -w switch anyway.”

You can use the -w switch either as a command-line option to Perl, as:

perl -w <scriptname>

or after the shebang line in the Perl script, such as:

#!/usr/bin/perl -w

or since version 5.10:

#!/usr/bin/env perl -w

Starting with Perl 5.6.0, warnings.pm was added to the standard Perl library; similar to the -w switch, it is a pragma that allows you to control the types of warnings printed.

To see more about how the warnings pragma works, use the perldoc command at the command line:

perldoc warnings;

In your programs, add the following line under the #! line or, if not using the #! line, at the top of the script:

use warnings;

This enables all possible warnings. To turn off warnings, simply add as a line in your script:

no warnings;

This disables all possible warnings for the rest of the script.

4.5.3 The diagnostics Pragma

This special pragma enhances the warning messages to a more verbose explanation of what went wrong in your program. Like the warnings pragma, it affects the compilation phase of your program, but unlike the warnings pragma, it attempts to give you an explanation that doesn’t assume you are an experienced programmer.

4.5.4 The strict Pragma and Words

Another important pragma is the strict pragma. If your program disobeys the restrictions placed on it, it won’t compile. There are three possible ways that being strict will cause your program to abort given the arguments: “subs”, “vars”, and “refs”. Using strict without arguments get you all three. If, for example, there is a chance that you might have used barewords (that is, unquoted words, as in Hello), the strict pragma with sub as the argument will catch it, and your program will abort until you quote the word, “Hello”. When creating modules, strict is also used to enforce privately scoped variables and catch symbolic references, a topic we cover in Chapter 5, “What’s in a Name?

4.6 What You Should Know

1. How do you define stdin, stdout, and stderr?

2. What is meant by the term filehandle?

3. How do you represent a number in octal, hexadecimal, or binary?

4. What is the main difference between the print, say, and printf functions?

5. How do double and single quotes differ in the way they treat a string?

6. What are q and qq used for?

7. What are literals?

8. What is the use of _ _END_ _?

9. What are backslash sequences?

10. What is the purpose of the sprintf function?

11. What does strict do?

12. What is a pragma? What does the feature pragma do?

13. How can you check to make sure your syntax is ok? How can you check for a spelling error?

14. What is a here document? How is it useful in CGI programs?

4.7 What’s Next?

In the next chapter, you will learn about Perl variables and the meaning of the “funny characters.” You will be able to create and access scalars, arrays, and hashes and understand context and namespaces. You will also learn how to get input from a user and why we need to chomp. A number of array and hash functions will be introduced.

Exercise 4: A String of Perls

1. Use the print, printf, or say function to output the following string:

“Ouch,” cried Mrs. O’Neil, “You mustn’t do that Mr. O’Neil!”

2. Use the sprintf function to format $34.666666 as $34.67 and store it in a variable. Print the value of the variable with the say function.

3. Write a Perl script called literals.plx that will print the following:

$ perl literals
Today is Mon Mar 12 12:58:04 PDT 2014  (Use localtime())
The name of this PERL SCRIPT is literals.
Hello. The number we will examine is 125.5.
The NUMBER in decimal is 125.
The following number is taking up 20 spaces and is right justified.
|                        125|
                The number in hex is 7d
                The number in octal is 175
The number in scientific notation is 1.255000e+02
The unformatted number is 125.500000
The formatted number is 125.50
My boss just said, "Can't you loan me $12.50 for my lunch?"
I flatly said, "No way!"
Good-bye (Makes a beep sound)

Note: The words PERL SCRIPT and NUMBER are capitalized by using string literal escape sequences.

What command-line option would you use to check the syntax of your script?

4. Add to your literals script a here document to print:

Life is good with Perl.

I have just completed my second exercise!

5. What is the feature pragma used for?

6. How would you turn on warnings in the script? How would you turn them off? How would you turn on diagnostics?

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

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