Hi, I am having a strange problem with expect.pm which hopefully others have encountered. I have set up my own prompt and do some checking and send a command to another program, I expect to wait for the result and display it before moving to the next prompt but I get my prompt back much earlier and after giving 1 to 2 more inputs, I get the previous result back. If I make it very simple, no subroutines, checks etc, I suppose it works fine. I used the recommended settings to get a pipe-like behaviour. 2) Is there a simpler way I can just have a running program in the background, give it input, fetch output and display it back on stdout, besides using expect. the cleaned up code is somewhat like this:
$mapfile="toy2.map"; $outfile="toy2.smv"; open(spcmap,"<",$mapfile) || die("Couldn't read $mapfile: $!"); expectsmv(); close($mapfile); sub expectsmv { my $exp = new Expect; my ($bout, $aout, $err); $exp->log_stdout(0); $exp->raw_pty(1); $exp->manual_stty(1); my $command="NuSMV -int"; $exp->spawn($command) or die "Cannot spawn $command: $!\n"; #$exp->log_file("expect_log"); # Remove the banner # $exp->send("\n"); my $patidx = $exp->expect(undef,'NuSMV > ', '-re','NuSMV\s+>\s+' ); #$exp->clear_accum(); #Setup NuSMV and load model file $exp->send("read_model -i $outfile \n"); $exp->send("process_model \n"); #$err = $exp->error; #if($err != 0){ print "Error=", $err ;} #$exp->clear_accum(); while(1) { print "B2S > "; my $cmd = <STDIN>; if($cmd =~ /\s*quit\s+/){ last; $exp->soft_close(); } elsif($cmd =~ /\s*query\s+./) { my($q, $c) = split(" ",$cmd,2); #$cmd = nusmv_q_trans($c); chomp($cmd); $exp->clear_accum(); if($cmd !~ /\b*error1\b*/){ $cmd = "check_spec -p \" $cmd \" "; print "\n $cmd \n"; $exp->send($cmd);} else { print "\n Error \n"; } #Do some pattern matching with either the simple interface my $patidx = $exp->expect(undef,'NuSMV > ', '-re','NuSMV\s+>\s+' ); $err = $exp->exp_error(); if($err != 0){ print "Error=", $err ;} else{ # print $patidx; ($bout = $exp->exp_before) =~ tr/\r//d; $err = $bout; #$bout = nusmv_res_trans($bout); print $bout; } } # end of elsif else # Not a Query { $exp->send($cmd); my $patidx = $exp->expect(undef,'NuSMV > ', '-re','NuSMV\s+>\s+' ); $err = $exp->exp_error; if($exp->error() != 0){ print $exp->error(); last;} else{ ($bout = $exp->exp_before) =~ tr/\r//d; ($aout = $exp->exp_after) =~ tr/\r//d; print $bout; } } #end of else } $exp->clear_accum(); } #end of subroutine #Routine for Translating Query into nusmv format sub nusmv_q_trans { my ($cmd) = @_; #change species names to bngl S# format # search for a species name; beginning with alphabet and replace with +S# found from hash print $cmd; #$cmd =~ s/\b([a-zA-Z])\w*\b/S$smap{$1} /g; $cmd =~ s{ \b([a-zA-Z])\w*\b } { $smap{$1} ? "S$smap{$1}" : return "error1" }xge; $cmd =~ tr/_//d; return $cmd; } sub nusmv_res_trans { my ($res) = @_; $res =~ s/\bS(\d+)\b/$spar[$1]/g; return $res; }
AT

20050713 ysth: reparented this original question under follow that had more info


In reply to Problem with expect.pm by AT
in thread expect.pm problem by AT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.