A little-known (and perhaps undocumented) feature of Perl is that a trailing "2>&1" on a command will be handled by Perl itself (not the shell) if there is no other reason to use a shell (if there are no other shell metacharacters in the given command string). (At least that was true quite a while ago when I was reading some of the Perl source code and I don't expect such a feature to be removed.)

So this problem would go away if the (useless) single quotes were removed from the example $cmd string. Yes, that "fix" likely won't work for the real problem.

I find it unfortunate that there is no way to trigger this feature when passing a list to system.

Update: Note that there are several modules to choose from that do "IPC" or "run commands" that can handle the "2>&1" for you. Or you can just handle it yourself using open (the 'fork' and 'dup' forms) and exec. Maybe I'll post an example of doing that in a reply, since I'd have to reread a few things to be able to write such.

- tye        


In reply to Re^2: Unable to grab STDERR from a backtick command (2>&1) by tye
in thread Unable to grab STDERR from a backtick command by reasonablekeith

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.