in reply to Re^2: Need a solution to Expect.pm 's single line regex matching limitation
in thread Need a solution to Expect.pm 's single line regex matching limitation
Sample install script:
#!/usr/bin/perl my %questions = ( 'Path for package: ' => <<EOH, So we are poing to install The Best All-Purpose Compiler ever. It groks perl, java, c#, haskell, forth and lisp and many more all at the same time. Big deal. EOH 'do you want Visual Basic support? ' => <<EOH, The next abomination is optional. PLEASE think again if you are inclined to install support for that cruft. EOH 'What is your nick? ' => <<EOH, We want some sensitive information from you to harrass you on every IRC channel in which you talk bad of our product. Yes, we monitor them all. EOH ); for (keys %questions) { print $questions{$_}; sleep 1; print; chop ($foo = <STDIN>); sleep 1; print "you said: '$foo', good. We'll come back to that.\n"; } print "Thank you for using The Great Foo.\n";
expect script to deal with that install script:
#!/usr/bin/perl use Expect; my %answers = ( 'Path for package: ' => '/foo/bar/quux', 'do you want Visual Basic support? ' => 'NO!', 'What is your nick? ' => 'shmem', 'Thank you' => '', ); my $exp = Expect->new(); $exp->spawn( 'perl', 'install.pl') or die; my $prompts = join('|', map {qr{\Q$_\E} } keys %answers); $exp->expect(120, -re => $prompts, sub { my $exp = shift; my $matched = $exp->match; my $answer = delete $answers{$matched}; $exp->send( $answer."\n"); $exp->exp_continue if keys %answers; } ); print "All done.\n";
Output:
So we are poing to install The Best All-Purpose Compiler ever. It groks perl, java, c#, haskell, forth and lisp and many more all at the same time. Big deal. Path for package: /foo/bar/quux you said: '/foo/bar/quux', good. We'll come back to that. We want some sensitive information from you to harrass you on every IRC channel in which you talk bad of our product. Yes, we monitor them all. What is your nick? shmem you said: 'shmem', good. We'll come back to that. The next abomination is optional. PLEASE think again if you are inclined to install support for that cruft. do you want Visual Basic support? NO! you said: 'NO!', good. We'll come back to that. Thank you for using The Great Foo. done.
That's how it works, basically. Of course, you'll have to tweak your regexes well.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Need a solution to Expect.pm 's single line regex matching limitation
by deepblue (Initiate) on Nov 18, 2007 at 03:41 UTC |