home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / shell / 5124 < prev    next >
Encoding:
Text File  |  1992-12-21  |  11.0 KB  |  204 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!paladin.american.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!caen!sol.ctr.columbia.edu!eff!news.oc.com!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: ksh 1, perl 2 - ksh or perl for scripting?
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Dec21.174403.26053@news.eng.convex.com>
  8. Date: Mon, 21 Dec 1992 17:44:03 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <ASH.92Dec21095237@ulysses.mr.ams.com>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 186
  17.  
  18. From the keyboard of ash@ulysses.mr.ams.com (Alan Harder):
  19. :Hi, all.  We are currently trying to decide if we should move from
  20. :/bin/sh as our language for production scripting to ksh, or if we
  21. :should move to perl instead.  Is anyone out there using perl as their
  22. :production scripting language of choice? 
  23.  
  24. Certainly.  Many shops use perl for various purposes, including
  25. install scripts, test drivers, database interfaces, menu systems, 
  26. and sysadmin tools.
  27.  
  28. [The following text contains pieces of semi-canned prose which some of
  29.  you may have seen before.]
  30.  
  31. What you're really asking is whether you should use perl or ksh for your
  32. scripting.  The problem with that question is that not all problems require
  33. the same solution.  For simple command-oriented tasks, a shell script
  34. works just fine.  It's faster to write, and sometimes faster to execute.
  35. The built-in test, expr, and other features of ksh will give you a
  36. performance win in this area.
  37.  
  38. Perl and ksh do not address precisely the same problem set.  There is some
  39. overlap, but in general, ksh is an interactive shell and command
  40. language, whereas perl is not a shell but much more the general purpose
  41. programming language.   Perl fills the gap between sh and C, and indeed
  42. extends into those languages' problem domains as well  Ksh does not.
  43.  
  44. Shell programming is inherently cumbersome at expressing certain kinds of
  45. algorithms.  Most of us have written, or at least seen, shell scripts from
  46. hell.  While often touted as one of UNIX's strengths because they're
  47. conglomerations of small, single-purpose tools, these shell scripts
  48. quickly grow complex that they're cumbersome and hard to understand,
  49. modify and maintain.  After a certain point of complexity, the strength of
  50. the UNIX philosophy of having many programs that each does one thing well
  51. becomes its weakness.
  52.  
  53. The big problem with piping tools together is that there is only one
  54. pipe.  This means that several different data streams have to get
  55. multiplexed into a single data stream, then demuxed on the other end of
  56. the pipe.  This wastes processor time as well as human brain power.
  57.  
  58. For example, you might be shuffling through a pipe a list of filenames,
  59. but you also want to indicate that certain files have a particular
  60. attribute, and others don't.  (E.g., certain files are more than ten
  61. days old.)  Typically, this information is encoded in the data stream
  62. by appending or prepending some special marker string to the filename.
  63. This means that both the pipe feeder and the pipe reader need to know
  64. about it.  Not a pretty sight.
  65.  
  66. Because perl is one program rather than a dozen others (sh, awk, sed, tr,
  67. wc, sort, grep, ...), it is usually clearer to express yourself in perl
  68. than in sh and allies, and often more efficient as well.  You don't need
  69. as many pipes, temporary files, or separate processes to do the job.  You
  70. don't need to go shoving your data stream out to tr and back and to sed
  71. and back and to awk and back and to sort back and then back to sed and
  72. back again.  Doing so can often be slow, awkward, and/or confusing.
  73.  
  74. Anyone who's ever tried to pass command line arguments into a sed script
  75. of moderate complexity or above can attest to the fact that getting the
  76. quoting right is not a pleasant task.  In fact, quoting in general in the
  77. shell is just not a pleasant thing to code or to read.
  78.  
  79. In a heterogeneous computing environment, the available versions of many
  80. tools varies too much from one system to the next to be utterly reliable.
  81. Does your sh understand functions on all your machines?  What about your
  82. awk?  What about local variables?  It is very difficult to do complex
  83. programming without being able to break a problem up into subproblems of
  84. lesser complexity.  You're forced to resort to using the shell to call
  85. other shell scripts and allow UNIX's power of spawning processes serve as
  86. your subroutine mechanism, which is inefficient at best.  That means your
  87. script will require several separate scripts to run, and getting all these
  88. installed, working, and maintained on all the different machines in your
  89. local configuration is painful.  With perl, all you need do is get
  90. it installed on the system -- which is really pretty easy thanks to
  91. Larry's Configure program -- and after that you're home free.
  92.  
  93. Shell scripts can seldom hope to approach a perl program's speed.
  94. In fact, perl programs are often faster than a C program, at least, one
  95. that hasn't been highly tuned.  In general, if you have a perl and C expert
  96. working on the same problem, you'll get the perl code to 2 to 3 times
  97. the C code's speed, although for some problems, it's much better than that.
  98. The next release of perl will also be substantially faster (current figures
  99. indicate that 25% faster is not unlikely) than the current one.
  100.  
  101. Besides being faster, perl is a more powerful tool than sh, sed, or awk.
  102. I realize these are fighting words in some camps, but so be it.  There
  103. exists a substantial niche between shell programming and C programming
  104. that perl conveniently fills.  Tasks of this nature seem to arise with
  105. extreme frequency in the realm of systems administration.  Since a system
  106. administrator almost invariably has far too much to do to devote a week to
  107. coding up every task before him in C, perl is especially useful for him.
  108. Larry Wall, perl's author, has been known to call it "a shell for C
  109. programmers."  I like to think of it as a "BASIC for UNIX."  I realize
  110. that this carries both good and bad connotations.  So be it.
  111.  
  112. In what ways is perl more powerful than the individual tools?  This list
  113. is pretty long, so what follows is not necessarily an exhaustive list.
  114. To begin with, you don't have to worry about arbitrary and annoying
  115. restrictions on string length, input line length, or number of elements in
  116. an array.  These are all virtually unlimited, i.e. limited to your
  117. system's address space and virtual memory size.
  118.  
  119. Perl's regular expression handling is far and above the best I've ever
  120. seen.  For one thing, you don't have to remember which tool wants which
  121. particular flavor of regular expressions, or lament that fact that one
  122. tool doesn't allow (..|..) constructs or +'s \b's or whatever.   With
  123. perl, it's all the same, and as far as I can tell, a proper superset of
  124. all the others.
  125.  
  126. Perl has a fully functional symbolic debugger (written, of course, in
  127. perl) that is an indispensable aid in debugging complex programs.  Neither
  128. the shell nor sed/awk/sort/tr/... have such a thing.  There've been folks
  129. who've switched over to doing all their major production scripting in Perl
  130. just so that they have access to a real debugger.
  131.  
  132. Perl has a loop control mechanism that's more powerful even than C's.  You
  133. can do the equivalent of a break or continue (last and next in perl) of
  134. any arbitrary loop, not merely the nearest enclosing one.  You can even do
  135. a kind of continue that doesn't trigger the re-initialization part of a
  136. loop, something you do from time to time want to do.
  137.  
  138. Perl's data-types and operators are richer than the shells' or awk's,
  139. because you have scalars, numerically-indexed arrays (lists), and
  140. string-indexed (hashed) arrays.  Each of these holds arbitrary data
  141. values, including floating point numbers, for which mathematic built-in
  142. subroutines and power operators are available.  In can handle
  143. binary data of arbitrary size.
  144.  
  145. Speaking of lisp, you can generate strings, perhaps with sprintf(), and
  146. then eval them.  That way you can generate code on the fly.  You can even
  147. do lambda-type functions that return newly-created functions that you can
  148. call later. The scoping of variables is dynamic, fully recursive subroutines
  149. are supported, and you can pass or return any type of data into or out
  150. of your subroutines.
  151.  
  152. You have a built-in automatic formatter for generating pretty-printed
  153. forms with automatic pagination and headers and center-justified and
  154. text-filled fields like "%(|fmt)s" if you can imagine what that would
  155. actually be were it legal.
  156.  
  157. There's a mechanism for writing suid programs that can be made more secure
  158. than even C programs thanks to an elaborate data-tracing mechanism that
  159. understands the "taintedness" of data derived from external sources.  It
  160. won't let you do anything really stupid that you might not have thought of.
  161.  
  162. You have access to just about any system-related function or system call,
  163. like ioctl's, fcntl, select, pipe and fork, getc, socket and bind and
  164. connect and attach, and indirect syscall() invocation, as well as things
  165. like getpwuid(), gethostbyname(), etc.  You can read in binary data laid
  166. out by a C program or system call using structure-conversion templates.
  167.  
  168. At the same time you can get at the high-level shell-type operations like
  169. the -r or -w tests on files or `backquote` command interpolation.  You can
  170. do file-globbing with the <*.[ch]> notation or do low-level readdir()s as
  171. suits your fancy.
  172.  
  173. Dbm files can be accessed using simple array notation.  This is really
  174. nice for dealing with system databases (aliases, news, ...), efficient
  175. access mechanisms over large data-sets, and for keeping persistent data.
  176.  
  177. Perl is extensible, and with the next release, will be embeddable. 
  178. People link it with their own libraries for accessing curses, database
  179. access routines, network management routines, or X graphics libraries.
  180. Perl's namespace is more flexible than C's, having a sort of package
  181. notation for public and private data and code as well as
  182. module-initialization routines.
  183.  
  184. Don't be dismayed by the apparent complexity of what I've just discussed.
  185. Perl is actually very easy to learn because so much of it derives from
  186. existing tools.  It's like interpreter C with sh, sed, awk, and a lot
  187. more built in to it.  There's a very considerable quantity of code out 
  188. there already written in perl, including libraries to handle things
  189. you don't feel like reimplementing.  
  190.  
  191. Don't give up your shell programming.   You'll want it for writing
  192. makefiles and making shell callouts in perl, if for no other reason. :-)
  193. My personal rule of thoumb is usually that if I can do the task in under a
  194. dozen or so lines of shell code that is straightforward and clean and not
  195. too slow, then I use the Bourne shell, otherwise I use perl, except for
  196. the occasional task for which C is very clearly the optimal solution.
  197.  
  198. --tom
  199. -- 
  200.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  201.  
  202.  
  203. Emacs is a fine operating system, but I still prefer UNIX. -me
  204.