Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
die() doesn't work, but exit() still does. I tried this, with good results:
#!/usr/bin/perl use strict; use warnings; $| = 1; sub IO::Handle::DESTROY { print "desctructor enter\n"; my $self = shift; close $self or do { warn $!; exit; }; print "destructor exit\n"; } sub something { open ( my $foo, '>', '/dev/full' ); print $foo 'bar'; } print "pre\n"; something(); print "post\n";
You can, of course, adjust the arguments list to make exit() return a failure code to the OS. Unfortunately, that's not going to get caught as an exception, though.

This leaves some possibilities, although none are spectacular if you were counting on catching the die(). You could certainly set a flag before the exit and have final clean-up triggered by an END block if that flag is set. You could call the cleanup right from there in the destructor and exit from the cleanup code. If there's ever a reason not to leave the program on the full disk error, you could trigger your complaining/cleanup code in there and continue.

Depending on how much state you want to handle, you can work around the die getting caught lots of ways. Here's one that works in toy testing, and lets you use it as a module:
package Foo; my $error; END { warn $error if $error; } sub IO::Handle::DESTROY { print "desctructor enter\n"; my $self = shift; close $self || do { $error = $!; exit; }; print "destructor exit\n"; } 1;
You can make the END block do whatever you need it to do to complain/cleanup. Just put use Foo; at the top of your program, and any failed file close triggers exit and what ever's in your END block. For that matter, since setting IO::Handle::DESTROY in the module seems to work pretty well, you could just put your cleanup code in there, too.


Christopher E. Stith

In reply to Re^5: How do I make the garbage collector throw an exception when it fails to auto-close a filehandle? by mr_mischief
in thread How do I make the garbage collector throw an exception when it fails to auto-close a filehandle? by Aristotle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2014-09-01 13:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (11 votes), past polls