Padre

 view release on metacpan or  search on metacpan

lib/Padre/Task/Run.pm  view on Meta::CPAN

package Padre::Task::Run;

# Generic task for executing programs via system() and streaming
# their output back to the main program.

use 5.008005;
use strict;
use warnings;
use Params::Util ();
use Padre::Task  ();
use Padre::Logger;

our $VERSION = '1.02';
our @ISA     = 'Padre::Task';

sub new {
	TRACE( $_[0] ) if DEBUG;
	my $class = shift;
	my $self  = $class->SUPER::new(@_);

	# Params and defaults
	$self->{timeout} ||= 10;
	unless ( Params::Util::_ARRAY( $self->{cmd} ) ) {
		die "Failed to provide command to execute";
	}

	return $self;
}

sub run {
	TRACE( $_[0] ) if DEBUG;
	my $self = shift;

	# Set up for execution
	require IPC::Run;
	my $timeout = IPC::Run::timeout( $self->{timeout} );
	my $stdin   = '';
	my $stdout  = '';
	my $stderr  = '';

	# Start the process and wait for output
	TRACE( "Running " . join( @{ $self->{cmd} } ) ) if DEBUG;
	my $handle = IPC::Run::start(
		$self->{cmd},
		\$stdin,
		\$stdout,
		\$stderr,
		$timeout,
	);

	# Wait for output and send them to the handlers
	local $@ = '';
	eval {
		while (1)
		{
			if ( $stdout =~ s/^(.*?)\n// ) {
				$self->stdout("$1");
				next;
			}
			$handle->pump;
		}
	};
	if ($@) {



( run in 1.042 second using v1.01-cache-2.11-cpan-140bd7fdf52 )