#!/usr/local/bin/perl

$infile = "foo.txt" ;
$outfile = "bar.txt" ;
$scrapfile = "baz.txt" ;

open(INF,"<$infile") || die "Can't open $infile for reading" ;
open(OUTF,">$outfile") || die "Can't open $outfile for writing" ;
open(SCRAPS,">$scrapfile") || die "Can't open $scrapfile for writing" ;

chop($date = `date`) ;

# this version uses the implicitly defines $_ variable
foreach () {
   if ( /Ignore/ ) { 
      print SCRAPS ;
      next; 
      }
   if ( /\*DATE\*/ ) { 
      s/\*DATE\*/$date/g ; 
      }
   if ( /\#/ ) { 
      @parts = split ("#");
      print OUTF "$parts[0]\n" ;
      print SCRAPS "#" . $parts[1,$#parts] ;  # prints range of array elts
   } else {
      print OUTF ;
      } 
}

close INF ;
close OUTF ;
close SCRAPS ;