package System::Introspector::FileHandles;
use Moo;

sub gather {
    my ($self) = @_;
    my $pipe = $self->_open_lsof_pipe;
    my @handles;
    while (defined( my $line = <$pipe> )) {
        chomp $line;
        my @fields = split m{\0}, $line;
        push @handles, { map {
            m{^(.)(.*)$};
            ($1, $2);
        } @fields };
    }
    return \@handles;
}

sub _open_lsof_pipe {
    my ($self) = @_;
    my $command = 'lsof -F0';
    open my $pipe, '-|', $command
        or die "Unable to open pipe to '$command': $!\n";
    return $pipe;
}

1;

__END__

=head1 NAME

System::Introspector::FileHandles - Gather opened filehandles

=head1 DESCRIPTION

Uses C<lsof> to build a list of open filehandles.

=head1 SEE ALSO

=over

=item L<System::Introspector>

=back

=cut

