Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: limit output filesize

by Transient (Hermit)
on Jul 08, 2005 at 18:57 UTC ( [id://473540]=note: print w/replies, xml ) Need Help??


in reply to limit output filesize

I ran this as a test:
#!/usr/bin/perl use warnings; use strict; my $file_base = "output"; open( FILE, ">$file_base" ) or die "Unable to open $file_base for writ +ing!\n$!\n"; for (1..100) { print FILE "0" x 1024; print "File Size: ".(stat($file_base))[7]."\n"; last if (stat($file_base))[7] > 50000; } close FILE or die "Error closing $file_base\n$!\n";
the output was as follows (abridged):
File Size: 0 File Size: 0 File Size: 0 File Size: 0 File Size: 4096 File Size: 4096 File Size: 4096 File Size: 4096 ... File Size: 49152 File Size: 49152 File Size: 49152 File Size: 53248
So it stopped close to, but not at, 50k because while the file is open, only full blocks are written. You may want to keep that in mind and make sure the cutoff is at the highest blocksize that is less than your threshold.

This is AIX 5.1 on a JFS2 file system. I won't be able to speak for others

Update:
as an aside $|++ did nothing for this
You could also keep tabs on your output size as well, if it is all under your control.

Replies are listed 'Best First'.
Re^2: limit output filesize
by rogue90 (Novice) on Jul 08, 2005 at 19:12 UTC
    I see - I was using the filehandle for stat. Thanks much!
      Urm... the filehandle for stat, actually for -s, works for me.
      open OUT, ">test.txt"; for (1 .. 1000) { print OUT "Hello, Perlmonks!\n" x 10; print -s OUT; }
      It works, but -s is indeed set according to the buffer size, and not according to the already printed output:
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      4096
      4096
      4096
      4096
      4096
      4096
      4096
      4096
      ...
      

      With $| set to a true value it'll work less coarse, but most likely quite a bit slower:

      open OUT, ">test.txt"; my $fh = select OUT; $| = 1; select $fh; for (1 .. 1000) { print OUT "Hello, Perlmonks!\n" x 10; print -s OUT; }
      192
      384
      576
      768
      960
      1152
      1344
      1536
      1728
      1920
      2112
      2304
      ...
      
      I think the coarse version is still fine enough.
        The coarse version is indeed fine enough. I can limit how frequently I stat which should speed things up a bit. Thanks for your help.
        I was just about to say that - if I don't have 1024 chars per line and have say 100 instead it doesn't work the same.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found