Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

by furry_marmot (Pilgrim)
on Apr 12, 2011 at 17:43 UTC ( #899011=note: print w/replies, xml ) Need Help??

in reply to Re^2: 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?

#!/usr/bin/perl use strict; use warnings; use File::Copy; #change this to 0 and execute, then back to one and execute my $value0 = 1; my $value1 ='true'; ## marmot: open for overwriting open (INFO, "> tmp"); print INFO "this is simple\n"; close (INFO); ## marmot: open RESULT_LOG for appending open (FILE, ">> RESULT_LOG") ; print FILE " STATUS obtained : $value1\n"; # If true then copies the information to the RESULT_LOG ## marmot: translation --> if $value0 is true, overwrite RESULT_LOG wi +th tmp. ## By the way, <FILE>, which is RESULT_LOG, is still open for +appending, ## so this is probably a bad idea. if ($value0 == 1) { copy("tmp","RESULT_LOG");} ## marmot: Otherwise, append RESULT_LOG with text. else { print FILE "Check keyword supplied\n"; } close(FILE);
Apart from confusing copying with appending, and trying to overwrite a file that's already open for appending, this code can be made to work with the changes below.
if ($value0 == 1) { open INFO, "<tmp"; print FILE <INFO>; # Appends to RESULT_LOG else { print FILE "Check keyword supplied\n"; } close(FILE); close(INFO);

I realize this is just a snippet to show what you're trying to accomplish, but I have to wonder if it makes sense to open 'tmp' and write text to it, when you're just going to append it to another file later. Why not put this information in a variable and keep it around until you need to write it to a file?

Also, for the sake of your own clarity, you might want to give your file handles names that are closer to the file names they represent, or perhaps the functions they perform. E.g.,

open TMP, "<tmp"; open LOG, ">>RESULT_LOG"; open RESULTS, ">>RESULT_LOG";


Replies are listed 'Best First'.
Re^4: Is there a way to avoid copy function from overwriting old contents?
by justkar4u (Novice) on Apr 12, 2011 at 18:01 UTC
    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.
      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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://899011]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-04-24 01:52 GMT
Find Nodes?
    Voting Booth?