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

📄 ch37_04.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 37] 37.4 Why Learn Perl? #2 </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:53Z"><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_03.htm"TITLE="37.3 Three Great Virtues of a Programmer "><LINKREL="next"HREF="ch37_05.htm"TITLE="37.5 And Now, Perl 5 "></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_03.htm"TITLE="37.3 Three Great Virtues of a Programmer "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 37.3 Three Great Virtues of a Programmer "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_05.htm"TITLE="37.5 And Now, Perl 5 "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 37.5 And Now, Perl 5 "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5562">37.4 Why Learn Perl? #2 </A></H2><PCLASS="para">Donning my vestments as devil's advocate, let me start by saying thatjust because you learn something new, you shouldn't entirely forgetthe old.  UNIX is a pluralistic environment in which many pathscan lead to the solution, some more circuitously than others.Different problems can call for different solutions.  If you forceyourself to program in nothing but Perl, you may be short-changingyourself and taking the more tortuous route for some problems.</P><PCLASS="para">Now, that being said, I shall now reveal my true colors as Perldisciple and perhaps not infrequent evangelist.  Perl is withoutquestion the greatest single program to appear in the UNIXcommunity (although it runs elsewhere, too) in the last ten years.[Tom wrote this in 1992 or so, but I'd bet his opinion hasn't changedsince then. <CODECLASS="literal">;-)</CODE> <EMCLASS="emphasis">-JP</EM>&nbsp;] It makes programming fun again.It's simple enough to get a quick start on, but rich enough for somevery complex tasks.  I frequently learn new things about it despitehaving used it nearly daily since Larry Wall first released it to thegeneral public around 1991.  Heck, sometimes even Larry learnssomething new about Perl! The Artist is not always aware of thebreadth and depth of his own work.</P><PCLASS="para">It is indeed the case that Perl is a strict superset of <EMCLASS="emphasis">sed</EM> and<EMCLASS="emphasis">awk</EM>, so much so that <EMCLASS="emphasis">s2p</EM> and <EMCLASS="emphasis">a2p</EM> translators existfor these utilities.  You can do anything in Perl that you can do inthe shell, although Perl is, strictly speaking, not a commandinterpreter.  It's more of a programming language.</P><PCLASS="para">Most of us have written, or at least seen, shell scripts from hell.While often touted as one of UNIX's strengths because they'reconglomerations of small, single-purpose tools, these shell scriptsquickly grow so complex that they're cumbersome and hard tounderstand, modify, and maintain.  After a certain point ofcomplexity, the strength of the UNIX philosophy of having manyprograms that each does one thing well becomes its weakness.</P><PCLASS="para">The big problem with piping tools together is that there is only onepipe.  This means that several different data streams have to getmultiplexed into a single data stream, then demuxed on the other endof the pipe.  This wastes processor time as well as human brain power.</P><PCLASS="para">For example, you might be shuffling a list of filenames through apipe, but you also want to indicate that certain files have aparticular attribute, and others don't.  (For example, certain filesare more than ten days old.)  Typically, this information is encodedin the data stream by appending or prepending some special markerstring to the filename.  This means that both the pipe feeder and thepipe reader need to know about it.  Not a pretty sight.</P><PCLASS="para">Because <EMCLASS="emphasis">perl</EM> is one program rather than a dozen others(<EMCLASS="emphasis">sh</EM>, <EMCLASS="emphasis">awk</EM>, <EMCLASS="emphasis">sed</EM>, <EMCLASS="emphasis">tr</EM>, <EMCLASS="emphasis">wc</EM>, <EMCLASS="emphasis">sort</EM>,<EMCLASS="emphasis">grep</EM>, and so on), it is usually clearer to express yourself in<EMCLASS="emphasis">perl</EM> than in <EMCLASS="emphasis">sh</EM> and allies, and often more efficient aswell.  You don't need as many pipes, temporary files, or separateprocesses to do the job.  You don't need to go shoving your datastream out to <EMCLASS="emphasis">tr</EM> and back, and to <EMCLASS="emphasis">sed</EM> and back, and to<EMCLASS="emphasis">awk</EM> and back, and to <EMCLASS="emphasis">sort</EM> and back, and then back to<EMCLASS="emphasis">sed</EM>, and back again.  Doing so can often be slow, awkward,and/or confusing.</P><PCLASS="para">Anyone who's ever tried to pass command-line arguments into a<EMCLASS="emphasis">sed</EM> script of moderate complexity or above can attest to thefact that getting the quoting right is not a pleasant task.  In fact,quoting in general in the shell is just not a pleasant thing to codeor to read.</P><PCLASS="para">In a heterogeneous computing environment, the available versions ofmany tools vary too much from one system to the next to be utterlyreliable.  Does your <EMCLASS="emphasis">sh</EM> understand functions on all yourmachines?  What about your <EMCLASS="emphasis">awk</EM>?  What about local variables?It is very difficult to do complex programming without being able tobreak a problem up into subproblems of lesser complexity.  You'reforced to resort to using the shell to call other shell scripts andallow UNIX's power of<SPANCLASS="link">spawning processes (<ACLASS="linkend"HREF="ch38_02.htm"TITLE="fork and exec ">38.2</A>)</SPAN>to serve as your subroutine mechanism, which is inefficient at best.That means your script will require several separate scripts to run,and getting all these installed, working, and maintained on all thedifferent machines in your local configuration is painful.  With<EMCLASS="emphasis">perl</EM>, all you need to do is get it installed on thesystem&nbsp;- which is really pretty easy thanks to Larry's <EMCLASS="emphasis">Configure</EM>program&nbsp;- and after that you're home free.</P><PCLASS="para">Perl is even beginning to be included by some software and hardwarevendors' standard software distributions.  I predict we'll see a lotmore of this in the next couple of years.</P><PCLASS="para">Besides being faster, <EMCLASS="emphasis">perl</EM> is a more powerful tool than<EMCLASS="emphasis">sh</EM>, <EMCLASS="emphasis">sed</EM>, or <EMCLASS="emphasis">awk</EM>.  I realize these are fighting wordsin some camps, but so be it.  There exists a substantial niche betweenshell programming and C programming that <EMCLASS="emphasis">perl</EM> convenientlyfills.  Tasks of this nature seem to arise with extreme frequency inthe realm of system administration.  Since system administratorsalmost invariably have far too much to do to devote a week to codingup every task before them in C, <EMCLASS="emphasis">perl</EM> is especially useful forthem.  Larry Wall, Perl's author, has been known to call it &quot;a shellfor C programmers.&quot;  I like to think of it as a &quot;BASIC forUNIX.&quot;  I realize that this carries both good and badconnotations.</P><PCLASS="para">In what ways is <EMCLASS="emphasis">perl</EM> more powerful than the individual tools?This list is pretty long, so what follows is not necessarily anexhaustive list.  To begin with, you don't have to worry aboutarbitrary and annoying restrictions on string length, input linelength, or number of elements in an array.  These are all virtuallyunlimited; i.e., limited to your system's address space and virtualmemory size.</P><PCLASS="para">Perl's<SPANCLASS="link">regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>handling is far and above the best I've ever seen.  For one thing, youdon't have to remember which tool wants which particular flavor ofregular expressions, or lament the fact that one tool doesn't allow<CODECLASS="literal">(..|..)</CODE> constructs or <CODECLASS="literal">+</CODE>'s <CODE

⌨️ 快捷键说明

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