SPUG: Default action from Except.pm

Joseph Werner telcodev at gmail.com
Thu Oct 30 12:14:08 PDT 2008


I see a problem with your phrasing of this problem, as given in your
code sample. You are asking for a default action for an undefined
timeout [wait forever].  If you just take the Perlish approach and
give a pattern that always matches:   m{\A.*\z}xms  then, well, it
will always match, right away.  I suspect that what you want is a
default case if a key string is not heard in a certain time frame.

If you gave a [more] specific example of what you want to do, I might
be of more help. Perl Expect shines in networked automation [Telnet,
SSH, etc] where timing is unpredictable; there is usually a better way
to do your task for most other cases.

Here is a couple of Perl scripts that are chatting with each other
using Perl Expect.  demoExpect.pl loosely follows your code example.
some_command.pl prints random sentences at random intervals, and
listens for some keywords.   Take a look at these examples; listen to
the wealth of criticism fellow SPUGers will certainly offer on my
examples; consider the implications of timing and clearing [or not
clearing] the accumulator and play with the code.

Good luck,
Joseph Werner

Begin demoExpect.pl
use strict;
use warnings;
use Expect;

my $timeout = 7;

my $regx1 = qr /.*name.*/;
my $regx2 = qr /.*city.*/;

my $exp = new Expect;
while (1) {
    my $case = $exp->expect( $timeout, '-re', $regx1, '-re', $regx2 );
    $case && $case == 1 && do {
        $exp->send("cmd MYNAME\n");
    $case && $case == 2 && do {
        $exp->send("act MYCITY\n");

    # Default case:
    $exp->send("def myDEFAULT\n");


Begin some_command.pl

use strict;
use warnings;
use Expect;

my $commandRe = qr {\A cmd (.*) \z}xms;
my $actionRe  = qr {\A act (.*) \z}xms;
my $defaultRe = qr {\A def (.*) \z}xms;

my $exp                 = Expect->exp_init( \*STDIN );
my $randomSleepInterval = 10;
my @randomTextSamples   = (
    "I am using Except.pm to automate a interactive script.",
    "My dog is named Boxer",
    "I adhere to the prevailing CPAN Perl community standards for programming.",
    "I can think of many uniquie city names in Michigan.",
"I would like a report that lists the number of patients by city that
visited between a certain date range.",
    "Seventy weeks are determined upon thy people and upon thy holy city.",
"He did say, however, that if they concentrated on the server edition
of Ubuntu that they could be profitable in two years.",
"This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.",
"Perl is a high-level programming language with an eclectic heritage
written by Larry Wall and a cast of thousands.",

while (1) {
    print $randomTextSamples[ int( rand( scalar @randomTextSamples ) ) ], "\n";
    sleep int( rand($randomSleepInterval) );
    my @input =
      $exp->expect( 0, '-re', $commandRe, '-re', $actionRe, '-re', $defaultRe );
    next if $input[1];
    print "\nI heard ", $input[2], "\n";

On Wed, Oct 29, 2008 at 8:27 PM, Mayank Agarwal
<mayank_a at students.iiit.ac.in> wrote:
> Dear all,
> I am using Except.pm to automate a interactive script. I searched net, but
> could not find, how to give a default action, if no expression matches in
> except construct.
> My code looks like the below:
> my $exp = new Expect;
> $exp->spawn("some_command")
> $exp->expect(undef,
>                  [qr/.*name.*/   =>
>                    sub {
>                      my $fh = shift;
>                      $fh->send("myname\n");
>                      exp_continue;
>                    }
>                   ],
>                  [qr/.*city.*/   =>
>                    sub {
>                      my $fh = shift;
>                      $fh->send("mycity\n");
>                      exp_continue;
>                    }
>                   ],
> );
> Thanks and Regards
> Mayank
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
>     POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>    MEETINGS: 3rd Tuesdays
>    WEB PAGE: http://seattleperl.org/

I require any third parties to obtain my permission to submit my
information to any other party for each such submission. I further
require any third party to follow up on any submittal of my
information by sending detailed information regarding each such
submission to telcodev at gmail.com
Joseph Werner

More information about the spug-list mailing list