call_to_expect($one_command); execute_other_code(); call_to_expect($another_command); ------ test-expect-unrolled ----- use Expect; use Modern::Perl; my $exp = Expect->spawn("midish","-v") or die "Couldn't start program: $!\n"; # don't copy program output to STDOUT $exp->log_stdout(0); $exp->expect(1, '+ready', \&do_cmd); print_output(); $exp->expect(1, '+ready', \&do_cmd); print_output(); $exp->expect(1, '+ready', \&do_cmd); print_output(); sub do_cmd { print "enter command >> "; my $cmd = ; $exp->send($cmd); } sub print_output { map{say "midish: $_"} split "\n", $exp->before; } $exp->soft_close(); __END__ program output: $ perl test-expect-unrolled enter command >> asdf enter command >> 1234 midish: midish: asdf midish: asdf: no such proc enter command >> ffff midish: midish: 1234 midish: 2.5: statement or proc definition expected ^C $