removed redundant data structure part in commit count
[scpubgit/System-Introspector.git] / lib / System / Introspector / Probe / FileHandles.pm
1 package System::Introspector::Probe::FileHandles;
2 use Moo;
3
4 use System::Introspector::Util qw(
5     handle_from_command
6     transform_exceptions
7 );
8
9 has lsof_command => (is => 'ro', default => sub { 'lsof' });
10
11 sub gather {
12     my ($self) = @_;
13     return transform_exceptions {
14         my $pipe = $self->_open_lsof_pipe;
15         my @handles;
16         while (defined( my $line = <$pipe> )) {
17             chomp $line;
18             my @fields = split m{\0}, $line;
19             push @handles, { map {
20                 m{^(.)(.*)$};
21                 ($1, $2);
22             } @fields };
23         }
24         return { handles => \@handles };
25     };
26 }
27
28 sub _open_lsof_pipe {
29     my ($self) = @_;
30     my $lsof = $self->lsof_command;
31     return handle_from_command "$lsof -F0";
32 }
33
34 1;
35
36 __END__
37
38 =head1 NAME
39
40 System::Introspector::FileHandles - Gather opened filehandles
41
42 =head1 DESCRIPTION
43
44 Uses C<lsof> to build a list of open filehandles.
45
46 =head1 SEE ALSO
47
48 =over
49
50 =item L<System::Introspector>
51
52 =back
53
54 =cut
55