sirohia has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Open3 doesn't work in a sub-routine
by daxim (Curate) on Jul 05, 2012 at 07:55 UTC | |
|
Re: Open3 doesn't work in a sub-routine
by ig (Vicar) on Jul 05, 2012 at 09:49 UTC | |
by Anonymous Monk on Jul 05, 2012 at 10:01 UTC | |
|
Re: Open3 doesn't work in a sub-routine
by Anonymous Monk on Jul 05, 2012 at 07:41 UTC | |
by sirohia (Initiate) on Jul 05, 2012 at 07:45 UTC | |
by Anonymous Monk on Jul 05, 2012 at 07:54 UTC |