Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for error on implicit close. [rt.cpan.org #52985] #96

Open
toddr opened this issue Jan 16, 2020 · 2 comments
Open

Check for error on implicit close. [rt.cpan.org #52985] #96

toddr opened this issue Jan 16, 2020 · 2 comments
Labels

Comments

@toddr
Copy link
Collaborator

toddr commented Jan 16, 2020

Migrated from rt.cpan.org#52985 (status was 'open')

Requestors:

From [email protected] on 2009-12-23 00:19:13
:

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.

From [email protected] on 2009-12-23 02:31:20
:

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.



@EvanCarroll
Copy link

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.

@toddr
Copy link
Collaborator Author

toddr commented Dec 28, 2023

Needs a PR.

@toddr toddr added the Needs PR label Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants