Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Using OPEN to append to a file

by jjdraco (Scribe)
on Oct 19, 2002 at 23:45 UTC ( #206586=note: print w/ replies, xml ) Need Help??


in reply to Using OPEN to append to a file

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

jjdraco
learning Perl one statement at a time.


Comment on Re: Using OPEN to append to a file
Download Code
Re: Re: Using OPEN to append to a file
by /dev/trash (Curate) on Oct 20, 2002 at 00:46 UTC
    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

      jjdraco
      learning Perl one statement at a time.
        Well it wasn't a blank line it. It was like you said later the first element of the array in the file.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://206586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-09-16 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (43 votes), past polls