mhoang has asked for the wisdom of the Perl Monks concerning the following question:

I write the script to generate the report, I need to add data to the file,and create the heading if it is a new page. I donot know why it keeps addding the heading to the file every time I run the script.I do not know what is wrong with my script, pls help me, thanks

if($status == 0) { $Msg=<<MSG; Copy is successful!!! MSG sendAdminMessage($Msg, \@emailAddressAdmins); #Generate the log file open(OUTFILE, ">>$outputFile") || die "Unable to open file[$outputFile +] for writing: $!\n"; $| = 1; # Auto-flush output = on print "page number [$=]\n"; write OUTFILE; close OUTFILE; # Output formatting: ========================================================= format OUTFILE_TOP = Staff Extra Bulletin Log Pg @<< $% Filename Last-Updated Current ====================================================== . format OUTFILE = @<<<<<<<<<<<<< @<<<<<<< @<<<<<<<< $fileone,$oldTime,$newTime . + } else { exit(0); }

Replies are listed 'Best First'.
Re: append text to a file
by cdarke (Prior) on Aug 04, 2011 at 07:29 UTC
    Perl and the format command have no knowledge of what is already in the file, it does not magically remember that you already added a heading, it assumes you are running from the start.

    You need to keep a record of the line count yourself somewhere, either in this file or another, or read the whole file each time to figure out what $- should be.
      Thank u. Got it !
Re: append text to a file
by Marshall (Canon) on Aug 05, 2011 at 14:09 UTC
    Another approach is to split the data collection from the report generation. Put this data: $fileone,$oldTime,$newTime out using a .cvs format. Read that file and generate a report for the humans whenever you need to.