Alien-wxWidgets
view release on metacpan or search on metacpan
inc/inc_IPC-Cmd/IPC/Cmd.pm view on Meta::CPAN
to locate. C<can_run> works much like the unix binary C<which> or the bash
command C<type>, which scans through your path, looking for the requested
binary .
Unlike C<which> and C<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 C<undef> if it was not.
=cut
sub can_run {
my $command = shift;
# a lot of VMS executables have a symbol defined
# check those first
if ( $^O eq 'VMS' ) {
require VMS::DCLsym;
my $syms = VMS::DCLsym->new;
return $command if scalar $syms->getsym( uc $command );
}
require Config;
require File::Spec;
require ExtUtils::MakeMaker;
if( File::Spec->file_name_is_absolute($command) ) {
return MM->maybe_command($command);
} else {
for my $dir (split /\Q$Config::Config{path_sep}\E/, $ENV{PATH}) {
my $abs = File::Spec->catfile($dir, $command);
return $abs if $abs = MM->maybe_command($abs);
}
}
}
=head2 $ok | ($ok, $err, $full_buf, $stdout_buff, $stderr_buff) = run( command => COMMAND, [verbose => BOOL, buffer => \$SCALAR] );
C<run> takes 3 arguments:
=over 4
=item command
This is the command to execute. It may be either a string or an array
reference.
This is a required argument.
See L<CAVEATS> for remarks on how commands are parsed and their
limitations.
=item verbose
This controls whether all output of a command should also be printed
to STDOUT/STDERR or should only be trapped in buffers (NOTE: buffers
require C<IPC::Run> to be installed or your system able to work with
C<IPC::Open3>).
It will default to the global setting of C<$IPC::Cmd::VERBOSE>,
which by default is 0.
=item buffer
This will hold all the output of a command. It needs to be a reference
to a scalar.
Note that this will hold both the STDOUT and STDERR messages, and you
have no way of telling which is which.
If you require this distinction, run the C<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.
=back
C<run> will return a simple C<true> or C<false> when called in scalar
context.
In list context, you will be returned a list of the following items:
=over 4
=item success
A simple boolean indicating if the command executed without errors or
not.
=item errorcode
If the first element of the return value (success) was 0, then some
error occurred. This second element is the error code the command
you requested exited with, if available.
=item full_buffer
This is an arrayreference containing all the output the command
generated.
Note that buffers are only available if you have C<IPC::Run> installed,
or if your system is able to work with C<IPC::Open3> -- See below).
This element will be C<undef> if this is not the case.
=item out_buffer
This is an arrayreference containing all the output sent to STDOUT the
command generated.
Note that buffers are only available if you have C<IPC::Run> installed,
or if your system is able to work with C<IPC::Open3> -- See below).
This element will be C<undef> if this is not the case.
=item error_buffer
This is an arrayreference containing all the output sent to STDERR the
command generated.
Note that buffers are only available if you have C<IPC::Run> installed,
or if your system is able to work with C<IPC::Open3> -- See below).
This element will be C<undef> if this is not the case.
=back
See the C<HOW IT WORKS> Section below to see how C<IPC::Cmd> decides
inc/inc_IPC-Cmd/IPC/Cmd.pm view on Meta::CPAN
### this should *also* work on multiple pipes in the command
### if there's no pipe in the command, append STDIN to the back
### of the command instead.
### XXX seems IPC::Run works it out for itself if you just
### dont pass STDIN at all.
# if( $special_chars and $special_chars =~ /\|/ ) {
# ### only add STDIN the first time..
# my $i;
# @command = map { ($_ eq '|' && not $i++)
# ? ( \*STDIN, $_ )
# : $_
# } @command;
# } else {
# push @command, \*STDIN;
# }
# \*STDIN is already included in the @command, see a few lines up
return IPC::Run::run( @command,
fileno(STDOUT).'>',
$_out_handler,
fileno(STDERR).'>',
$_err_handler
);
}
sub _system_run {
my $self = shift;
my $cmd = shift;
my $verbose = shift || 0;
my @fds_to_dup = $verbose ? () : qw[STDOUT STDERR];
__PACKAGE__->__dup_fds( @fds_to_dup );
### system returns 'true' on failure -- the exit code of the cmd
system( $cmd );
__PACKAGE__->__reopen_fds( @fds_to_dup );
return if $?;
return 1;
}
{ use File::Spec;
use Symbol;
my %Map = (
STDOUT => [qw|>&|, \*STDOUT, Symbol::gensym() ],
STDERR => [qw|>&|, \*STDERR, Symbol::gensym() ],
STDIN => [qw|<&|, \*STDIN, Symbol::gensym() ],
);
### dups FDs and stores them in a cache
sub __dup_fds {
my $self = shift;
my @fds = @_;
__PACKAGE__->_debug( "# Closing the following fds: @fds" ) if $DEBUG;
for my $name ( @fds ) {
my($redir, $fh, $glob) = @{$Map{$name}} or (
Carp::carp(loc("No such FD: '%1'", $name)), next );
### MUST use the 2-arg version of open for dup'ing for
### 5.6.x compatibilty. 5.8.x can use 3-arg open
### see perldoc5.6.2 -f open for details
open $glob, $redir . fileno($fh) or (
Carp::carp(loc("Could not dup '$name': %1", $!)),
return
);
### we should re-open this filehandle right now, not
### just dup it
if( $redir eq '>&' ) {
open( $fh, '>', File::Spec->devnull ) or (
Carp::carp(loc("Could not reopen '$name': %1", $!)),
return
);
}
}
return 1;
}
### reopens FDs from the cache
sub __reopen_fds {
my $self = shift;
my @fds = @_;
__PACKAGE__->_debug( "# Reopening the following fds: @fds" ) if $DEBUG;
for my $name ( @fds ) {
my($redir, $fh, $glob) = @{$Map{$name}} or (
Carp::carp(loc("No such FD: '%1'", $name)), next );
### MUST use the 2-arg version of open for dup'ing for
### 5.6.x compatibilty. 5.8.x can use 3-arg open
### see perldoc5.6.2 -f open for details
open( $fh, $redir . fileno($glob) ) or (
Carp::carp(loc("Could not restore '$name': %1", $!)),
return
);
### close this FD, we're not using it anymore
close $glob;
}
return 1;
}
}
sub _debug {
my $self = shift;
my $msg = shift or return;
my $level = shift || 0;
local $Carp::CarpLevel += $level;
Carp::carp($msg);
return 1;
}
1;
__END__
=head1 HOW IT WORKS
C<run> will try to execute your command using the following logic:
=over 4
=item *
If you have C<IPC::Run> installed, and the variable C<$IPC::Cmd::USE_IPC_RUN>
is set to true (See the C<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.
=item *
Otherwise, if the variable C<$IPC::Cmd::USE_IPC_OPEN3> is set to true
(See the C<GLOBAL VARIABLES> Section), try to execute the command using
C<IPC::Open3>. Buffers will be available on all platforms except C<Win32>,
interactive commands will still execute cleanly, and also your verbosity
settings will be adhered to nicely;
=item *
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.
=item *
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.
=back
=head1 Global Variables
The behaviour of IPC::Cmd can be altered by changing the following
global variables:
=head2 $IPC::Cmd::VERBOSE
This controls whether IPC::Cmd will print any output from the
commands to the screen or not. The default is 0;
=head2 $IPC::Cmd::USE_IPC_RUN
This variable controls whether IPC::Cmd will try to use L<IPC::Run>
when available and suitable. Defaults to true if you are on C<Win32>.
=head2 $IPC::Cmd::USE_IPC_OPEN3
This variable controls whether IPC::Cmd will try to use L<IPC::Open3>
when available and suitable. Defaults to true.
=head2 $IPC::Cmd::WARN
This variable controls whether run time warnings should be issued, like
the failure to load an C<IPC::*> module you explicitly requested.
Defaults to true. Turn this off at your own risk.
=head1 Caveats
=over 4
=item Whitespace
When you provide a string as this argument, the string will be
split on whitespace to determine the individual elements of your
command. Although this will usually just Do What You Mean, it may
break if you have files or commands with whitespace in them.
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.
=item IO Redirect
( run in 0.362 second using v1.01-cache-2.11-cpan-e93a5daba3e )