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

The first code set is plain Expect and the second is Perl's version. The foreach loop in Perl's version will only execute the first command. Can you please assist me with converting the plain Expect code to Perl's version?
foreach table $tables { set timeout 3600 expect { ">" { sleep 1; send "TABLE $table; FORMAT 132 PACK\r" } timeout { errmsg "TABLE COMMAND PROMPT NOT FOUND FOR $table" } } expect { "COMMAND DISALLOWED DURING DUMP" { errmsg "TABLE: $table - DUM +P IN PROGR ESS" } "Directory is full" { errmsg "TABLE: $table - DIRECTORY IS FUL +L" } "UNKNOWN TABLE" { sleep 1; send "abort\r"; continue } "TABLE: $table" { send "" } timeout { errmsg "TABLE NAME NOT FOUND FOR $table" } } expect { "The first column" { sleep 1; send "LIS ALL\r" } timeout { errmsg "FIRST COLUMN NOT FOUND FOR $table" } } expect { "EMPTY TABLE" { sleep 1; continue } "TOP" { send "" } timeout { errmsg "TOP NOT FOUND FOR $table" } } expect { "BOTTOM" { sleep 1; send "quit all\r" } timeout { errmsg "BOTTOM NOT FOUND FOR $table" } } }
@tables = ("TONES", "OFCENG", "EASAC"); foreach $table (@tables) { $exp->expect(180, [ qr/>/i, sub { my $self = shift; sleep(1); $self->send("TABLE $table; FORMAT 132 PACK\r"); }], [ qr/UNKNOWN TABLE/i, sub { my $self = shift; sleep(1); $self->send("")ort\r"); next; }], [ qr/TABLE: $table/i, sub { my $self = shift; $self->send(""); }], [ qr/The first column/i, sub { my $self = shift; sleep(1); $self->send("LIS ALL\r"); }], [ qr/EMPTY TABLE/i, sub { my $self = shift; sleep(1); next; }], [ qr/TOP/i, sub { my $self = shift; $self->send(""); }], [ qr/BOTTOM/i, sub { my $self = shift; sleep(1); $self->send("quit all\r"); }], ); }

Replies are listed 'Best First'.
Re: Perl's Expect - foreach loop
by Roy Johnson (Monsignor) on Apr 28, 2005 at 17:58 UTC
    What happens after it does the first command? Hang until timeout? Repeatedly executes first command?

    Is there always a '>' in the prompt? If so, you might need to change the order of your list of matches. You might also look at exp_continue. I'm just guessing.


    Caution: Contents may have been coded under pressure.
      The first command is only executed for each $table in @tables. Regarding the exp_continue command, this was tried but in would just keep cycling on the first $table. The command prompt is ">".
        Still guessing, but I think what you want is multiple calls to expect, one for each time you want it to respond to something you've sent:
        $exp->expect(180, [ qr/>/i, sub { my $self = shift; sleep(1); $self->send("TABLE $table; FORMAT 132 PACK\r"); }]); $exp->expect(180, [ qr/UNKNOWN TABLE/i, sub { my $self = shift; sleep(1); $self->send("")ort\r"); next; }], [ qr/TABLE: $table/i, sub { my $self = shift; $self->send(""); }]); ...etc...

        Caution: Contents may have been coded under pressure.