in reply to <rant>CPAN modules failing to check for write errors</rant>

Printing to a file handle with insufficient disk space does not produce a failure, at least not on my system. Rather, the failure happens when you call close(). A lot of people don't check the return value of close() (including me), but maybe they should.

To test this, I created a 1 MB loopback filesystem and wrote a bit of Perl to write a lot of data to it:

$ dd if=/dev/zero of=./test_small_lo count=1 bs=1M 1+0 records in 1+0 records out $ sudo /sbin/losetup /dev/loop0 ./test_small_lo $ sudo /sbin/mkfs.ext2 /dev/loop0 [ snip output ] $ sudo mount /dev/loop0 /mnt/data $ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/loop0 1003 13 939 2% /mnt/data [ snip other mounted file systems ] $ cd /mnt/data $ sudo touch test_file $ sudo chmod 666 test_file # So I don't have to run code below as root $ perl -e ' > open( FH, ">", "test_file" ) or die $!; > for( 0 .. 10_000_000 ) { print FH 1 or die $! } > close(FH) or die $! ' No space left on device at -e line 3.

I honestly don't see the point in checking print()'s return value. It puts in more code that will only happen for such extremely rare occasions that you can simply discount them.

Update: See my reply to adrianh below.

----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Replies are listed 'Best First'.
Re: Re: <rant>CPAN modules failing to check for write errors</rant>
by adrianh (Chancellor) on Jun 01, 2004 at 16:59 UTC
    Printing to a file handle with insufficient disk space does not produce a failure, at least not on my system.

    It does on some systems. I think it might on yours too if you have $| set or add some "\n"s to the output (I suspect the close is doing the final flush that's putting the file over the limit.)

    I honestly don't see the point in checking print()'s return value. It puts in more code that will only happen for such extremely rare occasions that you can simply discount them.

    This, of course, depends on how you feel about those rare occasions and the impact of data being lost. Some of my nastiest debugging sessions in the past have been dealing with exactly this sort of rare occasion and I would prefer people to take the care to die or whatever when they occurred.

    (not that I think any sort of approval system for CPAN is the right way to go about fixing these sorts of problem)

      Actually, I just realized something:

      $ perl -e ' > die ' Died at -e line 2.

      I mis-counted the lines in my example of printing to a file handle without enough space. The die was occuring at the print after all. I'm not sure if you can rely on this feature to be cross-platform (my particular system is Linux 2.6.5, glibc 2.3.2, Perl 5.8.2).

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.