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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.