SPUG: More printing reports

Colin Meyer cmeyer at helvella.org
Mon Feb 11 11:28:43 CST 2002


Hi Walter,

On Mon, Feb 11, 2002 at 09:19:25AM -0700, North, Walter wrote:
> 
> Thanks to Colin for his attempt to aid me but perhaps I did not make 
> myself compeletely clear.  I tried a number of variations but still
> end up the same.  No header on 1st page of my reports after the first
> report.

...

> 
> use FileHandle;
> # loop thru a list of files
> foreach $FILE (@LIST_OF_FILES) {
> 	< Do some stuff like create PRTFILE based on FILE etc > 
> 	open FILE, "<$FILE" or die "blah blah"
> 	open APRTFILE, ">>$PRTFILE" or die "blah blah";
> 	APRTFILE->$format_name("A_FORMAT");
> 	APPRTFILE->$format_top_name("A_FORMAT_TOP");
> 		# loop thru the contents of FILE
> 		while (<FILE>) {
> 		chomp;
> 		<do some stuff to create the output line>
> 		write APRTFILE;
> 	} # end of loop thru a file
> close APRTFILE;
> close FILE;
> } # end of loop thru a list of files

In order to use the method call syntax (APRTFILE->format_name()) with
filehandles, you need to use FileHandle (or IO::File) objects. You
cannot use the traditional filehandles.  

If this doesn't make sense, then a reading of the perlboot manpage
might help.

I've again tried to rearrange your code excerpt to use FileHandle (which
inherit from IO::File) objects.

Have fun,
-C.

Try this [not tested]:

use FileHandle;

foreach $FILE (@LIST_OF_FILES) {
      #< Do some stuff like create PRTFILE based on FILE etc >

      # FILE is a traditional Perl filehandle
      open FILE, "<$FILE" or die "blah blah"

      # $aprtfile is a FileHandle object
      my $aprtfile = FileHandle->new;

      # instead of saying:
      #open APRTFILE, ">>$PRTFILE" or die "blah blah";

      # we say:
      $aprtfile->open(">>$PRTFILE") or die "blah blah";

      # method call syntax doesn't work with traditional filehandles
      #APRTFILE->$format_name("A_FORMAT");

      # use FileHandle objects instead
      $aprtfile->format_name("A_FORMAT");
      $aprtfile->format_top_name("A_FORMAT_TOP");

              # loop thru the contents of FILE
              while (<FILE>) {
              chomp; 
              #<do some stuff to create the output line>

              # this is how we would have done it 
              # with traditional filehandles
              # write APRTFILE;

              # it is done with FileHandle objects like so:
              $aprtfile->write;

      } # end of loop thru a file

      # traditional filehandle:
      # close $aprtfile ;

      # oo style:
      $aprtfile->close or die "Problem writing to aprtfile: $!";
      # always check for possible errors when closing a filehandle that
      # was written to.  

      close FILE;
} # end of loop thru a list of files

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list