<br><div class="gmail_quote">On 29 July 2011 08:27, Jacinta Richardson <span dir="ltr"><<a href="mailto:jarich@perltraining.com.au" target="_blank">jarich@perltraining.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

On 27/07/11 05:30, Tim Hogard wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have this bit of code and the disk partition /usr/local/tmp is full:<br>
        #!/usr/bin/perl<br>
<br>
        $SIG{PIPE} = sub { die "\nSigPIPE!\n" };<br>
<br>
        open(OUT,">/usr/local/tmp/x$$"<u></u>) or print "cant open $? $!\n";<br>
        print "err $?\n";<br>
<br>
        print OUT  "foo $$ \n" or print "cant print out $? $!\n";<br>
<br>
        print "err $?\n";<br>
        close OUT or print "cant close $? $!\n";<br>
        print "err $?\n";<br>
<br>
</blockquote>
$? doesn't get set during file operations.<br>
<br>
Are you targetting a version of Perl that is more than 11 years old?  If not, you might prefer to write this like:<br>
<br>
Perl 5.6+<br>
<br>
#!/usr/bin/perl<br>
use strict;<br>
use warnings;<br>
use IO::Handle;<br>
use autodie;  # (Perl 5.8.0+)<br>
<br>
open my $fh, ">", "/usr/local/tmp/x$$";        # Check your errors if you're not using autodie<br>
<br>
$fh->autoflush();<br>
<br>
print {$fh} "...." or die "Failed to print: $!";<br>
<br>
close $fh;  # Check error if you're not using autodie<br>
<br>
<br>
<br>
Or Perl 5.14.1+<br>
<br>
#!/usr/bin/perl<br>
use v5.14;<br>
use warnings;<br>
use autodie;<br>
<br>
open my $fh, ">", "/usr/local/tmp/x$$";        # Check your errors if you're not using autodie<br>
<br>
$fh->autoflush();<br>
print {$fh} "...." or die "Failed to print: $!";<br>
<br>
close $fh;  # Check error if you're not using autodie<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
That helps, but it would be best to know as soon as I open the file<br>
so I can put the entire output in a different file on a different<br>
partition.  The unix "open" system call reports "no space" only<br>
when the directory entry can't be extended and has nothing to do<br>
with how much space is left on the disk.<br>
<br>
</blockquote>
<br>
The problem is that Perl is not your operating system, and Perl cannot tell in advance how much data you're going to try to write to disk.  If any.  As such, it cannot catch this when you open the file.  What if there's 1gb of space available.  But you want to only write 100k.  That should be okay, right?  But what if you want to write 3gb?  Sure, if the disk is completely full you want to know in advance, but if that's the case you have bigger problems....  I imagine you'd have to ask your operating system.  Perl's open() is not the right tool here.<br>
</blockquote><div><br></div><div>And then there is the sparse-file capability.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I could rewind but I'm opening for append since there is small<br>
chance two processes could write the same file at the same time.<br>
<br>
</blockquote>
<br>
The better solution is probably locking.  Unless it doesn't matter if multiple processes mix their data.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I can't flush without loading the IO package (which I don't want<br>
to).<br>
<br>
</blockquote>
The IO::Handle package is harmless.  I'm really not sure why you'd be trying to avoid it.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Does anyone have any other ideas?<br>
</blockquote>
<br>
Look into asking the operating system how full that partition is, rather than trying to repurpose tools that aren't made for it.  Also, look into modernising your Perl syntax to reduce future maintenance pain.<div><div>

</div><div><br>
<br>
    J</div></div></blockquote><div><br></div><div>Isn't Perl's IO (from v5.7) almost completely reliant on "PerlIO"... aka, which means that open() is using PerlIO anyway?  So including any IO package has minimal overhead (making the choice of "should I load an IO package?" largely irrelevant)?</div>
<div><br></div><div>regards,</div><div>Mathew Robertson</div></div>