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;
