Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?

by polettix (Vicar)
on Jan 13, 2007 at 20:15 UTC ( #594567=note: print w/ replies, xml ) Need Help??


in reply to How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?

In Linux:

PolettiX:~# dd if=/dev/zero of=testfile bs=1024 count=1024 1024+0 records in 1024+0 records out 1048576 bytes transferred in 0.019164 seconds (54715967 bytes/sec) PolettiX:~# PolettiX:~# PolettiX:~# mkfs.ext2 testfile mke2fs 1.37 (21-Mar-2005) testfile is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 128 inodes, 1024 blocks 51 blocks (4.98%) reserved for the super user First data block=1 1 block group 8192 blocks per group, 8192 fragments per group 128 inodes per group Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. PolettiX:~# PolettiX:~# PolettiX:~# mount testfile -o loop /mnt

Now you have a filesystem that will be easy to fill up:

#!/usr/bin/perl use strict; use warnings; use Fatal qw( open close ); chdir '/mnt'; { open my $fh, '>', 'prova.dat'; print {$fh} "ciao\n" for 1 .. 1000000; # close $fh; } print {*STDERR} "should be closed here!\n";

In my system, the final string on STDERR gets printed when the close above is commented; uncommenting the close triggers the fatal error:

Can't close(GLOB(0x814cd4c)): No space left on device at (eval 2) line + 3 main::__ANON__('GLOB(0x814cd4c)') called at ./prova.pl line 13

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.


Comment on Re: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?
Select or Download Code
Replies are listed 'Best First'.
Re^2: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?
by Aristotle (Chancellor) on Jan 13, 2007 at 23:51 UTC

    In my system, the final string on STDERR gets printed when the close above is commented; uncommenting the close triggers the fatal error:

    And thatís exactly the point: when you have an explicit close, you can make it throw an exception; when you omit the close, the error is silent. But I want to omit the close and still get an exception. Fatal wonít help me there.

    Thanks for the pointer about the loop device though! All I can say in retrospect is, díuh. However, Linux has an easier way, mentioned by Zaxo in the old thread from gaal: thereís a /dev/full device where writing always fails with ENOSPC.

    Makeshifts last the longest.

      Oh, I misunderstood that you didn't know for sure about Fatal's behaviour:
      In the simplest case, use Fatal qw( :void close ); would work for this, but I strongly doubt it, and in any case I canít think of a good way to purposefully trigger a close failure in order to test it.
      And yes... the loop device rocks :)

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.

        NoÖ

        $ perl -MFatal=open,close,syswrite
        -e'open $fh,">/dev/full"; syswrite $fh, 1; close $fh'
        Can't syswrite(GLOB(0x8130c24), 1): No space left on device at (eval 3) line 3
        	main::__ANON__('GLOB(0x8130c24)') called at -e line 1
        $ perl -MFatal=open,close
        -e'open $fh,">/dev/full"; print { $fh } 1 or die "$!\n"; close $fh'
        Can't close(GLOB(0x8130c24)): No space left on device at (eval 2) line 3
        	main::__ANON__('GLOB(0x8130c24)') called at -e line 1

        Ö not with buffered I/O.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (17)
As of 2015-07-28 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls