Around 20% of delegates on our
Tcl Courses are using Tcl because of the Expect package that extends it to "choreograph" interaction with other systems and processes, and yet the Expect module in Perl (Expect.pm) is decidedly niche and we cover it on our
Perl for Larger Projects course only on request. But it *is* very useful ...
So - what does Expect on Perl do?
It allows you to
spawn a process so that you can replace your terminal interaction with an automated script. You then
send any necessary text to that process (as if typed from the keyboard), and you tell your Perl program what to
expect back ... and that can be a specific piece of text, a regular expression match, and end of file, or a timeout.
I've written a complete
[source code here] for the delegates on this week's course, and taken the example of "pinging" half a dozen different hosts on the Internet for which we are responsible or have an interest in; If I run the script, I get a report from each of the computers as six pings are run, results are grabbed, analysed and tabulated.
Notes:
a) Just like Tcl's Expect, multiple processes can be controlled at the same time; in the example just published, I've only used a single process at a time to help you learn.
b) In Tcl, you would use
expect_out to return to you the part of the buffer that's passed over before the match string is found. In Perl, you don't have that global variable, but you do have methods such as
before. It's quite hard to find examples of this ... so here's a sample in which I capture a round trip time from deep in a line and echo out just the round trip time itself:
$line = $exp->before();
if ($line =~ /time=([\d\.]+)\s*(\w+)/) {
print "Round trip - $1 $2\n";
}
(written 2010-10-22)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T242 - Tcl/Tk - More on Expect [435] Expect for Windows - (2005-09-04)
[1173] Cheat Sheet / Check list for Expect maintainers - (2007-05-02)
[1411] Buffering of inputs to expect, and match order - (2007-10-27)
[1475] Tcl/Tk - updating your display while tasks are running - (2007-12-16)
[1531] Expecting a item from a list of possibles - (2008-02-04)
[2475] Quick easy and dangerous - automated logins via Tcl / Expect - (2009-10-24)
[3448] Checking all the systems on a subnet, using Expect and Tk - (2011-09-18)
T211 - Tcl/Tk - What is Expect? Why use it? [286] Automating regular manual procedures - (2005-04-21)
[1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
[1409] What is Expect? - (2007-10-26)
[1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1602] Automating processes through Expect - (2008-04-05)
[2474] Using Tcl and Expect to automate repetitive jobs - (2009-10-24)
[2489] Parallel Pinging, using Python Threads or Expect spawn lists - (2009-11-02)
[3286] Should we cover expect and/or Tk on our public Tcl courses? - (2011-05-11)
[3572] Adding Expect on top of Tcl - what is it and where can I get a training course to learn about it? - (2012-01-08)
[4405] Backup procedures - via backup server - (2015-01-24)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
P307 - Perl/Tk [595] Add a friendly front end with Tk - (2006-02-08)
[596] The magic of -textvariable - (2006-02-08)
[599] Perl/Tk real time display - (2006-02-10)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1340] Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python) - (2007-09-09)
P219 - Perl - Libraries and Resources [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)
[112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
[357] Where do Perl modules load from - (2005-06-24)
[358] Use standard Perl modules - (2005-06-25)
[712] Why reinvent the wheel - (2006-05-06)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[760] Self help in Perl - (2006-06-14)
[1219] Judging the quality of contributed Perl code - (2007-06-06)
[1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1444] Using English can slow you right down! - (2007-11-25)
[1863] About dieing and exiting in Perl - (2008-11-01)
[1865] Debugging and Data::Dumper in Perl - (2008-11-02)
[2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
[2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[3101] The week before Christmas - (2010-12-23)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
Some other Articles
Audio equipmentException handling in Perl - using evalWhat are .pid files?Children, zombies, and reaping processesExpect in Perl - a short explanation and a practical example Dulwich College Preparatory, and Sevenoaks, SchoolsSetting up a matrix of data (2D array) for processing in your programSanta announcement, 5th December 2010, MelkshamLots of ways of doing it in Perl - printing out answersIncrement operators for counting - Perl, PHP, C and others