⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch37_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 37] 37.2 Why Learn Perl? #1 </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:48:50Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch37_01.htm"TITLE="37. Perl, a Pathologically Eclectic Rubbish Lister"><LINKREL="prev"HREF="ch37_01.htm"TITLE="37.1 What We Do and Don't Tell You About Perl "><LINKREL="next"HREF="ch37_03.htm"TITLE="37.3 Three Great Virtues of a Programmer "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch37_01.htm"TITLE="37.1 What We Do and Don't Tell You About Perl "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 37.1 What We Do and Don't Tell You About Perl "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 37<BR>Perl, a Pathologically Eclectic Rubbish Lister</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch37_03.htm"TITLE="37.3 Three Great Virtues of a Programmer "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 37.3 Three Great Virtues of a Programmer "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5561">37.2 Why Learn Perl? #1 </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-5561-IX-PERL-ADVANTAGES"></A>Perl is a language for easily manipulating text, files, and processes.Perl provides a more concise and readable way to do many jobs that wereformerly accomplished (with difficulty) by programming in the Clanguage or one of the shells.While this book is primarily intendedfor users of the UNIX operating system, Perl runs on severalother operating systems, and provides a portable model of computingacross diverse architectures.</P><PCLASS="para">In the beginning, Perl was intended to be a data reduction language: alanguage for navigating among various files in an arbitrary fashion,scanning large amounts of text efficiently, invoking commands toobtain dynamic data, and printing easily formatted reports based onthe information gleaned.And it does these things quite well&nbsp;- thepattern matching and textual manipulation capabilities of Perl oftenoutperform dedicated C programs.But as Perl developed, it alsobecame a convenient file manipulation language&nbsp;- that is, a language inwhich you can deal with the files themselves apart from theircontents, moving them, renaming them, changing their permissions, andso on.And it also became a convenient process manipulation language,allowing you to create and destroy processes, to control the flow ofdata between them, to preprocess their input and postprocess theiroutput, and to clean up after them when they blow up.And it became anetworking language, with the ability to communicate to otherprocesses on other machines via sockets.</P><PCLASS="para">These things can be done in other languages, such as C or one of theshells.But the solutions are difficult and ugly, because C can'teasily do many of the things that a shell can do, and a shell can't domany of the things that C lets you do.Perl fills a rather largeniche between them&nbsp;- providing you with those things that are easy todo in both languages (all in one convenient place), thus bridging thegap between shell programming and C programming.</P><PCLASS="para">On the other hand, knowledge of Perl can actually help you in learningthe C language, if that is your goal.And if you already know C, thenlearning Perl will be easy, since the languages are structured quitesimilarly.Perl also shares features with many of the UNIXutilities that a shell would invoke, and this can ease your learning ofboth Perl and UNIX.</P><PCLASS="para">It has been stated that a language is not worth knowing unless itteaches you to think differently.  Perl is the exception to that rule(for those who know UNIX), because much of Perl is derived inspirit from other portions of UNIX.  To those who merely likePerl, it is the Practical Extraction and Report Language.  To thosewho love it, it's the Pathologically Eclectic Rubbish Lister.  And tothe minimalists in the crowd who think there should only be one way todo something, Perl looks hopelessly redundant and derivative.  Butsomehow, by a grave violation of the minimalistic UNIX toolboxphilosophy, Perl has become the UNIX tool of choice for manytasks of small-to-medium complexity, and ends up fitting quite happilyback into the toolbox.  Perl can be said to be the toolsmith'sworkbench from which new tools are derived.</P><PCLASS="para">Perl is in many ways a simple language.  The types and structures usedby Perl are easy to use and understand, and you can often tell what awell-written piece of Perl code is doing just by glancing at it.  Youdon't have to know any special incantations to compile a Perlprogram&nbsp;- you can just<SPANCLASS="link">execute it like a shell script (<ACLASS="linkend"HREF="ch45_03.htm"TITLE="Don't Need a Shell for Your Script?  Don't Use One ">45.3</A>)</SPAN>.You don't have to know everything there is to know about Perl beforeyou can write useful programs.</P><PCLASS="para">Though simple in many ways, Perl is also a rich language, and there ismuch to be learned about it.  Although it will take some time for youto absorb all that Perl can do, you will be glad that you have accessto the extensive capabilities of Perl when the time comes that youneed them.  We noted above that Perl borrows many capabilities fromthe shells and C, but Perl also possesses a strict superset of<EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> capabilities.  There are, in fact, translatorssupplied with Perl to turn your old <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> (and<EMCLASS="emphasis">nawk</EM>, and <EMCLASS="emphasis">gawk</EM>) scripts into Perl scripts, so you cansee how the features you may already be familiar with correspond tothose of Perl.</P><PCLASS="para">There are other more mundane reasons why people like Perl.  ManyUNIX utilities have undocumented limitations: they don't likelines longer than <EMCLASS="emphasis">n</EM>, where <EMCLASS="emphasis">n</EM> is some mysterious power oftwo, or they blow up if you feed them binary data.  These limitationsare to some extent encouraged by the C language in which they arewritten.  Perl, however, does not have these limitations.  Your lines(and arrays) may grow as long as you like.  Your subroutine recursionmay go as deep as you like.  Variable names can be as long as youlike.  Binary data will not cause problems.  The hashed tables used byassociative arrays expand as necessary to avoid degradation ofperformance.  And you can keep them in database files called DBMfiles.</P><PCLASS="para">You might also like to learn Perl because it will allow you to writeprograms more securely.  Through a dataflow tracing mechanism, Perlcan determine which data is derived from insecure sources, and preventdangerous operations before they happen.  System administrators willparticularly love this feature.</P><PCLASS="para">You might like Perl because it lets you develop programs quickly.  Asan interpreted script language, you get instant feedback whensomething isn't right.  And there's a built-in symbolic debugger thatunderstands any Perl expression because it's written in Perl (andbecause a running Perl script is itself good at executing random bitsof Perl code).</P><PCLASS="para">People have been callingPerl a &quot;system administration language&quot; primarily because systemadministrators have been talking about it, but we think it has amuch broader appeal.  </P><PCLASS="para">[Perl is too complex (or should we say &quot;complete&quot;) a language todescribe in this chapter.  There is an extensive manual page on thedisc along with the software, but if what we've said here intriguesyou, you should probably get Larry and Randal's book <EMCLASS="emphasis">ProgrammingPerl</EM>.  As they say in the preface of the book (from which thisarticle was taken): &quot;Whether you are learning Perl because you arecurious, or because your boss told you to, this handbook will lead youthrough both the basics and the intricacies.  And although we don'tintend to teach you how to program, the perceptive reader will be ableto absorb some of the art, and a little of the science, ofprogramming.  You will also learn much about UNIX, and how tobalance the benefits of the integrated-tool approach with the benefitsof the toolbox approach.  We will encourage you to develop the threegreat 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 firmlybelieve that learning Perl will increase the value of yourresume.&quot;-<EMCLASS="emphasis">TOR</EM>&nbsp;]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LW, RS</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">Programming Perl</CITE></SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch37_01.htm"TITLE="37.1 What We Do and Don't Tell You About Perl "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 37.1 What We Do and Don't Tell You About Perl "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch37_03.htm"TITLE="37.3 Three Great Virtues of a Programmer "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 37.3 Three Great Virtues of a Programmer "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">37.1 What We Do and Don't Tell You About Perl </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">37.3 Three Great Virtues of a Programmer </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -