[sf-perl] capture stdout and stderr, possibly supply input

yary not.com at gmail.com
Fri Jun 24 14:52:07 PDT 2011


On Thu, Jun 23, 2011 at 5:40 PM, Philip J. Hollenback <philiph at pobox.com> wrote:
> I am writing a perl script that needs to invoke an external program and
> collect the stdout and stderr of that program.  I also need to check the
> return code.
...
> My complication is that this external program may stop and ask for a
> password.  If that happens, I need to get a password from the user and
> feed it to the external program to continue.  Then I can capture the
> output I need after that (the password prompt will always happen before
> all other output).  Thus I can't really use IO::CaptureOutput because it
> will halt and wait for the password input without displaying anything on
> stdout (since I'm already capturing stdout).

Looking at the docs from IO::CaptureOutput, it will only return after
the code is done, so you can't use it exactly in that case, as you
suspected.

You have two options:

1. Always pipe the password to the external program's stdin. If it
needs it, it will get it, if it doesn't need it, no harm done. That
assumes that the external program reads nothing else from its stdin.
(Some programs read password from the terminal and not stdin, which is
could complicate this approach).

2. Use IPC::Open2 or IPC::Open3. Check for a password at the start of
reading, handle it, continue.


More information about the SanFrancisco-pm mailing list