Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: A way to report open file handles a Perl script has open? -- using ${^LAST_FH} ?

by shmem (Chancellor)
on Apr 05, 2017 at 12:57 UTC ( #1187105=note: print w/replies, xml ) Need Help??


in reply to Re: A way to report open file handles a Perl script has open? -- using ${^LAST_FH} ?
in thread A way to report open file handles a Perl script has open?

Nice. A sub wrapping open before overriding is missing... and for close. Combining your and my approach:

At toplevel:

use Hash::Util::FieldHash qw(id_2obj); my %fd; BEGIN{ Hash::Util::FieldHash::fieldhash %fd; my $open = sub { @_ > 2 ? open $_[0],$_[1],$_[2] : open $_[0], $_[1]; }; my $close = sub { close $_[0] }; *CORE::GLOBAL::open = sub { my $result = $open->(@_); if ($result) { $fd{$_[0]} = join " ",@_[1,2],caller; } $result; }; *CORE::GLOBAL::close = sub { my $result = $close->(@_); if ($result) { $fd{$_[0]} .= " (closed)"; } else { $fd{$_[0]} .= " (close failed)"; } $result; }; } open my $devnull, '>/dev/null' or die; { open my $fh, '>', "blorfldyick" or die; print $fh "foo!\n" or die; print "(1) ", id_2obj($_), " => $fd{$_}\n" for keys %fd; close $fh; print "after close\n"; print "(2) ", id_2obj($_), " => $fd{$_}\n" for keys %fd; } print "after scope:\n"; print "(3) ", id_2obj($_), " => $fd{$_}\n" for keys %fd; open my $devzero, '<', '/dev/zero' or die; # time passes... print "later:\n"; print "(4) ", id_2obj($_), " => $fd{$_}\n" for keys %fd; __END__ (1) GLOB(0xb22190) => >/dev/null main open.pl 29 (1) GLOB(0xb63780) => > blorfldyick main open.pl 32 after close (2) GLOB(0xb22190) => >/dev/null main open.pl 29 (2) GLOB(0xb63780) => > blorfldyick main open.pl 32 (closed) after scope: (3) GLOB(0xb22190) => >/dev/null main open.pl 29 later: (4) GLOB(0xb363b8) => < /dev/zero main open.pl 42 (4) GLOB(0xb22190) => >/dev/null main open.pl 29

Combine with Time::HiRes and you can sort the history.

update: done, made into a package.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
  • Comment on Re^2: A way to report open file handles a Perl script has open? -- using ${^LAST_FH} ?
  • Download Code

Replies are listed 'Best First'.
Re^3: A way to report open file handles a Perl script has open? -- using ${^LAST_FH} ?
by huck (Vicar) on Apr 05, 2017 at 13:14 UTC

    That is a neat trick i need to remember

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1187105]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-04-25 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?