Could you take a look and let me know if you see any glaring issues.

This is iffy: or die "$+, $^E";.

You probably mean $! not $+, and $^E probably won't give you anything unless you are running under windows (or maybe OS/2?).

And you are testing if the thread is joinable, but never joining (or detaching) it. Which means it will hang around until the program ends. At which point you would probably get a warning message.

Also I assumed the way I have it coded would enable me to get whatever was "printed" prior to killing the thread but this does not seem to be the case, any ideas on what I am doing wrong?

I'm not really sure I understand what you mean by that? Exactly where in your code do you want to access it?

You should certainly be able to access the contents of the shared array @result from outside the thread whilst the command is still running. But probably not the way you have it coded currently:

@results = <$fh>;

Perl probably locks the entire array until that statement completes, which won't be until the command completes. If you changed that to push @results, $_ while <$fh>; you might have more success, but remember that you would have to employ locking to prevent conflicts.

Perhaps a better way would be to use a Thread::Queue which is basically just a shared array and a couple of simple methods (or was until they f***ed with it recently!), but it does take care of the locking for you which is a great convenience, as you know it is done right.

However, since timedCommand() can't return anything until you've cleaned up the process and thread, it is hard to see what benefit you would get from that?


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: How to deal with a forked proces that is waiting for user input? by BrowserUk
in thread How to deal with a forked proces that is waiting for user input? by gepapa

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.