keydet89 has asked for the wisdom of the Perl Monks concerning the following question:

All,

I'm trying to execute some external commands within Perl on Windows 2K & XP systems. I need to capture the output in order to send the output of the command to a socket. Right now, I'm using the backticks, wrapped in exec{}, such as:

exec {

@output = 'cmd';

};

Every now and then, I'll come across a command that hangs, but when using backticks, I don't have a way of killing the process after a timeout.

I've looked at Win32::Process and Win32::Job, but neither one has a way of capturing the output in a list, or even a scalar...Win32::Job uses files.

Is there a way to accomplish this?

Thanks,

Harlan
keydet89 at yahoo dot com

  • Comment on Win32 Processes...capturing output and timing out

Replies are listed 'Best First'.
Re: Win32 Processes...capturing output and timing out
by NetWallah (Canon) on Apr 20, 2005 at 19:36 UTC
    You may want to
    use threads;
    use threads::shared;

    Start your external process within a thread, (no need for "exec", then pass data to the parent using shared variables or
    use Thread::Queue;

    The parent process can manage the threads, timing them out, and killing as required.

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein

      NetWallah,

      Thanks for the response...I'm looking into it now.

      One thing I'm grappling with is this...I can ignore a thread, or wait for it to exit...but how do I say, wait for x seconds, and if the thread hasn't completed, kill it?

      Thanks again for your help.

        You might find this subthread useful, but do read the whole subthread.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?
        No clean way that I know of, to kill a thread.

        Here is a link to some PM discussion of a usable, but ugly option.

             "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein