.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Arch::Run 3pm" .TH Arch::Run 3pm "2022-06-08" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Arch::Run \- run subprocesses and capture output .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& 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 => [ \*(Aqdu\*(Aq, \*(Aq\-hs\*(Aq, glob(\*(Aq/usr/share/*\*(Aq) ], \& mode => LINES, \& datacb => sub { chomp(my $str = $_[0]); set_str($str); }, \& exitcb => sub { $go = 0; set_str("exit code: $_[0]"); }, \& ); \& \& Gtk2\->main; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Arch::Run allows the user to run run subprocesses and capture their output in a single threaded environment without blocking the whole application. .PP You can use either \fBpoll\fR to wait for and handle process output, or use \fBhandle_output\fR and \fBhandle_exits\fR to integrate \&\fBArch::Run\fR with your applications main loop. .SH "METHODS" .IX Header "METHODS" The following functions are available: \&\fBrun_with_pipe\fR, \&\fBrun_async\fR, \&\fBget_output_handle\fR, \&\fBhandle_output\fR, \&\fBpoll\fR, \&\fBwait\fR, \&\fBkillall\fR, \&\fBobserve\fR, \&\fBunobserve\fR. .IP "\fBrun_with_pipe\fR \fI\f(CI$command\fI\fR" 4 .IX Item "run_with_pipe $command" .PD 0 .IP "\fBrun_with_pipe\fR \fI\f(CI$executable\fI\fR \fI\f(CI$argument\fI\fR ..." 4 .IX Item "run_with_pipe $executable $argument ..." .PD 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. .Sp The programs standard error handle (\s-1STDERR\s0) is left unchanged. .IP "\fBrun_async\fR \fI\f(CI%args\fI\fR" 4 .IX Item "run_async %args" Run a command asyncronously in the background. Returns the subprocesses pid. .Sp Valid keys for \fI\f(CI%args\fI\fR are: .RS 4 .IP "\fBcommand\fR => \fI\f(CI$command\fI\fR" 4 .IX Item "command => $command" .PD 0 .IP "\fBcommand\fR => [ \fI\f(CI$executable\fI\fR \fI\f(CI$argument\fI\fR ... ]" 4 .IX Item "command => [ $executable $argument ... ]" .PD Program and parameters. .IP "\fBmode\fR => \fI\f(CI$accum_mode\fI\fR" 4 .IX Item "mode => $accum_mode" Control how output data is accumulated and passed to \fBdata\fR and \&\fBfinish\fR callbacks. .Sp \&\fI\f(CI$accum_mode\fI\fR can be one of .RS 4 .IP "\fB\s-1RAW\s0\fR" 4 .IX Item "RAW" No accumulation. Pass output to \fBdata\fR callback as it is received. .IP "\fB\s-1LINES\s0\fR" 4 .IX Item "LINES" Accumulate output in lines. Pass every line separately to \fBdata\fR callback. .IP "\fB\s-1ALL\s0\fR" 4 .IX Item "ALL" Accumulate all data. Pass complete command output as one block to \&\fBdata\fR callback. .RE .RS 4 .RE .IP "\fBdatacb\fR => \fI\f(CI$data_callback\fI\fR" 4 .IX Item "datacb => $data_callback" Codeblock or subroutine to be called when new output is available. Receives one parameter, the accumulated command output. .IP "\fBexitcb\fR => \fI\f(CI$exit_callback\fI\fR" 4 .IX Item "exitcb => $exit_callback" 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.) .RE .RS 4 .RE .IP "\fBget_output_handle\fR \fI\f(CI$pid\fI\fR" 4 .IX Item "get_output_handle $pid" Returns the \s-1STDOUT\s0 handle of process \f(CW$pid\fR. You should never directly read from the returned handle. Use IO::Select or IO::Poll to wait for output and call \fBhandle_output\fR to process the output. .IP "\fBhandle_output\fR \fI\f(CI$pid\fI\fR" 4 .IX Item "handle_output $pid" Handle available output from process \fI\f(CI$pid\fI\fR. .Sp \&\fB\s-1ATTENTION:\s0\fR Call this method only if there really is output to be read. It will block otherwise. .IP "\fBpoll\fR \fI\f(CI$timeout\fI\fR" 4 .IX Item "poll $timeout" Check running subprocesses for available output and run callbacks as appropriate. Wait at most \fI\f(CI$timeout\fI\fR seconds when no output is available. .Sp Returns the number of processes that had output available. .IP "\fBwait\fR \fI\f(CI$pid\fI\fR" 4 .IX Item "wait $pid" Wait for subprocess \fI\f(CI$pid\fI\fR to terminate, repeatedly calling \fBpoll\fR. Returns the processes exit status or \f(CW\*(C`undef\*(C'\fR if \fBpoll\fR has already been called after the processes exit. .IP "\fBkillall\fR [\fI\f(CI$signal\fI\fR]" 4 .IX Item "killall [$signal]" Send signal \fI\f(CI$signal\fI\fR (\fB\s-1SIGINT\s0\fR if omitted) to all managed subprocesses, and wait until every subprocess to terminate. .IP "\fBobserve\fR \fI\f(CI$observer\fI\fR" 4 .IX Item "observe $observer" Register an observer object that wishes to be notified of running subprocesses. \fI\f(CI$observer\fI\fR should implement one or more of the following methods, depending on which event it wishes to receive. .RS 4 .IP "\fB\->cmd_start\fR \fI\f(CI$pid\fI\fR \fI\f(CI$executable\fI\fR \fI\f(CI$argument\fI\fR ..." 4 .IX Item "->cmd_start $pid $executable $argument ..." Called whenever a new subprocess has been started. Receives the subprocesses \s-1PID\s0 and the executed command line. .IP "\fB\->cmd_output_raw\fR \fI\f(CI$pid\fI\fR \fI\f(CI$data\fI\fR" 4 .IX Item "->cmd_output_raw $pid $data" Called whenever a subprocess has generated output. Receives the subprocesses \s-1PID\s0 and a block of output data. .Sp \&\fB\s-1NOTE:\s0\fR \fI\f(CI$data\fI\fR is not preprocesses (e.g. split into lines). \&\fBcmd_output_raw\fR receives data block as if \fB\s-1RAW\s0\fR mode was used. .IP "\fB\->cmd_exit\fR \fI\f(CI$pid\fI\fR \fI\f(CI$exitcode\fI\fR" 4 .IX Item "->cmd_exit $pid $exitcode" Called whenever a subprocess exits. Receives the subprocesses \s-1PID\s0 and exit code. .RE .RS 4 .RE .IP "\fBunobserve\fR \fI\f(CI$observer\fI\fR" 4 .IX Item "unobserve $observer" Remove \fI\f(CI$observer\fI\fR from observer list.