Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 37.1 What We Do and Don't Tell You About Perl Chapter 37
Perl, a Pathologically Eclectic Rubbish Lister
Next: 37.3 Three Great Virtues of a Programmer

37.2 Why Learn Perl? #1

Perl is a language for easily manipulating text, files, and processes. Perl provides a more concise and readable way to do many jobs that were formerly accomplished (with difficulty) by programming in the C language or one of the shells. While this book is primarily intended for users of the UNIX operating system, Perl runs on several other operating systems, and provides a portable model of computing across diverse architectures.

In the beginning, Perl was intended to be a data reduction language: a language for navigating among various files in an arbitrary fashion, scanning large amounts of text efficiently, invoking commands to obtain dynamic data, and printing easily formatted reports based on the information gleaned. And it does these things quite well - the pattern matching and textual manipulation capabilities of Perl often outperform dedicated C programs. But as Perl developed, it also became a convenient file manipulation language - that is, a language in which you can deal with the files themselves apart from their contents, moving them, renaming them, changing their permissions, and so on. And it also became a convenient process manipulation language, allowing you to create and destroy processes, to control the flow of data between them, to preprocess their input and postprocess their output, and to clean up after them when they blow up. And it became a networking language, with the ability to communicate to other processes on other machines via sockets.

These things can be done in other languages, such as C or one of the shells. But the solutions are difficult and ugly, because C can't easily do many of the things that a shell can do, and a shell can't do many of the things that C lets you do. Perl fills a rather large niche between them - providing you with those things that are easy to do in both languages (all in one convenient place), thus bridging the gap between shell programming and C programming.

On the other hand, knowledge of Perl can actually help you in learning the C language, if that is your goal. And if you already know C, then learning Perl will be easy, since the languages are structured quite similarly. Perl also shares features with many of the UNIX utilities that a shell would invoke, and this can ease your learning of both Perl and UNIX.

It has been stated that a language is not worth knowing unless it teaches you to think differently. Perl is the exception to that rule (for those who know UNIX), because much of Perl is derived in spirit from other portions of UNIX. To those who merely like Perl, it is the Practical Extraction and Report Language. To those who love it, it's the Pathologically Eclectic Rubbish Lister. And to the minimalists in the crowd who think there should only be one way to do something, Perl looks hopelessly redundant and derivative. But somehow, by a grave violation of the minimalistic UNIX toolbox philosophy, Perl has become the UNIX tool of choice for many tasks of small-to-medium complexity, and ends up fitting quite happily back into the toolbox. Perl can be said to be the toolsmith's workbench from which new tools are derived.

Perl is in many ways a simple language. The types and structures used by Perl are easy to use and understand, and you can often tell what a well-written piece of Perl code is doing just by glancing at it. You don't have to know any special incantations to compile a Perl program - you can just execute it like a shell script (45.3). You don't have to know everything there is to know about Perl before you can write useful programs.

Though simple in many ways, Perl is also a rich language, and there is much to be learned about it. Although it will take some time for you to absorb all that Perl can do, you will be glad that you have access to the extensive capabilities of Perl when the time comes that you need them. We noted above that Perl borrows many capabilities from the shells and C, but Perl also possesses a strict superset of sed and awk capabilities. There are, in fact, translators supplied with Perl to turn your old sed and awk (and nawk, and gawk) scripts into Perl scripts, so you can see how the features you may already be familiar with correspond to those of Perl.

There are other more mundane reasons why people like Perl. Many UNIX utilities have undocumented limitations: they don't like lines longer than n, where n is some mysterious power of two, or they blow up if you feed them binary data. These limitations are to some extent encouraged by the C language in which they are written. Perl, however, does not have these limitations. Your lines (and arrays) may grow as long as you like. Your subroutine recursion may go as deep as you like. Variable names can be as long as you like. Binary data will not cause problems. The hashed tables used by associative arrays expand as necessary to avoid degradation of performance. And you can keep them in database files called DBM files.

You might also like to learn Perl because it will allow you to write programs more securely. Through a dataflow tracing mechanism, Perl can determine which data is derived from insecure sources, and prevent dangerous operations before they happen. System administrators will particularly love this feature.

You might like Perl because it lets you develop programs quickly. As an interpreted script language, you get instant feedback when something isn't right. And there's a built-in symbolic debugger that understands any Perl expression because it's written in Perl (and because a running Perl script is itself good at executing random bits of Perl code).

People have been calling Perl a "system administration language" primarily because system administrators have been talking about it, but we think it has a much broader appeal.

[Perl is too complex (or should we say "complete") a language to describe in this chapter. There is an extensive manual page on the disc along with the software, but if what we've said here intrigues you, you should probably get Larry and Randal's book Programming Perl. As they say in the preface of the book (from which this article was taken): "Whether you are learning Perl because you are curious, or because your boss told you to, this handbook will lead you through both the basics and the intricacies. And although we don't intend to teach you how to program, the perceptive reader will be able to absorb some of the art, and a little of the science, of programming. You will also learn much about UNIX, and how to balance the benefits of the integrated-tool approach with the benefits of the toolbox approach. We will encourage you to develop the three great virtues of a programmer: laziness, impatience, and hubris. Along the way, we hope you find the book mildly amusing in some spots (and wildly amusing in others). And while we're at it, we firmly believe that learning Perl will increase the value of your resume."-TOR ]

- LW, RS from O'Reilly & Associates' Programming Perl

Previous: 37.1 What We Do and Don't Tell You About Perl UNIX Power ToolsNext: 37.3 Three Great Virtues of a Programmer
37.1 What We Do and Don't Tell You About Perl Book Index37.3 Three Great Virtues of a Programmer

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System