IPC::Cmd - finding and running system commands made easy |
IPC::Cmd - finding and running system commands made easy
use IPC::Cmd qw[can_run run];
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 ) ) { print "fetched webpage successfully: $buffer\n"; }
### in list context ### my( $success, $error_code, $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; }
### 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, interactively if desired, platform independent 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.
can_run
takes but a single 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.
It will return the full path to the binary you asked for if it was
found, or undef
if it was not.
run
takes 3 arguments:
See CAVEATS for remarks on how commands are parsed and their limitations.
IPC::Run
to be installed or your system able to work with
IPC::Open3
).
It will default to the global setting of $IPC::Cmd::VERBOSE
,
which by default is 0.
run
command in list context
and inspect the individual buffers.
Of course, this requires that the underlying call supports buffers. See the note on buffers right above.
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:
IPC::Run
installed,
or if your system is able to work with IPC::Open3
-- See below).
This element will be undef
if this is not the case.
IPC::Run
installed,
or if your system is able to work with IPC::Open3
-- See below).
This element will be undef
if this is not the case.
IPC::Run
installed,
or if your system is able to work with IPC::Open3
-- See below).
This element will be undef
if this is not the case.
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
will try to execute your command using the following logic:
IPC::Run
installed, and the variable $IPC::Cmd::USE_IPC_RUN
is set to true (See the GLOBAL VARIABLES
Section) use that to execute
the command. You will have the full output available in buffers, interactive commands are sure to work and you are guaranteed to have your verbosity
settings honored cleanly.
Otherwise, if the variable $IPC::Cmd::USE_IPC_OPEN3
is set to true
(See the GLOBAL VARIABLES
Section), try to execute the command using
IPC::Open3
. Buffers will be available on all platforms except Win32
,
interactive commands will still execute cleanly, and also your verbosity
settings will be adhered to nicely;
Otherwise, if you have the verbose argument set to true, we fall back
to a simple system()
call. We cannot capture any buffers, but
interactive commands will still work.
Otherwise we will try and temporarily redirect STDERR and STDOUT, do a
system()
call with your command and then re-open STDERR and STDOUT.
This is the method of last resort and will still allow you to execute
your commands cleanly. However, no buffers will be available.
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 the IPC::Run manpage
when available and suitable. Defaults to true if you are on Win32
.
This variable controls whether IPC::Cmd will try to use the IPC::Open3 manpage 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.
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's extra or spurious whitespace 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.
IPC::Run
, IPC::Open3
Thanks to James Mastros and Martijn van der Streek for their help in getting IPC::Open3 to behave nicely.
Please report bugs or other issues to <bug-ipc-cmd@rt.cpan.org>.
This module by Jos Boumans <kane@cpan.org>.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
IPC::Cmd - finding and running system commands made easy |