home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7722 < prev    next >
Encoding:
Text File  |  1993-01-08  |  6.3 KB  |  131 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!gatech!destroyer!cs.ubc.ca!uw-beaver!fluke!inc
  3. From: inc@tc.fluke.COM (Gary Benson)
  4. Subject: Re: PERL
  5. Message-ID: <1993Jan8.175041.5709@tc.fluke.COM>
  6. Keywords: perl
  7. Organization: John Fluke Mfg. Co., Inc., Everett, WA
  8. References: <1993Jan6.212656.23814@lynx.dac.northeastern.edu>
  9. Date: Fri, 8 Jan 1993 17:50:41 GMT
  10. Lines: 119
  11.  
  12. In article <1993Jan6.212656.23814@lynx.dac.northeastern.edu> efroyman@lynx.dac.northeastern.edu (Ella Froyman) writes:
  13. >Hi netters;
  14. >
  15. >I am new to perl and can you clarify some things for me:
  16. >
  17. >(1) What does a $variable| mean?
  18. >(2) What does $source++ do?
  19. >    !(system ("mt -f $source fsf 1") >> 8) || &tape_fail();
  20. >
  21. >Thanks I know this are basic questions but i am new to PERL and
  22. >languages.
  23. >
  24. >
  25. >Send all replies to my email
  26. >Ella
  27.  
  28.  
  29. I did send an answer via email, but then a local expert I had asked for help
  30. in figuring out that last line suggested I should post a reply, too. Here is
  31. pretty close to what I said in my email to Ella:
  32.  
  33. First I asked for assurance that this was not a *HOMEWORK* assignment :-)
  34.  
  35. 1. $variable| probably means that the value of $variable is going to be a
  36.    command whose output will be piped back into the calling program. I
  37.    explained that the | symbol is also used for the bitwise OR operator, to
  38.    indicate alternatives in regular expressions, and as a centering
  39.    specifier on picture lines in formats, but that in none of those cases do
  40.    you typically see a variable. I told her that for a precise answer, she
  41.    should supply at least the entire line that the chunk appeared on, and
  42.    preferably a few surrounding lines, too. To illustrate the use, I showed
  43.    her the following two lines from R. Schwartz' zap program:
  44.  
  45.     $pscmd = $BSD ? "ps -auxww" : "ps -ef";
  46.     open(PS, "$pscmd|") || die "can't run $pscmd: $!";
  47.  
  48. I explained that the first line sets the ps command variable ($pscmd) to be
  49. "ps -auxww" if the system is a BSD one (or at least if the variable BSD is
  50. defined, which presumably the program assures only happens in a BSD system),
  51. but anyway. If the system is not a BSD one, the ps command will be "ps -ef".
  52.  
  53. In the second line, a filehandle called PS is opened to catch the output of
  54. the ps command, accomplished by ending the filename associated with the
  55. filehandle with the pipe symbol. Or die. 
  56.  
  57. I should have also mentioned that this "filename" really is a filename
  58. usually, but that a pipe symbol on one side or the other tells the 'open'
  59. function to interpret it as a command whose output will be piped in to the
  60. program (as it does in this instance), or to which the program will pipe
  61. output, if the pipe comes first. In addition, real filenames can have other
  62. such symbols associated with them by 'open': > to open the file for output,
  63. >> for opening in append mode, and < to open it for input.
  64.  
  65. (I did mention that both the if-then-else shorthand in the first line and
  66. the open-filehandle-piped command idiom on the second line are valuable
  67. tools to remember, and that she would probably find herself using them
  68. frequently. I also told her that if this is indeed a *homework* assignment,
  69. she should demand extra credit for uncovering these :-)
  70.  
  71. 2. $source++ just means to increment the value of the variable by one, and
  72.    is similar to ++$source, except with regards to when it performs the
  73.    increment. I forgot to mention that this autoincrement operator has a
  74.    little built in magic... if the value of the variable is a string, rather
  75.    than a number, "A" increments to "B", and "zz" to "aaa", and so on.
  76.  
  77. 3. I was not sure why Ella had included the line
  78.  
  79.      !(system ("mt -f $source fsf 1") >> 8) || &tape_fail();
  80.  
  81.     in her posting, but I assumed she wanted to know what it did. So I
  82.     called up a local Perl guru, and after I thought I understood it, I
  83.     wrote something like the following translation:
  84.  
  85.     Ignore the ! negation at the beginning for a moment. The "system" is to
  86.     execute the program "mt", which I discovered is a magnetic tape drive
  87.     control program. My man page did not indicate the meaning of the -f
  88.     option, but I presumed it to mean "force" as it frequently does in unix
  89.     programs; in other words, finish running even if errors occur. This mt
  90.     program will run on the device specified in the variable $source, and
  91.     fast forward until it reaches the first end-of-file marker (fsf 1). It
  92.     looks like this will cause the first file encountered to be skipped.
  93.     (Perhaps a preamble?)  The output of the system command will be shifted
  94.     right 8 bits by the bitwise >> operator; therefore the exit status will
  95.     be the result of evaluating this entire expression (system ... 8).
  96.     
  97.     The resulting number is 0 if the mt command succeeded, and non-zero
  98.     otherwise. Recalling that this entire expression is to be negated, a
  99.     non-zero value then becomes true, so the OR part is not evaluated 
  100.     (|| &tape_fail() means "or run the subroutine tape_fail).  However, if
  101.     the value of the shifted right status is non-zero, this means that the
  102.     mt command failed. When non-zero is negated, it becomes false, so the
  103.     program should evaluate the OR conditional and do that subroutine.
  104.  
  105.     In retrospect, I could have probably figured it out myself if I had kept
  106.     the words of L. Wall in mind, "In general, when confronted by a complex
  107.     expression, analyze it from the inside out to see what order things
  108.     happen."
  109.  
  110. I thanked her for her posting because it taught me something, and because it
  111. gave me an opportunity to give back some of the HUGE amounts of help I
  112. myself have received in studying Perl. I may not have explained all this in
  113. perfect technical terms, but it makes sense to me, and I hope it will also
  114. make sense to Ella. Please correct any errors I have made here, and expand
  115. on anything that may be unclear.
  116.  
  117. As I rewrote this, it occurred to me that I should also mention one other
  118. piece of information that all new Perl learners should be aware of:
  119.  
  120.     ISBN 0-937175-64-1
  121.     
  122. This is the number of the book called "Programming Perl" by Larry Wall and
  123. Randal Schwartz, a couple of guys who seem to know quite a bit about the
  124. subject.  It has a camel on the front. Get two copies.
  125.  
  126. -- 
  127. Gary Benson   -_-_-_-_-_-_-_-_-_-inc@sisu.fluke.com_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  128.  
  129. There are things that are so serious that you can only joke about them.
  130.                                                            -Heisenberg
  131.