AnyEvent-Subprocess

 view release on metacpan or  search on metacpan

lib/AnyEvent/Subprocess/Job/Delegate/Handle.pm  view on Meta::CPAN

    default => sub {
        require AnyEvent::Subprocess::Running::Delegate::Handle;
        return 'AnyEvent::Subprocess::Running::Delegate::Handle';
    },
);

has 'handle' => (
    traits     => ['NoClone'],
    is         => 'ro',
    isa        => 'AnyEvent::Subprocess::Handle',
    lazy_build => 1,
);

sub _build_pipes {
    my $self = shift;
    my $direction = $self->direction;

    if ($direction eq 'rw') {
        return [ portable_socketpair() ];
    }
    elsif ( $direction eq 'r' ) {
        return [ portable_pipe() ];
    }
    else {
        return [ reverse (portable_pipe()) ];
    }
}

sub _build_handle {
    my $self = shift;

    my ($name, $direction) = map { $self->$_ } qw/name direction/;

    return $self->__build_handle(
        $self->pipes->[0],
        _direction => $direction,
        _name      => "parent $name handle ($direction)",
    );
};

sub build_run_delegates {
    my $self = shift;
    return $self->run_delegate_class->new(
        name           => $self->name,
        direction      => $self->direction,
        handle         => $self->handle,
        want_leftovers => $self->want_leftovers,
    );
}

sub parent_finalize_hook {
    my $self = shift;

    close $self->pipes->[1];
}

sub child_setup_hook {
    my $self = shift;

    my $name = $self->handle->name;
    $self->handle->do_not_want; # DO NOT WANT (in child)

    # reopen fake fds to the "real" ones

    my $ch = $self->pipes->[1];

    if ($self->has_replace) {
        my $replacement = ref $self->replace ?
          fileno($self->replace) :
            $self->replace;

        dup2( fileno($ch), $replacement )
          or confess "failed to dup $name to $replacement: $!";
    }

    AnyEvent::Util::fh_nonblocking $ch, 0;
}

sub build_code_args {
    my $self = shift;
    if ($self->pass_to_child){
        return $self->name => $self->pipes->[1];
    }
    return;
}

sub parent_setup_hook {}
sub child_finalize_hook {}
sub receive_child_result {}
sub receive_child_error {}

__PACKAGE__->meta->make_immutable;

1;



=pod

=head1 NAME

AnyEvent::Subprocess::Job::Delegate::Handle - share a filehandle or socket with the child

=head1 VERSION

version 1.102912

=head1 INITARGS

=head2 direction

'r' for a pipe from the child to the parent, 'w' for a pipe from the
parent to the child, 'rw' for a socket.

=head2 replace

Optional.  If specified, can be a Perl filehandle (C<\*STDERR>) or
integer fd number (C<2>).  This filehandle will be opened to the object
created by this delegate in the child.  (So if you say
C<< direction => 'r', replace => \*STDERR >>, the parent will be able to
read the child's STDERR via this delegate.  If you say



( run in 1.108 second using v1.01-cache-2.11-cpan-39bf76dae61 )