#!/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`) ;

foreach () {
   # this version uses boolean interpretation of expressions as
   # a cute substitution for the if in previous versions
   /Ignore/ && do { print SCRAPS; next } ;
   /\*DATE\*/ && s/\*DATE\*/$date/g ; 
   if ( /#/ ) {             # no need to escape # in patterns 
      @parts = split ("#"); # or in strings
      print OUTF "$parts[0]\n" ;
      print SCRAPS "#" . $parts[1,$#parts] ; #  prints range of array elts
   } else {
      print OUTF ;
      } 
}

close INF ;
close OUTF ;
close SCRAPS ;