Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Using OPEN to append to a file

by /dev/trash (Curate)
on Oct 19, 2002 at 21:41 UTC ( [id://206573] : perlquestion . print w/replies, xml ) Need Help??

/dev/trash has asked for the wisdom of the Perl Monks concerning the following question:

I have this simple piece of code:
use warnings; use strict; use diagnostics; open(LOG,"C:/WINDOWS/U.S. Robotics 56K Voice INT PnP.log") || die "Can +'t open1"; open(ARCHIVE,">>H:/ppp_stats.log") || die "Can't open2"; while(<LOG>){ print ARCHIVE $_; } open(LOG,">C:/WINDOWS/U.S. Robotics 56K Voice INT PnP.log") || die "Co +uldn't truncate logfile!"; close ARCHIVE; close LOG;
Basically all I want is for the US Robotics log file to be appended to the ppp_stats log file since Windows truncates the US Robotics file from time to time. it works great 90% of the time but I sometimes get duplicate lines in the appended file. What am I doing wrong?

Replies are listed 'Best First'.
Re: Using OPEN to append to a file
by jjdraco (Scribe) on Oct 19, 2002 at 23:45 UTC
    have you tried using Tie::File it works great for me when reading and writing to files. Here is what I came up with:
    #!perl use strict; use warnings; use Tie::File; my @log; my @archive; tie @log, 'Tie::File', "C:/WINDOWS/U.S. Robotics 56K Voice INT PnP.log +"; tie @archive, 'Tie::File', "H:/ppp_stats.log"; push @archive, @log; $#log = 0; pop @log; #otherwise the first line of the file #gets left there, untie @log; untie @archive;
    I did acouple of test runs on small files and it worked for me, but you might want to run some test before using it as working code. Feel free to make any edits. I make no promise that this will work for you, but it worked when I ran it.

    Update: I didn't put the use diagnostic in my code, I'm not sure what that does, I've never seen it before.

    Update: as nothingmuch pointed out to me instead of finding the length of @log and subtracting that from $#log, I can just set $#log to 0, much easier :)

    Update: now it won't leave the blank line in the log file, not sure if thats the best way to do it, but it works

    learning Perl one statement at a time.
      I've run it a couple of times and seems to work but it leaves one line in the US Robotics log file, which causes a dupe when it's run again. I just ran it again and the whole US Robotics file was cleared except for that one line.
        should be able to make acouple of small edits to it to get rid of that blank line. Is that blank line a big deal or can you live with it, I for one would like it, then it leaves breaks inbetween writes.

        Update: ok take another look at the code again I added a pop line to get that line off the file, the program was leaving the first element of the array in the file so I pop it off in to nothing, probably not the best thing to do but I'm not sure what else would work. after all by just setting $#log to 0 aren't I just loosing the whole file in memory somewhere. I'm not sure

        learning Perl one statement at a time.
Re: Using OPEN to append to a file
by gjb (Vicar) on Oct 19, 2002 at 22:05 UTC

    It might be a good idea to close the LOG file before opening it with write access. Even if it doesn't solve your problem, at least it's cleaner.

    Hope this helps, -gjb-