• Aucun résultat trouvé

Constructing an input/output script

Dans le document Minimal Perl (Page 83-86)

Perl essentials

2.7 C ONSTRUCTING PROGRAMS

2.7.2 Constructing an input/output script

What about programs that involve multiple Application Types? As indicated in step 1 of section 2.7, such cases require the largest option cluster (i.e., the one with the most keyletters) from any of the relevant areas.

For example, consider the line-numbering command shown earlier:

perl -wnl -e 'printf "$.: "; print;' marx_bros

The printing of the record-number variable ($.) via printf qualifies as Output Generation, as does the implicit printing of the $_ variable via print. Thus far, the -wl Primary Option Cluster seems to be indicated.

But what about the marx_bros argument at the end of the command? Its pres-ence reminds us that the program reads input, which specifies the Application Type of Input Processing and the -wnl Primary Option Cluster. That one is larger than -wl, so it wins, and we use it.

At this point in the construction of the program, we have the following:

perl -wnl

Step 2 doesn’t require any adjustments, because we want to use the default of reading a line at a time with the n option.

Step 3 doesn’t apply, because no additional option clusters are needed.

We’re writing a script rather than a one-liner, so step 4b gives us this:

$ cat num_lines

#! /usr/bin/perl -wnl

Next we make the script executable:

$ chmod +x num_lines

In step 5b, we add the code for the program itself, which yields

#! /usr/bin/perl -wnl printf "$.: ";

print;

Step 6 reminds us to supply a filename argument for our test run:

$ num_lines marx_bros 1: Groucho

...

Step 7 tells us that the final print can be deleted if we replace the shebang line’s n option with p, which would reduce the program to:

#! /usr/bin/perl -wpl printf "$.: ";

But that adjustment makes it too easy for a casual reader to mistakenly conclude that the program just prints line numbers, so we’ll keep the final print statement in this case. However, when we discuss sed-like Perl programs in chapter 4, you’ll see several examples where replacing n with p is recommended.

2.8 S

UMMARY

Perl was created as an amalgamation of the best features of various UNIX languages and utilities. But Larry also added some imaginative new features, which represent a departure from UNIX traditions. The result is that Perl is a large and complex lan-guage—and a potentially confusing one—because it provides so many distinctly dif-ferent ways to accomplish the same task.

By focusing on a carefully selected subset of the language, you can easily learn to write small programs that provide the essential infrastructure for many types of data processing tasks. In this chapter, you became familiar with the most essential features of Perl, including the most important invocation options, special variables, and functions.

Let’s review some key details. The w invocation option enables warnings, which provide you with valuable feedback about potential problems in a program. The n option enables automatic input processing, which loads the “data variable” $_ with the contents of each input line in turn. The most widely used function in Perl pro-grams is print, which sends its arguments to the output destination. It’s often used without any arguments, to exploit the fact that it prints the contents of $_ by default.

In addition to learning key elements of the language, you saw how Minimal Perl’s guidelines for program construction take the guesswork out of selecting appropriate invocation options for programs and putting together the scripts or one-liners that can do the job.

In the following chapters, you’ll learn other features of Perl that provide additional benefits for programs having particular needs. However, we’ll retain our focus on doing as much as possible with a small subset of the language. I think you’ll be impressed with how much you’ll be able to do with the little you’ll need to learn!

Directions for further study

Appendix B provides guidelines for using parentheses in code and expands on the dis-cussion provided at the end of section 2.4.5.

To learn more about other topics covered here, you can consult online documen-tation by issuing the commands listed at the chapter’s end, such as manperlintro. Some of those commands use Perl’s perldoc utility for accessing online documen-tation, which is similar to the Unix man command. However, perldoc has addi-tional features, such as the ability to extract and display small excerpts from large documents. Even better, because it’s distributed with Perl, it’s available on every

system that a Perl programmer would use—which cannot be said of man, which is Unix-based.

In cases where both perldoc and man can do the job of delivering the desired documentation on a Unix system, we show the use of man, because it’s faster.

For example, you can learn more about CPAN by using this command:

perldoc -q CPAN # answers questions about CPAN

The q option instructs perldoc to search Perl’s online Frequently Asked Questions (FAQ) files for matching keywords.

In the interest of keeping things simple in your early days with Perl, you may want to put off exploring the following resources until you feel comfortable with all the material in part 1:

man perlrun # describes invocation options

perldoc -f print # describes the "print" function

man perlintro # provides beginner orientation

man Text::Autoformat # describes the indicated module

man perlop # describes operator precedence

C H A P T E R 3

Perl as a (better)

Dans le document Minimal Perl (Page 83-86)