home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5962 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  3.6 KB

  1. Path: sparky!uunet!noc.near.net!bigboote.WPI.EDU!bigboote.wpi.edu!john
  2. From: john@sekrit.WPI.EDU (John Stoffel)
  3. Newsgroups: comp.lang.perl
  4. Subject: Question on use of Next, Redo and Last
  5. Date: 14 Sep 92 17:34:43
  6. Organization: Worcester Polytechnic Institute
  7. Lines: 135
  8. Distribution: world
  9. Message-ID: <JOHN.92Sep14173443@sekrit.WPI.EDU>
  10. NNTP-Posting-Host: sekrit.wpi.edu
  11.  
  12.  
  13. I've been writing a program to parse through a bunch of mail messages
  14. consisting of syserr logs from workstations that we manage on campus
  15. here.  I used some code written by Larry Wall to parse out the machine
  16. the error log came from and some other info, and then the body of the
  17. message which contained the actuall log for that day.  I still don't
  18. totally grok (or even understand!  :-) why the code works.  I've read
  19. the camel book quite a few times, but the section on last, next, etc
  20. is kinda weak without a really good contrived example.  Here is
  21. Larry's code and then mine, just to give an example I can deal with. 
  22.  
  23. Thanks!
  24.  
  25.                     John
  26.  
  27. ----------------------------larry code---------------------------------
  28.  
  29. # Taken from his reply to dana@thumper.bellcore.com
  30.  
  31. $USER = $ENV{'USER'};
  32. open(MAILBOX, "/usr/spool/mail/$USER")
  33.     || die "cannot open mailbox for $USER: $!\n";
  34.  
  35. $/ = "";
  36.  
  37. while (<MAILBOX>) {
  38.     next unless /^From /;
  39.     next unless /\*\*Phone Message\*\*/;
  40.  
  41.     # Got one...
  42.  
  43.     $_ = <MAILBOX>;    # Assuming next paragraph contains this stuff:
  44.     ($from) = /From:  +(.*)/;
  45.     ($comp) = /Company: +(.*)/;
  46.     ($phone) = /Phone: +(.*)/;
  47.     print "F: $from ($phone). M: ";
  48.  
  49.     while (<MAILBOX>) {
  50.     last if /^From /;
  51.     tr/\n/ /;        # or maybe tr/\n/ /s
  52.     print;
  53.     }
  54.     print "\n";
  55.     redo if $_;            # moral equivalent of unget
  56. }
  57.  
  58.  
  59. -----------------------my code-------------------------------------
  60. #!/usr/local/bin/perl
  61. #
  62. # This program reads in a bunch of mail messages from the syserr demons and 
  63. # creates a summary report.  This saves my brain cells and fingers!
  64. # fmterr.pl version 1.0  9/10/92
  65. #
  66.  
  67. $syserr = "/usr1/john/.SYSERR";
  68.  
  69. $/ = "";    # to read in paragraphs of stuff
  70.         # tried using "From ", but that just caught
  71.         # the beginning headers AFTER they went through.
  72.         # maybe redo would do the trick?
  73.  
  74. $\ = "\n";
  75.  
  76. open(SYSERR,$syserr) || die "Can't open $syserr: $!\n";
  77.  
  78. TOP:
  79. while (<SYSERR>) {
  80.     next unless /^From /;
  81.  
  82.     ($hostname) = join('',"--",/Subject: syserr on (\w+)\.*/,"--");
  83.     ($date) = /Date: .*, (.*) \d\d.*/; 
  84.     ($num) = split(' ',$date,1);
  85.     if ($num < 10) {
  86.     $date = '0' . $date;
  87.     }
  88.     $host{$hostname . $date} = $hostname;  # I like being able to do this!
  89.     $date{$hostname . $date} = $date;
  90.  
  91.     while (<SYSERR>) {
  92.     last if /^From /;
  93.  
  94.     $tmp = $tmp . $_;
  95.     }                
  96.     
  97.     $data{$hostname . $date} = $tmp;
  98.     undef($tmp);
  99.  
  100.     redo if $_;
  101. }                 
  102.  
  103. $~ = BODY;
  104.  
  105. foreach $key (sort keys %host) {
  106.     split(/^/,$data{$key});
  107.     undef($count);
  108.     undef($tmp);
  109.     foreach (@_) {
  110.     if (/file: table is full/) {
  111.         $count++;
  112.     }
  113.     else {
  114.         $tmp = $tmp . $_;
  115.     }
  116.     }
  117.     if (defined($count)) {
  118.     $tmp = $tmp . " file: table was full $count times\n\n";
  119.     }
  120.  
  121.     undef($data{$key});
  122.     $data{$key} = $tmp;
  123.     write;
  124. }
  125.  
  126. format BODY =
  127. ---------------------------------------------------------------------------
  128. @<<<<<<<<<<<<<<<                                              --@<<<<<<<<--
  129. $host{$key},$date{$key}
  130.  
  131. @*
  132. $data{$key}
  133. .
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. --
  141. Youth of today!  Join me in a mass rally for traditional mental attitudes!
  142. -------------------------------------------------------------------------------
  143. john@wpi.wpi.edu | Work Station Specialist | Worcester Polytechnic Institute
  144. John Stoffel     | 508-831-5512 (work)     | Worcester, MA  01609
  145.