in reply to Re^3: Is there a way to avoid copy function from overwriting old contents?
in thread Is there a way to avoid copy function from overwriting old contents?

Hi Marmot, Thanks for the help. I was able to get my problem solved . Using this code
if (true) {open (my $fh, ">> RESULT_LOG"); copy "Battery_Status", $fh;} else {print "Error";}
I was not storing it in a variable because it can contain a lot of information, since its an output coming after the execution of a command on a hardware device ! So had to store in a file and also I am parsing it for some keyword and if thats true then the above code is executed to append the result from the file to the log.
  • Comment on Re^4: Is there a way to avoid copy function from overwriting old contents?
  • Download Code

Replies are listed 'Best First'.
Re^5: Is there a way to avoid copy function from overwriting old contents?
by furry_marmot (Pilgrim) on Apr 12, 2011 at 19:59 UTC
    Why do you use File::Copy? I tested your code and, to my surprise, copy does append to a file if you open it for appending and copy to the filehandle.
    copy "file1", $fh
    But it's not documented in the POD, and it only works as a side effect of how File::Copy deals with filehandles. Why not do as others have suggested:
    if (true) { open (my $fh, ">> RESULT_LOG"); open BATTERY, "<Battery_Status"; print $fh <BATTERY>; close BATTERY; } else {print "Error"}
    It's a couple of extra lines, but you can use all built-ins, obviating the need for an external module.

    My two cents.

    --marmot