use Expect; $Expect::Debug = 3; # verbose debug output $Expect::Log_Stdout = 1; # show chatter for debugging $Expect::Exp_Internal =1; my $exp = Expect->spawn("units") or die "Cannot spawn unit: $!\n"; $exp->log_stdout(1); $exp->expect(15, [ qr/you have/i => sub { my $exp = shift; print "you have"; $exp->send("2 km"); exp_continue; } ], [ qr/you want/i , sub { my $exp = shift; print "you want"; $exp->send("m"); exp_continue; }], [ 'eof', sub { my $exp = shift; print "eof"; print $exp->before(); } ], [ qr/.*/, sub { my $exp = shift; print $exp->error; print $exp->before(); } ] ); #### user@dell:~> units 2526 units, 72 prefixes, 56 nonlinear units You have: #### spawn id(20): Does `' match: pattern #1: -re `(?^i:you have)'? No. pattern #2: -re `(?^i:you want)'? No. pattern #3: -eof `'? No. pattern #4: -re `(?^:.*)'? YES!! Before match string: `' Match string: `' After match string: `' Matchlist: () Calling hook CODE(0x7fd8217449c0)... Returning from expect successfully. spawn id(20): accumulator: `' Closing spawn id(20).