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

Hello fellow monks. I am hoping you can help me with an Expect question. I'll try to explain it as clearly and briefly as possible.

I'm trying to automate some cpan module installation, but it seems like cpan isn't allowing to auto-yes all inputs and I can't even disable the Is it OK to try to connect to the Internet [yes] prompt.

So, I wrote a little Expect script that would send "\n" to everything that matches [.*?]. Here it is, with a few random modules.

use strict; use Expect; my $exp = Expect->spawn("cpan -i AMF::Perl Crypt::GeneratePassword Dat +e::Manip") or die "Cannot spawn ssh: $!\n"; $exp->expect( undef, [qr/\[.*?\]/ => sub { my $fh = shift; print "\n[[AUTO-ENTER]]\n"; $fh->send( "\n"); exp_continue; } ], );
The only problem (and it's not really an issue but I'd like the script to be clean), is it triggers on things like this, that don't even have an STDIN prompt:
SWF::File [requires] Readonly [requires] List::MoreUtils [requires] [[AUTO-ENTER]] [[AUTO-ENTER]] [[AUTO-ENTER]]
I do not want to make a whitelist or a blacklist of things that should or should not trigger sending the "\n". Ideally, it would only send anything if the line is followed by an STDIN prompt.

So the question to you all, mighty monks, can one detect an STDIN prompt in Expect and only perform an action if it's there, for example on the same line as the text being regexed?

Please let me know if I'm not being clear enough.
Thanks!

Artem
http://beerpla.net

Replies are listed 'Best First'.
Re: Expect.pm and STDIN question
by salva (Canon) on Jan 07, 2009 at 21:39 UTC
    The question mark is a regular expression metacharacter. You have to escape it:
    qr/\[.*\?\]/
      @salva I know it is, that's why it's there. It makes the match non-greedy, which doesn't really matter in this case.