Two solutions : 1) print to another file and move it to the first after you close it

2) put the whole content in a @buffer, and save it to the file after you finish.

In your case I would prefer the first. The second one is better when you want to process the contents another time before printing/saving

Just as an example I will show a snippet of a backup script I have. The first part collects the output messages of a program hilighting the interesting lines
. The second is the function that sends that as an email.
{...} foreach(@fsystem){ $fs=$_; print "Backup do $mountpoint{$fs} ($fs)\n"; open (FS,"/sbin/dump $param \/dev/$mountpoint{$fs} 2>&1 |"); push(@messages,("\nBackup de /dev/$mountpoint{$fs} ($fs)\n\n\n +")); while($linha=<FS>){ push(@messages,$linha); if ($linha=~ /DUMP IS DONE/){ print color 'green'; } if ($linha=~/error/)&&($linha=~/ERROR/)&&($linha=~/Error/){ print color 'red'; } print $linha; print color 'reset'; } close(FS); } {...} sub EnviaMail(){ $smtp = Net::SMTP->new('256.256.256.256'); $smtp->mail($sender); $smtp->to($resp); $smtp->data(); $smtp->datasend("To: $resp\n"); $smtp->datasend("From: \"Backup $localhost\" \<$sender>\n"); $smtp->datasend("Subject: Backup $localhost\n"); $smtp->datasend("\n"); foreach $linha (@messages) { $smtp->datasend($linha); } $smtp->dataend(); $smtp->quit; }
Zenn

In reply to Re: Finding a string within a string by zenn
in thread Finding a string within a string by skyler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.