in reply to Finding a string within a string

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