If you want both stdout and stderr (and don't mind them mixed together:

#! perl -slw use strict; use threads; sub thread { my $output = ''; open CMD, '-|', q[ perl -E"say( qq[$$:saying $_] ), warn( qq[$$:wa +rning $_\n] ), sleep(1) for 1 .. 4" 2>&1 ] or die $!; $output .= $_ while <CMD>; close CMD; return $output; } printf "Got\n'%s'\n", $_->join for map threads->create( \&thread ), 1 +.. 4; __END__ C:\test>t-junk Got '33168:warning 1 33168:warning 2 33168:warning 3 33168:warning 4 33168:saying 1 33168:saying 2 33168:saying 3 33168:saying 4 ' Got '30208:warning 1 30208:warning 2 30208:warning 3 30208:warning 4 30208:saying 1 30208:saying 2 30208:saying 3 30208:saying 4 ' Got '15844:warning 1 15844:warning 2 15844:warning 3 15844:warning 4 15844:saying 1 15844:saying 2 15844:saying 3 15844:saying 4 ' Got '15032:warning 1 15032:warning 2 15032:warning 3 15032:warning 4 15032:saying 1 15032:saying 2 15032:saying 3 15032:saying 4 '

If you only want stderr:

#! perl -slw use strict; use threads; sub thread { my $output = ''; open CMD, '-|', q[ perl -E"say( qq[$$:saying $_] ), warn( qq[$$:wa +rning $_\n] ), sleep(1) for 1 .. 4" 2>&1 >nul ] or die $!; $output .= $_ while <CMD>; close CMD; return $output; } printf "Got\n'%s'\n", $_->join for map threads->create( \&thread ), 1 +.. 4; __END__ C:\test>t-junk Got '34456:warning 1 34456:warning 2 34456:warning 3 34456:warning 4 ' Got '32700:warning 1 32700:warning 2 32700:warning 3 32700:warning 4 ' Got '19172:warning 1 19172:warning 2 19172:warning 3 19172:warning 4 ' Got '22176:warning 1 22176:warning 2 22176:warning 3 22176:warning 4 '

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.

In reply to Re^3: thread save calling an external command by BrowserUk
in thread thread save calling an external command by Paul.Unix

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.