in reply to Re: Automating sudo actions
in thread Automating sudo actions

That is totally awesome zentara, thanks so much!

I never would have guessed that IO::Select was necessary--can you explain why it is? I've had lots of weirdness using IPC::Open3 in the past and I suspect there's something unixy I don't understand properly.

I always thought that IPC::Open3 was designed specifically so that you could just talk and listen to a process you spawned, but it doesn't seem to work that way (for long) in practice.

Replies are listed 'Best First'.
Re^3: Automating sudo actions
by tilly (Archbishop) on Jan 26, 2011 at 06:52 UTC
    The reason you need select or else IO::Select is very simple. When you are both talking to and listening from a process it is very important that you are not talking when it is trying to talk, or listening when it is trying to listen. If that happens you'll get a deadlock which lasts forever.

    What select (either version) does is tell you whether it is ready to talk or receive more input, so that you can correctly send or receive data. That way you know which you can safely do and avoid deadlocks.

    Please note that doing this correctly is complicated. If at all possible you want to arrange to need to talk to an external process, or listen from it, but not both.

Re^3: Automating sudo actions
by zentara (Cardinal) on Jan 26, 2011 at 14:30 UTC
    In addition to what tilly said about select, in this case there is an additional, and often useful technique to actually SEPARATE the stdout and stderr stream. In IPC::Open3, if you set the \*ERR handle to 0, it will go to stdout, like in IPC::Open2. But what if you wanted your stderr to go somewhere different, or be highlighted in different colors, or go to a log file?. Then the general purpose techique shown above can be used. It is almost the way you would do it in c. Most people forget about select, because many modules handle those details transparently for them.

    But it is a cute code fragment, isn't it? You know, us old geeks, drool over elegant time-tested code fragments. :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh