You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wouldn't it be cool if...
use autodie;
{
open my $fh, ">", $file;
print $fh @a_lot_of_stuff;
}
Failed when it ran out of disk space? This does.
use autodie;
{
open my $fh, ">", $file;
print $fh @a_lot_of_stuff;
close $fh;
}
But because the implicit close of a lexical filehandle is so convenient
one would not bother with an explicit one.
I'm throwing this out there to see if there's a clever way to implement
it. One is to have open() return a glob object which does an explicit
close() on DESTROY.
This would also offer a solution to another problem...
Can't close(GLOB(0x803704)) filehandle: 'No space left on device'
It would allow open to attach the original filename to the filehandle
object which can be used to generate better error messages down the line.
On the down side, it will break code that expects open() to return a
non-blessed reference. autodie is lexical, but filehandles get passed
around.
One work around would be to take advantage of the IO::Handle magic.
autodie could treat the filehandle as an inside out object to store data
on it and put accessor methods into IO::Handle. It could add an
IO::Handle::DESTROY to do the extra close.
Blarf. While the idea of using IO::Handle::DESTROY works A) Perl sends
out a "helpful" warning about it, which I suppose isn't so bad, but B)
$@ gets lost somewhere along the line and C) the eval block doesn't
fail, it looks at the last evaluated lexical statement inside.
use IO::Handle;
use warnings;
no warnings "redefine";
sub IO::Handle::DESTROY { die "DESTRRRRROY!" }
print "Eval failed.\n" if !eval {
open my $fh, "/dev/null";
};
print "Caught: $@\n";
__END__
(in cleanup) DESTRRRRROY! at - line 5.
Caught:
(in cleanup) DESTRRRRROY! at - line 5 during global destruction.
The text was updated successfully, but these errors were encountered:
Even if this can't be done. It should be documented. It seems reasonable that people will expect that they can clean up the close $fh cruft with the use of autodie. And, they can't.
Migrated from rt.cpan.org#52985 (status was 'open')
Requestors:
From [email protected] on 2009-12-23 00:19:13
:
From [email protected] on 2009-12-23 02:31:20
:
The text was updated successfully, but these errors were encountered: