Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

•Re: Catching errors in closing lexical filehandles

by merlyn (Sage)
on Sep 27, 2004 at 11:57 UTC ( #394129=note: print w/ replies, xml ) Need Help??


in reply to Catching errors in closing lexical filehandles

It's actually rather simple.

sub IO::Handle::DESTROY { my $deadbody = shift; not defined fileno $deadbody or close $deadbody or die "Cannot close $deadbody: $!"; }
Of course, this is a global change, but that's life. You can make it regional by putting it inside a local setting:
{ local *IO::Handle::DESTROY = sub { ... }; { open my $fh, "..."; ... } # will trigger here }
The nested parens here ensure that the handle is destroyed before the local sub assignment goes out of scope.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on •Re: Catching errors in closing lexical filehandles
Select or Download Code
Re^2: Catching errors in closing lexical filehandles
by gaal (Parson) on Sep 27, 2004 at 12:14 UTC
    Interesting: when I print ref $fh, I get GLOB, not IO::Handle. Is there some special magic at work here?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-09-19 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (149 votes), past polls