IPC::Cmd(3perl) | Perl Programmers Reference Guide | IPC::Cmd(3perl) |
IPC::Cmd - finding and running system commands made easy
use IPC::Cmd qw[can_run run run_forked]; my $full_path = can_run('wget') or warn 'wget is not installed!'; ### commands can be arrayrefs or strings ### my $cmd = "$full_path -b theregister.co.uk"; my $cmd = [$full_path, '-b', 'theregister.co.uk']; ### in scalar context ### my $buffer; if( scalar run( command => $cmd, verbose => 0, buffer => \$buffer, timeout => 20 ) ) { print "fetched webpage successfully: $buffer\n"; } ### in list context ### my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = run( command => $cmd, verbose => 0 ); if( $success ) { print "this is what the command printed:\n"; print join "", @$full_buf; } ### run_forked example ### my $result = run_forked("$full_path -q -O - theregister.co.uk", {'timeout' => 20}); if ($result->{'exit_code'} eq 0 && !$result->{'timeout'}) { print "this is what wget returned:\n"; print $result->{'stdout'}; } ### check for features print "IPC::Open3 available: " . IPC::Cmd->can_use_ipc_open3; print "IPC::Run available: " . IPC::Cmd->can_use_ipc_run; print "Can capture buffer: " . IPC::Cmd->can_capture_buffer; ### don't have IPC::Cmd be verbose, ie don't print to stdout or ### stderr when running commands -- default is '0' $IPC::Cmd::VERBOSE = 0;
IPC::Cmd allows you to run commands platform independently, interactively if desired, but have them still work.
The "can_run" function can tell you if a certain binary is installed and if so where, whereas the "run" function can actually execute any of the commands you give it and give you a clear return value, as well as adhere to your verbosity settings.
Utility function that tells you if "IPC::Run" is available. If the "verbose" flag is passed, it will print diagnostic messages if IPC::Run can not be found or loaded.
Utility function that tells you if "IPC::Open3" is available. If the verbose flag is passed, it will print diagnostic messages if "IPC::Open3" can not be found or loaded.
Utility function that tells you if "IPC::Cmd" is capable of capturing buffers in it's current configuration.
Utility function that tells you if "IPC::Cmd" is capable of providing "run_forked" on the current platform.
"can_run" takes only one argument: the name of a binary you wish to locate. "can_run" works much like the unix binary "which" or the bash command "type", which scans through your path, looking for the requested binary.
Unlike "which" and "type", this function is platform independent and will also work on, for example, Win32.
If called in a scalar context it will return the full path to the binary you asked for if it was found, or "undef" if it was not.
If called in a list context and the global variable $INSTANCES is a true value, it will return a list of the full paths to instances of the binary where found in "PATH", or an empty list if it was not found.
"run" takes 4 arguments:
See "Caveats" for remarks on how commands are parsed and their limitations.
It will default to the global setting of $IPC::Cmd::VERBOSE, which by default is 0.
Of course, this requires that the underlying call supports buffers. See the note on buffers above.
Defaults to 0, meaning no timeout is set.
"run" will return a simple "true" or "false" when called in scalar context. In list context, you will be returned a list of the following items:
See the "HOW IT WORKS" section below to see how "IPC::Cmd" decides what modules or function calls to use when issuing a command.
"run_forked" is used to execute some program or a coderef, optionally feed it with some input, get its return code and output (both stdout and stderr into separate buffers). In addition, it allows to terminate the program if it takes too long to finish.
The important and distinguishing feature of run_forked is execution timeout which at first seems to be quite a simple task but if you think that the program which you're spawning might spawn some children itself (which in their turn could do the same and so on) it turns out to be not a simple issue.
"run_forked" is designed to survive and successfully terminate almost any long running task, even a fork bomb in case your system has the resources to survive during given timeout.
This is achieved by creating separate watchdog process which spawns the specified program in a separate process session and supervises it: optionally feeds it with input, stores its exit code, stdout and stderr, terminates it in case it runs longer than specified.
Invocation requires the command to be executed or a coderef and optionally a hashref of options:
my $r = run_forked("some external command", { 'wait_loop_callback' => sub { if (condition) { kill(1, $$); } }, 'terminate_on_signal' => 'HUP', });
Combined with "stdout_handler" and "stderr_handler" allows terminating external command based on its output. Could also be used as a timer without engaging with alarm (signals).
Remember that this code could be called every millisecond (depending on the output which external command generates), so try to make it as lightweight as possible.
"run_forked" will return a HASHREF with the following keys:
Returns the character used for quoting strings on this platform. This is usually a "'" (single quote) on most systems, but some systems use different quotes. For example, "Win32" uses """ (double quote).
You can use it as follows:
use IPC::Cmd qw[run QUOTE]; my $cmd = q[echo ] . QUOTE . q[foo bar] . QUOTE;
This makes sure that "foo bar" is treated as a string, rather than two separate arguments to the "echo" function.
"run" will try to execute your command using the following logic:
The behaviour of IPC::Cmd can be altered by changing the following global variables:
This controls whether IPC::Cmd will print any output from the commands to the screen or not. The default is 0.
This variable controls whether IPC::Cmd will try to use IPC::Run when available and suitable.
This variable controls whether IPC::Cmd will try to use IPC::Open3 when available and suitable. Defaults to true.
This variable controls whether run-time warnings should be issued, like the failure to load an "IPC::*" module you explicitly requested.
Defaults to true. Turn this off at your own risk.
This variable controls whether "can_run" will return all instances of the binary it finds in the "PATH" when called in a list context.
Defaults to false, set to true to enable the described behaviour.
This variable controls whether "run" will remove any empty/null arguments it finds in command arguments.
Defaults to false, so it will remove null arguments. Set to true to allow them.
If your command contains special characters (< > | &), it will be internally stringified before executing the command, to avoid that these special characters are escaped and passed as arguments instead of retaining their special meaning.
However, if the command contained arguments that contained whitespace, stringifying the command would lose the significance of the whitespace. Therefore, "IPC::Cmd" will quote any arguments containing whitespace in your command if the command is passed as an arrayref and contains special characters.
If you do not wish this to happen, you should provide an array reference, where all parts of your command are already separated out. Note however, if there are extra or spurious whitespaces in these parts, the parser or underlying code may not interpret it correctly, and cause an error.
Example: The following code
gzip -cdf foo.tar.gz | tar -xf -
should either be passed as
"gzip -cdf foo.tar.gz | tar -xf -"
or as
['gzip', '-cdf', 'foo.tar.gz', '|', 'tar', '-xf', '-']
But take care not to pass it as, for example
['gzip -cdf foo.tar.gz', '|', 'tar -xf -']
Since this will lead to issues as described above.
for ( 1..4 ) { $_ % 2 ? print STDOUT $_ : print STDERR $_; }
IPC::[Run|Open3] will first read all of STDOUT, then all of STDERR, meaning the output looks like '13' on STDOUT and '24' on STDERR, instead of
1 2 3 4
This has been recorded in rt.cpan.org as bug #37532: Unable to interleave STDOUT and STDERR.
IPC::Run, IPC::Open3
Thanks to James Mastros and Martijn van der Streek for their help in getting IPC::Open3 to behave nicely.
Thanks to Petya Kohts for the "run_forked" code.
Please report bugs or other issues to <bug-ipc-cmd@rt.cpan.org>.
Original author: Jos Boumans <kane@cpan.org>. Current maintainer: Chris Williams <bingos@cpan.org>.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
2023-11-25 | perl v5.32.1 |