MojaveJoe has asked for the wisdom of the Perl Monks concerning the following question:

So I am having a problem getting my perl expect script to work. It works fine on most commands but there is some weirdness on others. I essentially execute this command and it appears that there are several responses before it hits the match. Each time it waits, it clears the buffer before getting the next response. Here is the debug output (edited for brevity - notice the snips):

Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x22cd510)', 60, '-re', ']MyPro +mpt>|]MyPrompt#|]MyPrompt...') called at ./get_ippools_exp.pl line 19 +2 main::cmd('Expect=GLOB(0x22cd510)', 'show config context da +ta | grep "ip pool"') called at ./get_ippools_exp.pl line 75 main::getPools() called at ./get_ippools_exp.pl line 25 spawn id(4): beginning expect. Timeout: 60 seconds. Current time: Fri Aug 26 13:50:58 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 92 byte(s). show config context data | grep "ip pool" Friday August 26 18:50:58 UTC 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 1815 byte(s). ip pool data1 ... ip pool data2 ... <snip> ip pool data16 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 2048 byte(s). ip pool data17 ... ip pool data18 ... <snip> ip pool data38 ... ip Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 351 byte(s). pool data39 ... ip pool data40 ... ip pool data41 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 33 byte(s). <----here it receives the prompt!!!! [local]MyPrompt> Returning from expect successfully. spawn id(4): accumulator: ` '<----You can see that the buffer is dum +ped

The code to call the command is simple:

my ($exp, $cmd) = @_; $exp->print($cmd, "\n"); $exp->expect($timeout, -re => $prompt) or return; my $output = $exp->before; $output =~ s/^$cmd\n//; # remove the sent cmd return $output;
It seems like each time the spawn id(4) process goes into "Waiting for new data (.. seconds)..." mode it dumps the prior values. Can anyone help me with this? Thank you!