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

How do I have perl fill in values for me, ie for passwd. I tried things like system("passwd \nchangeme\nnewpass\nnewpass") and it waits until passwd is complete before using anything after the \n's ... (btw, passwd is just being used as an example because it asks for three values)

Replies are listed 'Best First'.
Re: Value Fill
by rob_au (Abbot) on Dec 22, 2002 at 12:00 UTC
    The functionality which you seek is provided by the Expect module. For example:

    use Expect; my $process = Expect->spawn( '/usr/bin/passwd'); $process->expect( 10, "Password:" ); $process->send( "changeme\n" ); $process->send( "newpass\n" ); $process->send( "newpass\n" ); $process->expect( undef );

    Alternatively, if you find the interface to this module overly complex, you may want to investigate the Expect::Simple module.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000000100"))'

Re: Value Fill
by moof1138 (Curate) on Dec 23, 2002 at 01:56 UTC
    The Expect module mentioned above is the usually best approach for working with interactive processes. It is well designed for dealing with interactive processes, handling deadlocks and situations where you have some input that is expected, an unexpected error, or nothing at all returns.

    Another way to work with interactive processes is IPC::Open2. Using IPC::Open2 will give you a read and a write handle to a process. It should be included as a standard module, so it does not require installing a new module. But using it can degenerate quickly if you get unexpected errors or no output from a program, so you need to program carefully to avoid deadlocks. I would recommend you use Expect if at all possible.
Re: Value Fill
by doc (Scribe) on Dec 22, 2002 at 12:33 UTC

    You could use backtics `mysql -u root -ppassword somedb` or system using the single or multi arg syntax or Expect.pm What is best (and/or will work) depends on what you are trying to do with what external executable and if security is a major or minor issue.

    For many things you might do via system there is a Perl equivalent or interface just to add to the options.

    doc