Arch::Run(3pm)
π Arch::Run π
okay bestie, so this is Arch::Run, a lil gem from the User Contributed Perl Documentation collection. the whole vibe? run subprocesses and capture output β¨ no cap.
β¨ 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;
like, look at that. a whole GUI progress bar feeding off subprocess output. iconic π
πΈ DESCRIPTION πΈ
Arch::Run lets you run subprocesses and scoop up their output in a single threaded environment β which means you do not have to block your entire application while you wait. your app keeps vibing, the subprocess does its thing, everybody wins π«Ά
you've got two whole strategies, bestie:
- use
pollto wait for and handle process output yourself, or - use
handle_outputandhandle_exitsto integrate Arch::Run with your application's main loop
pick your fighter β¨
π METHODS π
the following functions are available: run_with_pipe, run_async, get_output_handle, handle_output, poll, wait, killall, observe, unobserve. that's the whole squad, no gatekeeping π
β run_with_pipe β
run_with_pipe $command
run_with_pipe $executable $argument ...
fork and exec a program with STDIN and STDOUT connected to pipes. so here's the tea:
- in scalar context it returns the output handle, and STDIN gets connected to
/dev/null(yeah, ghosted π«₯) - in list context, it returns both the output handle and the input handle
and the program's standard error handle (STDERR)? that one's left completely unchanged, untouched, minding its own business β¨
β run_async %args β
run a command asyncronously in the background. returns the subprocess's pid. the way it just... hands you a pid and dips π
valid keys for %args are:
command => $commandcommand => [ $executable $argument ... ]
program and parameters. simple as that π
mode => $accum_mode
controls how output data is accumulated and passed to the data and finish callbacks.
$accum_mode can be one of:
RAWβ no accumulation. pass output to the data callback as it is received, immediately, no filter, raw as it gets π₯LINESβ accumulate output in lines. pass every line separately to the data callback. one line at a time, baby β¨ALLβ accumulate all the data. pass the complete command output as one big block to the data callback. all at once, no crumbs π
datacb => $data_callback
a codeblock or subroutine that gets called when new output is available. it receives one parameter: the accumulated command output.
exitcb => $exit_callback
a codeblock or subroutine that gets called when the subprocess exits. it receives a single parameter: the command's exit code. (Or maybe not. We have to handle SIG{CHLD} then. But maybe we have to do so anyway.)
ngl bestie, even the docs are like "maybe??" about that one π we're keeping it exactly as it is though, periodt.
β get_output_handle $pid β
returns the STDOUT handle of process $pid.
β οΈ you should NEVER directly read from the returned handle. like, don't. useIO::SelectorIO::Pollto wait for output, and then callhandle_outputto process it. that's the whole flow, iykyk π
β handle_output $pid β
handle available output from process $pid.
π¨ ATTENTION: call this method only if there really is output to be read. it will block otherwise. it's giving clingy. don't do that to yourself π
β poll $timeout β
check running subprocesses for available output and run callbacks as appropriate. it waits at most $timeout seconds when no output is available. patient but not too patient πΈ
returns the number of processes that had output available.
β wait $pid β
wait for subprocess $pid to terminate, repeatedly calling poll in the meantime.
returns the process's exit status, or undef if poll has already been called after the process's exit. (ghosted again, sorry π«₯)
β killall [$signal] β
send signal $signal (SIGINT if omitted) to all managed subprocesses, and wait until every single subprocess terminates.
no survivors, no leftovers, we wait for the whole group to actually be gone π
β observe $observer β
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 events it wants to receive:
->cmd_start $pid $executable $argument ...β called whenever a new subprocess has been started. receives the subprocess's PID and the executed command line. main character energy, fr fr π->cmd_output_raw $pid $dataβ called whenever a subprocess has generated output. receives the subprocess's PID and a block of output data.
NOTE:$datais not preprocessed (e.g. split into lines).cmd_output_rawreceives the data block as ifRAWmode was used. raw data, no editing, no filter π
->cmd_exit $pid $exitcodeβ called whenever a subprocess exits. receives the subprocess's PID and exit code. goodbye bestie π¦
β unobserve $observer β
remove $observer from the observer list. it's not you, it's the observer list π
π a lil sign-off π
and that's the whole page, bestie! this doc is from perl v5.20.2, dated 2005-10-08 β vintage but still slaying β¨ go forth and run some subprocesses, you absolute icon ππ
π¦ where this page comes from
- ubuntu jammy: libarch-perl 0.5.2-2 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms