Arch
view release on metacpan or search on metacpan
perllib/Arch/Run.pm view on Meta::CPAN
# file handles to poll for.
while (exists $SUBS{$pid} && poll(undef)) {}
}
# returns undef if childs exit has already been handled
return $ret;
}
sub killall (;$) {
my $signal = shift || 'INT';
kill $signal, keys %SUBS;
while (%SUBS && poll(undef)) {}
}
sub _notify (@) {
die "no touching\n"
if caller ne __PACKAGE__;
my $method = shift;
foreach my $observer (@OBSERVERS) {
$observer->$method(@_) if $observer->can($method);
}
}
sub unobserve ($) {
my $observer = shift;
@OBSERVERS = grep { $_ ne $observer } @OBSERVERS;
}
sub observe ($) {
my $observer = shift;
unobserve($observer);
push @OBSERVERS, $observer;
}
1;
__END__
=head1 NAME
Arch::Run - run subprocesses and capture output
=head1 SYNOPSIS
use Gtk2 -init;
use Arch::Run qw(poll run_async LINES);
my $window = Gtk2::Window->new;
my $label = Gtk2::Label->new;
my $pbar = Gtk2::ProgressBar->new;
my $vbox = Gtk2::VBox->new;
$vbox->add($label); $vbox->add($pbar); $window->add($vbox);
$window->signal_connect(destroy => sub { Gtk2->main_quit; });
$window->set_default_size(200, 48); $window->show_all;
sub set_str { $label->set_text($_[0]); }
my $go = 1; # keep progress bar pulsing
Glib::Timeout->add(100, sub { $pbar->pulse; poll(0); $go; });
run_async(
command => [ 'du', '-hs', glob('/usr/share/*') ],
mode => LINES,
datacb => sub { chomp(my $str = $_[0]); set_str($str); },
exitcb => sub { $go = 0; set_str("exit code: $_[0]"); },
);
Gtk2->main;
=head1 DESCRIPTION
Arch::Run allows the user to run run subprocesses and capture their
output in a single threaded environment without blocking the whole
application.
You can use either B<poll> to wait for and handle process output, or
use B<handle_output> and B<handle_exits> to integrate
B<Arch::Run> with your applications main loop.
=head1 METHODS
The following functions are available:
B<run_with_pipe>,
B<run_async>,
B<get_output_handle>,
B<handle_output>,
B<poll>,
B<wait>,
B<killall>,
B<observe>,
B<unobserve>.
=over 4
=item B<run_with_pipe> I<$command>
=item B<run_with_pipe> I<$executable> I<$argument> ...
Fork and exec a program with STDIN and STDOUT connected to pipes. In
scalar context returns the output handle, STDIN will be connected to
/dev/null. In list context, returns the output and input handle.
The programs standard error handle (STDERR) is left unchanged.
=item B<run_async> I<%args>
Run a command asyncronously in the background. Returns the
subprocesses pid.
Valid keys for I<%args> are:
=over 4
=item B<command> => I<$command>
=item B<command> => [ I<$executable> I<$argument> ... ]
( run in 0.936 second using v1.01-cache-2.11-cpan-df04353d9ac )