JanFi86
JanFi86

Reputation: 519

Perl catching STDERR and STDOUT of subprocess

I have Perl script which is executing another script. How can I ensure that, when CONNECTION.pl is executed, I will capture STDERR and STDOUT, so that I can put it in the log in the parent script? Bellow is the code from the parent script:

my $systemcmd ='perl bin/CONNECTION.pl "'.$expanded_sql.'" "'.$tmp_file.'" "'.$fetch.'"';

eval {
  my $rc = system($systemcmd );
};

if ($@) {
  $errmsg = $@;
  croak {message=>$errmsg};
  unlink $tmp_file;
} 

Upvotes: 2

Views: 240

Answers (1)

choroba
choroba

Reputation: 241758

Try using Capture::Tiny to wrap the call to system.

Moreover, it might be cleaner to change the Perl script CONNECTION.pl into a module and use it from your script rather than shelling out to call perl again.

Upvotes: 8

Related Questions