How do you capture stderr, stdout, and the exit code all at once, in Perl?

后端 未结 6 2210
情深已故
情深已故 2020-11-27 03:19

Is it possible to run an external process from Perl, capture its stderr, stdout AND the process exit code?

I seem to be able to do combinations of these, e.g. use ba

6条回答
  •  天命终不由人
    2020-11-27 03:54

    I found IPC:run3 to be very helpful. You can forward all child pipes to a glob or a variable; very easily! And exit code will be stored in $?.

    Below is how i grabbed stderr which i knew would be a number. The cmd output informatic transformations to stdout (which i piped to a file in the args using >) and reported how many transformations to STDERR.

    use IPC::Run3
    
    my $number;
    my $run = run3("cmd arg1 arg2 >output_file",\undef, \undef, \$number);
    die "Command failed: $!" unless ($run && $? == 0);
    

提交回复
热议问题