Как я могу считать вывод ошибок внешних команд в Perl?

Как насчет простого разбиения множества следующим образом:

obs= c(rnorm(100, 0, 1), rnorm(100, sample(c(-1e6, 1e6), 1), 1))

histN <- function(x, n){
  clustRes <- kmeans(x,centers = n)
  par(mfrow=c(1,n))
  for(i in 1:n){
    hist(x[clustRes$cluster==i], xlab="", main = sprintf("subset = %d",i))
  }
}
dev.new()
histN(obs,2)

или как более симпатичная, но менее гибкая альтернатива:

hist2 <- function(x){
  clustRes <- kmeans(x,centers = 2)
  parDefault <- par()
  layout(matrix(c(1,1,1,2,3,3,3),nrow=1))
  idx1 <- which.min(clustRes$centers)
  idx2 <- which.max(clustRes$centers)
  h1 <- hist(x[clustRes$cluster==idx1],plot=FALSE)
  h2 <- hist(x[clustRes$cluster==idx2],plot=FALSE)
  yRange <- c(min(c(h1$counts,h2$counts)),max(c(h1$counts,h2$counts)))
  par(mai = c(parDefault$mai[1:3],0))
  par(cex = parDefault$cex)
  hist(x[clustRes$cluster==idx1],ylim= yRange, xlab="", ylab ="", main = sprintf("subset = %d",1),axes=FALSE)
  axis(1)
  axis(2)
  par(mai = c(parDefault$mai[1],0,parDefault$mai[3],0))
  par(cex = 3)
  plot(1:10,type="n", axes=FALSE, xlab="...", ylab ="")
  text(x= 5, y= 5, "...")
  axis(1, tick=FALSE,labels = FALSE)
  par(mai = c(parDefault$mai[1],0,parDefault$mai[3:4]))
  par(cex = parDefault$cex)
  hist(x[clustRes$cluster==idx2],ylim= yRange, xlab="", ylab ="", main = sprintf("subset = %d",2),axes=FALSE)
  axis(1)
  axis(4)
}
dev.new()
hist2(obs)
15
задан serenesat 16 February 2015 в 14:47
поделиться

7 ответов

Сами программы не могут выдавать «исключения», но могут возвращать ненулевые коды ошибок. Вы можете проверить код ошибки программы, запущенной с обратными метками или system () в Perl, используя $ ?:

$toPrint = "FAIL" if $?;

(Добавьте эту строку перед циклом, который проверяет @output .)

15
ответ дан 1 December 2019 в 00:16
поделиться

There's the answer in perlfaq8: How can I capture STDERR from an external command?


There are three basic ways of running external commands:

system $cmd;        # using system()
$output = `$cmd`;       # using backticks (``)
open (PIPE, "cmd |");   # using open()

With system(), both STDOUT and STDERR will go the same place as the script's STDOUT and STDERR, unless the system() command redirects them. Backticks and open() read only the STDOUT of your command.

You can also use the open3() function from IPC::Open3. Benjamin Goldberg provides some sample code:

To capture a program's STDOUT, but discard its STDERR:

use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
while( <PH> ) { }
waitpid($pid, 0);

To capture a program's STDERR, but discard its STDOUT:

use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);

To capture a program's STDERR, and let its STDOUT go to our own STDERR:

use IPC::Open3;
use Symbol qw(gensym);
my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);

To read both a command's STDOUT and its STDERR separately, you can redirect them to temp files, let the command run, then read the temp files:

use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHOUT = IO::File->new_tmpfile;
local *CATCHERR = IO::File->new_tmpfile;
my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
waitpid($pid, 0);
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
while( <CATCHOUT> ) {}
while( <CATCHERR> ) {}

But there's no real need for both to be tempfiles... the following should work just as well, without deadlocking:

use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHERR = IO::File->new_tmpfile;
my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
while( <CATCHOUT> ) {}
waitpid($pid, 0);
seek CATCHERR, 0, 0;
while( <CATCHERR> ) {}

And it'll be faster, too, since we can begin processing the program's stdout immediately, rather than waiting for the program to finish.

With any of these, you can change file descriptors before the call:

open(STDOUT, ">logfile");
system("ls");

or you can use Bourne shell file-descriptor redirection:

$output = `$cmd 2>some_file`;
open (PIPE, "cmd 2>some_file |");

You can also use file-descriptor redirection to make STDERR a duplicate of STDOUT:

$output = `$cmd 2>&1`;
open (PIPE, "cmd 2>&1 |");

Note that you cannot simply open STDERR to be a dup of STDOUT in your Perl program and avoid calling the shell to do the redirection. This doesn't work:

open(STDERR, ">&STDOUT");
$alloutput = `cmd args`;  # stderr still escapes

This fails because the open() makes STDERR go to where STDOUT was going at the time of the open(). The backticks then make STDOUT go to a string, but don't change STDERR (which still goes to the old STDOUT).

Note that you must use Bourne shell (sh(1)) redirection syntax in backticks, not csh(1)! Details on why Perl's system() and backtick and pipe opens all use the Bourne shell are in the versus/csh.whynot article in the "Far More Than You Ever Wanted To Know" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz . To capture a command's STDERR and STDOUT together:

$output = `cmd 2>&1`;                       # either with backticks
$pid = open(PH, "cmd 2>&1 |");              # or with an open pipe
while (<PH>) { }                            #    plus a read

To capture a command's STDOUT but discard its STDERR:

$output = `cmd 2>/dev/null`;                # either with backticks
$pid = open(PH, "cmd 2>/dev/null |");       # or with an open pipe
while (<PH>) { }                            #    plus a read

To capture a command's STDERR but discard its STDOUT:

$output = `cmd 2>&1 1>/dev/null`;           # either with backticks
$pid = open(PH, "cmd 2>&1 1>/dev/null |");  # or with an open pipe
while (<PH>) { }                            #    plus a read

To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out our old STDERR:

$output = `cmd 3>&1 1>&2 2>&3 3>&-`;        # either with backticks
$pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
while (<PH>) { }                            #    plus a read

To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:

system("program args 1>program.stdout 2>program.stderr");

Ordering is important in all these examples. That's because the shell processes file descriptor redirections in strictly left to right order.

system("prog args 1>tmpfile 2>&1");
system("prog args 2>&1 1>tmpfile");

The first command sends both standard out and standard error to the temporary file. The second command sends only the old standard output there, and the old standard error shows up on the old standard out.

28
ответ дан 1 December 2019 в 00:16
поделиться

Проверьте perlvar на $? . Если установлено значение 0, сигналов не было, и код возврата из программы также равен нулю. Это, вероятно, то, что вы хотите.

В этом случае вы можете даже просто использовать system и проверить, что его возвращаемое значение равно нулю, перенаправив stdout и stderr в /dev/null.

4
ответ дан 1 December 2019 в 00:16
поделиться

Assuming that diff errors wind up on STDERR, if you'd like to be able to examine or log the errors, I recommend the CPAN module Capture::Tiny:

use Capture::Tiny 'capture';
my ($out, $err) = capture { system($command) };

That's like backticks, but gives you STDOUT and STDERR separately.

4
ответ дан 1 December 2019 в 00:16
поделиться

Существует список интересных способов работы с выходными данными, заключенными в обратные символы. команда на сайте perldoc . Вам нужно будет прокрутить вниз или выполнить поиск «qx / STRING /» (без кавычек)

1
ответ дан 1 December 2019 в 00:16
поделиться

Вы также можете выполнить прогон с выводом 'diff -d', который облегчит чтение вашего кода.

foreach (`diff -d $args`){
  if (/^Only in/){
     do_whatever();
  }
}
0
ответ дан 1 December 2019 в 00:16
поделиться

Вы также можете:

my @output = `$command 2>\&1`;
0
ответ дан 1 December 2019 в 00:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: