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;

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 poll to wait for and handle process output, or use handle_output and handle_exits to integrate Arch::Run with your applications main loop.

METHODS

The following functions are available: run_with_pipe, run_async, get_output_handle, handle_output, poll, wait, killall, observe, unobserve. Fork and exec a program with \s-1STDIN\s0 and \s-1STDOUT\s0 connected to pipes. In scalar context returns the output handle, \s-1STDIN\s0 will be connected to /dev/null. In list context, returns the output and input handle. The programs standard error handle (\s-1STDERR\s0) is left unchanged. Run a command asyncronously in the background. Returns the subprocesses pid. Valid keys for %args are:

Program and parameters. Control how output data is accumulated and passed to data and finish callbacks. $accum_mode can be one of

\s-1RAW\s0

No accumulation. Pass output to data callback as it is received.

\s-1LINES\s0

Accumulate output in lines. Pass every line separately to data callback.

\s-1ALL\s0

Accumulate all data. Pass complete command output as one block to data callback.

Codeblock or subroutine to be called when new output is available. Receives one parameter, the accumulated command output. Codeblock or subroutine to be called when subprocess exits. Receives a single parameter, the commands exit code. (Or maybe not. We have to handle SIG{\s-1CHLD\s0} then. But maybe we have to do so anyway.)

Returns the \s-1STDOUT\s0 handle of process $pid. You should never directly read from the returned handle. Use IO::Select or IO::Poll to wait for output and call handle_output to process the output. Handle available output from process $pid. \s-1ATTENTION:\s0 Call this method only if there really is output to be read. It will block otherwise. Check running subprocesses for available output and run callbacks as appropriate. Wait at most $timeout seconds when no output is available. Returns the number of processes that had output available. Wait for subprocess $pid to terminate, repeatedly calling poll. Returns the processes exit status or \*(C`undef\*(C' if poll has already been called after the processes exit. Send signal $signal (\s-1SIGINT\s0 if omitted) to all managed subprocesses, and wait until every subprocess to terminate. Register an observer object that wishes to be notified of running subprocesses. $observer should implement one or more of the following methods, depending on which event it wishes to receive.

Called whenever a new subprocess has been started. Receives the subprocesses \s-1PID\s0 and the executed command line. Called whenever a subprocess has generated output. Receives the subprocesses \s-1PID\s0 and a block of output data. \s-1NOTE:\s0 $data is not preprocesses (e.g. split into lines). cmd_output_raw receives data block as if \s-1RAW\s0 mode was used. Called whenever a subprocess exits. Receives the subprocesses \s-1PID\s0 and exit code.

Remove $observer from observer list.