Hello, I made a simple subroutine to extract: 1/ return code, 2/ output, and 3/ error after executing a command. This works as a simple perl script, but my subroutine fails. Help please. Oh, I should explain what doesn't work. A) The command executes, but the exit code is always 0. B) Error is always empty. C) Output gets the error as well.
My sub-routine as below:package ABCUtilities; #!/usr/bin/perl use warnings; use strict; use IPC::Open3; sub execute { my $cmd = shift; my $pid = open3(\*WRITER, \*READER, \*ERROR, $cmd); my ($err_msg, $out_msg) = ('', ''); while( my $output = <READER> ) { $out_msg .= $output; # . " <br>"; } while( my $errout = <ERROR> ) { $err_msg .= $errout; # . "<br>"; } waitpid( $pid, 0 ) or die "$!\n"; my $exit_code = $? >> 8; return ($exit_code, $err_msg, $out_msg); } 1;
In reply to Open3 doesn't work in a sub-routine by sirohia
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |