Where is this pesky newline coming from?

This script processes a custom procmail.log and prints a summary of
activity of a stipulated number of processed messages.
Working from the end of file toward the beginning by using `tac'

I wanted a `--<spc>\n' delimiter between sections. As you see below
this works but somehow all but the last section get an extra newline.

I've gone blind looking or its source.  There isn't much code, so
seems like it should be apparent...

cat procsum.pl
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#!/usr/local/bin/perl -w

use vars qw ($myscript $count @out @pre_out $str $collect_lines @chunk $ii);
($myscript = $0) =~ s:^.*/::; 
$target = "$ENV{'HOME'}/t/var/log/procmail.log";
$proc = "/usr/bin/tac";
 
if(!$ARGV[0] || $ARGV[0] eq "help"){
   usage();
   exit;
}
if($ARGV[0] !~ /^[0-9]+$/ ){
   usage();
   exit;
}

$count = shift;

if($ARGV[0]){
   usage();
   exit;
}
open(PROC,"$proc $target|")or die "Cannot start PROC $target: $!";
$cnthits = 0;
while(<PROC>){ 
   chomp;
   if(/^STOP/){
      $collect_lines = "TRUE";
   }
   if($collect_lines && 
/^( +Folder:| Subject: |From |Message-[Ii][Dd])/){
      push @chunk,$_;
   }
   if(/^START/){
      $cnthits++;
      $collect_lines = '';
      @rev_chunk = reverse @chunk;
      print "<@rev_chunk> <@chunk>\n";
      @chunk = '';
      $str = join("\n",@rev_chunk);
      push @pre_out, "$str" ;
      $str = @rev_chunk = '';
   }
   last if($cnthits == ($count));
}
@out = reverse @pre_out;
for(@out){
  print "$_\n-- \n";
}
sub usage {
   print <<EOM;

Purpose: Print a summary of specified number of entries from 
         procmail.log.  Starting from the end.

Usage: \`$myscript NUM'

EOM
} 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

OUPUT is filtered thru `sed -n l' to make it apparent what is happening.
(PS - There are no blank lines in input file)

The first two output sections have an extra newline

 $ procsum.pl 3 |sed -n l
>From [EMAIL PROTECTED]  Tue Jun 24 22:20:04 2003$
 Subject: =?GB2312?B?tL/M7Mi71rLO77+5yfrL2NTCytXI6zEwq1xLL6xrc=?=$
  Folder: spam_from_newsguy1.in\t\t\t\t\t\t   2136$
Message-Id: <[EMAIL PROTECTED]>$
$
-- $
>From [EMAIL PROTECTED]  Tue Jun 24 22:40:05 2003$
 Subject: =?GB2312?B?x+u49867tPO457TzvePDx8X1uPbIy7Oho6E=?=$
  Folder: spam_from_newsguy1.in\t\t\t\t\t\t   2246$
Message-Id: <[EMAIL PROTECTED]>$
$
-- $
>From [EMAIL PROTECTED]  Tue Jun 24 22:45:10 2003$
 Subject: =?gb2312?q?_=CF=FA=CA=DB201=BC=D7=BB=F9=B9=E8=D3=CD?=$
  Folder: spam_from_newsguy1.in\t\t\t\t\t\t   8015$
Message-Id: <[EMAIL PROTECTED]>$
-- $



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to