Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Expect.pm and STDIN question

by Archon810 (Initiate)
on Jan 07, 2009 at 21:09 UTC ( [id://734730]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://734730]
Approved by Lawliet
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-09-19 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.