Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Welcome to the Monastery
 
PerlMonks  

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

by Util (Priest)
on Jan 12, 2007 at 17:56 UTC ( #594422=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?

Untested idea:

  1. Create a subclass of IO::Handle or IO::File with a custom DESTROY subroutine that closes (if open) with error handling.
  2. Use the subclass instead of basic filehandles.


Comment on Re: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?
Re^2: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle?
by eff_i_g (Curate) on Jan 12, 2007 at 20:01 UTC
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 07:25 UTC

    That was my next thought about attacking the problem. Iíd prefer being able to enable my desired behaviour with some sort of pragma somewhere and then not have to do anything special in the affected code, though. If I have to do any extra work at all (and if itís just remembering to instantiate a particular IO class instead of just calling open), Iíd rather stick to doing dumb and simple extra work than adding insufficiently magic complexity to the code.

    Makeshifts last the longest.

      The following triggers IO::Handle::DESTROY. It does not trigger IO::File::DESTROY even though this is a file handle.

      use IO::Handle; # or IO::File *IO::Handle::DESTROY { print "here\n" } { open my $fh, '<', 'a-file' or die; }

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        That seems to be the only reliable approach. You donít even need to load IO::Handle. However, I canít actually get it to complain:

        use strict; sub IO::Handle::DESTROY { my $self = shift; warn +(caller 0)[3]."($self)\n"; close $self or die "$!\n"; } { if( open my $fh, ">", "/dev/full" ) { print { $fh } 1 } } warn "Terminating\n"; __END__ IO::Handle::DESTROY(IO::Handle=IO(0x8130c0c)) Terminating

        The error does occur: if I change the die to a warn, I see the warning. But while die aborts execution (statements after it will not run), I canít make it throw an exception. The conditions from Carping in DESTROY, which was mentioned in another subthread, do not apply either.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2014-04-18 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls