Re: Expect.pm passing $exp to subroutine ** line number correction

[email protected] (Noah)
Newsgroups perl.beginners
Message-ID <[email protected]>
Hi John and List members,

Your advice has come in useful but still I am unable to keep the expect 
object variable $exp from one subroutine to another subroutine. 
sometihng gets broken and not sure what it is.  still running in to 
problems at line 168 (formerly 167).

Here is line 168:
"$exp->expect($timeout, $prompt);"

here is the error:
Can't call method "expect" on an un
defined value at ./get.config.pl line 167.


here is the relevant code:


sub figureOutHostnames {
     my $cmd = "telnet -l $user $SANEip";
     my $exp = new Expect() || die "Cannot spawn ssh command \n";

     $exp->raw_pty(0);
     $exp->spawn("$cmd");

       $exp->expect($timeout,
         ['assword:',
         sub { my $fh = shift; $fh->send("$pw\n");$stat=0;}],
         [timeout => sub {print STDERR " *** _login:Timed out waiting for
(auth) 'assword:'\n";
           $stat=1;}]
        );

     $prompt = qr/$blah >/;
     $patidx = $exp->expect($timeout, [$prompt]);

     $exp->send("2");
     $patidx = $exp->expect($timeout, [$prompt]);

     # Select blah
     $exp->send("1");

     while ($loginCount < $loginChoice) {
         my $prompt = qr/$blah >/;
         $exp->expect($timeout, [$prompt]);
         $exp->send($loginCount);

         # count area
         $exp->expect($timeout, [$prompt]);
         $read = $exp->before();

         my $upperNumber = 1;
         while ($upperNumber) {
              $upperNumber++;
              last if $read !~ /$upperNumber\s\)\s/;
         }
         $areaUpperLimit = $upperNumber - 1;

         while ($areaCount < $areaUpperLimit) {
             # send area count
             my $prompt = qr/$blah >/;
             $exp->send($areaCount);

             # count states
             $exp->expect($timeout, [$prompt]);
             $read = $exp->before();

             my $upperNumber = 1;
             while ($upperNumber) {
                 $upperNumber++;
                 last if $read !~ /$upperNumber\s\)\s/;
             }
             $stateUpperLimit = $upperNumber - 1;

             while ($stateCount < $stateUpperLimit) {
                 # send state number

                 my $prompt = qr/$blah >/;
                 $exp->send($stateCount);

                 # count cities
                 $exp->expect($timeout, [$prompt]);
                 $read = $exp->before();

                 my $upperNumber = 1;
                 while ($upperNumber) {
                     $upperNumber++;
                     last if $read !~ /$upperNumber\s\)\s/;
                 }
                 $cityUpperLimit = $upperNumber - 1;

                 # send city number
                 while ($cityCount < $cityUpperLimit) {

                     my $prompt = qr/$blah >/;
                     $exp->send($cityCount);

                     # count element number
                     $exp->expect($timeout, [$prompt]);
                     $read = $exp->before();

                     my $upperNumber = 1;
                     while ($upperNumber) {
                         $upperNumber++;
                         last if $read !~ /$upperNumber\s\)\s/;
                     }
                     $elementUpperLimit = $upperNumber - 1;

                     # send element number
                     while ($elementCount < $elementUpperLimit) {

                         my $prompt = qr/$blah >/;
                         $exp->send($elementCount);
                         &grabConfig($exp);
                         $elementCount++;
                     }
                     my $prompt = qr/$blah >/;
                     $exp->send("p");
                     $cityCount++;
                 }
                 my $prompt = qr/$blah >/;
                 $exp->send("p");
                 $stateCount++;
             }
             my $prompt = qr/$blah >/;
             $exp->send("p");
             $areaCount++;
         }
         my $prompt = qr/$blah >/;
         $exp->send("p");
         $loginCount++;
     }
     my $prompt = qr/$blah >/;
     $exp->send("x");
     $exp->soft_close();
}


sub grabConfig {
     my $prompt = qr/^$userPrompt\@[a-z\.0-9\-]+>/;
     print "************* $userPrompt $timeout $exp\n";

     $exp->expect($timeout, $prompt);
     $read = $exp->before();

     exit 0;

     #get hostname
     $read =~ /^$userPrompt@([^>]+)>\s/ and $hostname = uc $1;
     $outputFilename = "$hostname.config.txt";

     #delete saved configuraiton file

     # open new configuration file
     open (OUTPUT, ">$outputFilename")  || die "Cannot open 
'$outputFilename' $!";

     $read = $exp->before();
     $exp->expect($timeout, [$prompt]);

     print OUTPUT "$read";

     $exp->send("quit\n");
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.