7.4. Simple computation - adding two values

Example 7-1. examples/bc/bc1.pl

#!/usr/bin/perl
use strict;
use warnings;

use Expect;

my $e = Expect->new;
$e->raw_pty(1);
$e->spawn("bc") or die "Cannot run bc\n";
$e->expect(1, "warranty") or die "no warranty\n";
$e->send("23+7\n");
$e->expect(1, 30) or die "no sum\n";
print "Success\n";

  • raw_pty turns off echo

  • spawn starts the external program

  • expect(timeout, regex) return undef if failed

  • timeout is in seconds, 0 means check once, undef means wait forever

  • send - as if the user was typing at the keyboard


If you are interested in on-site trainings by the author, please contact me directly.

Online courses: