in reply to Limit a filesize

The easiest way may be the new module Tie::File. Stat the file, if it's too big or will be soon (say with what you are about to append), shift/splice the array, et voila! (I'd simply remove n entries each time, instead of calculating for exact size).

You can predict size with a stat / -s and then adding length($message).

--
perl -pe "s/\b;([mnst])/'\1/mg"

Replies are listed 'Best First'.
Re: Re: Limit a filesize
by Flame (Deacon) on Apr 09, 2002 at 02:27 UTC
    I did a short experement with a text file I have, basicaly I told it to print out the size of the file, and then open it, read it and print the length... they didn't match. I'll admit, I'm currently stuck on Win32, but the file was saved in UNIX (I love TextPad). So I don't quite know what to make of that, or did I misunderstand what you were saying?



    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

      You did misunderstand, or I was unclear. First and foremost the size of a file is not the same as the size of it's contents (especially on some filesystems). But for all but the strictest of purposes it's close enough. What I suggested was something like:
      #About to append $message, do we have enough room? if( -s $file + length($message) > $maxSize ){ #do magic here }

      --
      perl -pe "s/\b;([mnst])/'\1/mg"

        Oh, I think I understand now, basically, it's just a good approximation, not precise. Ok, thanks. I'll see what I can do with the information you guys gave me (including those of you in other sub-threads.)

        Thanks



        "Weird things happen, get used to it."

        Flame ~ Lead Programmer: GMS