AnyEvent-Run

 view release on metacpan or  search on metacpan

lib/AnyEvent/Run.pm  view on Meta::CPAN

    if ( AnyEvent::WIN32 ) {
        eval { require Win32::API };
        die "Win32::API failed to load:\n$@" if $@;
        
        eval { require Win32::Process };
        die "Win32::Process failed to load:\n$@" if $@;
        
        # ABOVE_NORMAL_PRIORITY_CLASS and BELOW_NORMAL_PRIORITY_CLASS aren't
        # provided by Win32::Process so their values have been hardcoded.
        $pri = $pri <= -16 ? Win32::Process::HIGH_PRIORITY_CLASS()
             : $pri <= -6  ? 0x00008000 # ABOVE_NORMAL
             : $pri <= 4   ? Win32::Process::NORMAL_PRIORITY_CLASS()
             : $pri <= 14  ? 0x00004000 # BELOW_NORMAL
             :               Win32::Process::IDLE_PRIORITY_CLASS();
        
        my $getCurrentProcess = Win32::API->new('kernel32', 'GetCurrentProcess', ['V'], 'N');
        my $setPriorityClass  = Win32::API->new('kernel32', 'SetPriorityClass',  ['N', 'N'], 'N');
        
        my $processHandle = eval { $getCurrentProcess->Call(0) };
        
        if ( !$processHandle || $@ ) {
            carp "Can't get process handle ($^E) [$@]";
            return;
        }
        
        eval { $setPriorityClass->Call($processHandle, $pri) };
        
        if ( $@ ) {
            carp "Couldn't set priority to $pri ($^E) [$@]";
        }
    }
    else {
        eval {
            unless ( setpriority( 0, $$, $pri ) ) {
                die "unable to set child priority to $pri\n";
            }
        };
        carp $@ if $@;
    }
}

sub DESTROY {
    my $self = shift;
    
    # XXX: doesn't play nice with linger option, so clear wbuf
    $self->{wbuf} = '';
    
    $self->SUPER::DESTROY(@_);
    
    if ( $self->{child_pid} ) {
        kill 9 => $self->{child_pid};
        waitpid $self->{child_pid}, 0;
    }
}

1;
__END__

=head1 NAME

AnyEvent::Run - Run a process or coderef asynchronously

=head1 SYNOPSIS

    use AnyEvent;
    use AnyEvent::Run;
    
    my $cv = AnyEvent->condvar;

    my $handle = AnyEvent::Run->new(
        cmd      => [ 'ls', '-l' ],
        priority => 19,              # optional nice value 
        on_read  => sub {
            my $handle = shift;
            ...
            $cv->send;
        },
        on_error  => sub {
            my ($handle, $fatal, $msg) = @_;
            ...
            $cv->send;
        },
    );
    
    # Send data to the process's STDIN
    $handle->push_write($data);

    $cv->recv;

=head1 DESCRIPTION

AnyEvent::Run is a subclass of L<AnyEvent::Handle>, so reading it's
documentation first is recommended.

This module is designed to run a child process, using an explicit
command line, a class name, or a coderef.  It should work on any
Unix system as well as Windows 2000 and higher.

For an alternate way of running a coderef in a forked process using
AnyEvent, see L<AnyEvent::Util>'s fork_call function.

=head1 METHODS

=head2 $handle = new( %args )

Creates and returns a new AnyEvent::Run object.  The process forks and either
execs (Unix) or launches a new process (Windows).  If using a coderef, the
coderef is run in the forked process.

The process's STDIN, STDOUT, and STDERR and connected to $handle->{fh}.

The child process is automatically killed if the AnyEvent::Run object goes out
of scope.

See L<AnyEvent::Handle> for additional parameters for new().

=over 4

=item cmd

Required. Takes a string, an arrayref, or a code reference.



( run in 0.536 second using v1.01-cache-2.11-cpan-df04353d9ac )