Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Expect in Perl - a short explanation and a practical example

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
P219 - Perl - Libraries and Resources
  [3377] What do I mean when I add things in Perl? - (2011-08-02)
  [3101] The week before Christmas - (2010-12-23)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
  [2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
  [2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)
  [1863] About dieing and exiting in Perl - (2008-11-01)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
  [1219] Judging the quality of contributed Perl code - (2007-06-06)
  [760] Self help in Perl - (2006-06-14)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [712] Why reinvent the wheel - (2006-05-06)
  [358] Use standard Perl modules - (2005-06-25)
  [357] Where do Perl modules load from - (2005-06-24)
  [112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
  [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)

P307 - Perl/Tk
  [1340] Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python) - (2007-09-09)
  [1310] Callbacks - a more complex code sandwich - (2007-08-19)
  [738] (Perl) Callbacks - what are they? - (2006-05-30)
  [599] Perl/Tk real time display - (2006-02-10)
  [596] The magic of -textvariable - (2006-02-08)
  [595] Add a friendly front end with Tk - (2006-02-08)

T211 - Tcl/Tk - What is Expect? Why use it?
  [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)
  [3286] Should we cover expect and/or Tk on our public Tcl courses? - (2011-05-11)
  [2489] Parallel Pinging, using Python Threads or Expect spawn lists - (2009-11-02)
  [2474] Using Tcl and Expect to automate repetitive jobs - (2009-10-24)
  [1602] Automating processes through Expect - (2008-04-05)
  [1531] Expecting a item from a list of possibles - (2008-02-04)
  [1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
  [1411] Buffering of inputs to expect, and match order - (2007-10-27)
  [1409] What is Expect? - (2007-10-26)
  [1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
  [1173] Cheat Sheet / Check list for Expect maintainers - (2007-05-02)
  [435] Expect for Windows - (2005-09-04)
  [286] Automating regular manual procedures - (2005-04-21)

T242 - Tcl/Tk - More on Expect
  [3448] Checking all the systems on a subnet, using Expect and Tk - (2011-09-18)
  [2475] Quick easy and dangerous - automated logins via Tcl / Expect - (2009-10-24)
  [1475] Tcl/Tk - updating your display while tasks are running - (2007-12-16)


Back to
Dulwich College Preparatory, and Sevenoaks, Schools
Previous and next
or
Horse's mouth home
Forward to
Children, zombies, and reaping processes
Some other Articles
Audio equipment
Exception handling in Perl - using eval
What are .pid files?
Children, zombies, and reaping processes
Expect in Perl - a short explanation and a practical example
Dulwich College Preparatory, and Sevenoaks, Schools
Setting up a matrix of data (2D array) for processing in your program
Santa announcement, 5th December 2010, Melksham
Lots of ways of doing it in Perl - printing out answers
Increment operators for counting - Perl, PHP, C and others
4316 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3009_Exp ... mple-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat