nysus recently asked whether there was a way to get at all the currently open filehandles.
Together with Discipulus I concocted a module which does that. It records open, close along with their respective time, and drops filehandles from the track record as soon as they get undefined or go out of scope.
package FileHandle::Track;
use Time::HiRes qw(gettimeofday);
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]}->{open} = join " ",@_[1,2],caller;
$fd{$_[0]}->{opentime} = join ".", gettimeofday;
}
$result;
};
*CORE::GLOBAL::close = sub {
my $result = $close->(@_);
$fd{$_[0]}->{close} = join " ", caller;
if ($result) {
$fd{$_[0]}->{close} .= " (closed)";
} else {
$fd{$_[0]}->{close} .= " (close failed)";
}
$fd{$_[0]}->{closetime} = join ".", gettimeofday;
$result;
};
}
sub get_fds {
return { map { id_2obj($_), $fd{$_} } keys %fd };
}
After making that into a module proper (tests, documentation with due credits) I'll upload that to cpan.
Any suggestions, critics, enhancements?
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.