From: Austin S. <te...@of...> - 2004-07-13 06:57:36
|
On Mon, Jul 12, 2004 at 07:23:41PM -0700, Bala Ayres wrote: > Expect doesn't have a blocking mode or concept. Not quite true, case in point: $session->expect(); If you pass undef as the first argument to expect() it will block until it has matching patterns. In the example above it will block until EOF. This is useful e.g. where you wait for a process about to end but want to make sure it finishes spitting out its output first. > Except > gets chunks from the kernel buffer and it isn't always > keeping up with the interacting program. That is, > expect is usually behind and the application it is > interacting with is usually ahead. To sync up both, > (to block like you alluded to) , you need to loop on > the pattern you are looking for. You need to consume > all the patterns that are available on the stdout, > produced either by you "sending" them or produced by > the application. If you pass up any input, then your > expect program may appear to work, but not > consistently. > > > Take ls \n for example. You need to loop looking for > the command to return to unix prompt such $ or # or > > or what have you. > > While waiting - you should consume everything that > comes your way by using expect(3,'-re', "(.*?)\n") > (note .*? won't consume \n thus you have hard code \n. > You can do a nonblocking grab of existing output by doing expect(0, '.*'). The advantage being that if there is no data available it won't wait the 3 seconds as in the example above. > Every time you consume you should put that out to a > buffer such as $buffer .= $obj->match() ; > Sometimes you may have to use expect_before() but you > have to play with it to see what suits your response > best. > > I will be happy to post some samples if you need more > help. Good luck expecting. > There are also a few examples included with the tutorial. They are generally out of date, but maybe a good place to start as well. If you have code that would be of general use please copy it here, perhaps it would be useful to add to the module. Austin |