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

[email protected] ("John W. Krahn")
Newsgroups perl.beginners
Message-ID <[email protected]>
Noah wrote:
> Hi,

Hello,

> thank you again list members for the quick response to my last question.
> 
> I am using the I am trying to figure out to pass my $exp to the
> subroutine "grabConfig".  $exp is the variable of my expect object.
> line 167 is "$patidx = $exp->expect($timeout, [$prompt]); "
> 
> 
> here is the error message:
> 
> "                   Please Enter Selection: > Can't call method "expect"
> on an undefined value at ./get.config.pl line 167."
> 
> here are snippets from my code:
> 
> ---- snip ---
> 
>                     # send element number
>                     while ($elementCount < $elementUpperLimit) {
> 
>                         $prompt = "$blah";
>                         $exp->send("$elementCount");

perldoc -q "What.s wrong with always quoting ..vars.?"

                           $prompt = $blah;
                           $exp->send( $elementCount );

>                         &grabConfig;

                           grabConfig();

perldoc perlsub

>                         $elementCount++;
>                     }
> 
> 
> sub grabConfig {
>     $prompt = "^$user\@[a-z\.0-9\-]+>";

       my $prompt = qr/^$user\@[a-z.0-9-]+>/;

>     $patidx = $exp->expect($timeout, [$prompt]);  <<<<<< line 169

According to the documentation:

<QUOTE>
$object->expect($timeout, @match_patterns)
</QUOTE>

The second argument to expect() should not be an array reference.

http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod

>     $read = $exp->before();
> 
>     #get hostname
>     $read =~ /^@([^>]+)>\s/;
>     $hostname = $1;

You should not use the numerical variables unless the match was successful:

       $read =~ /^@([^>]+)>\s/ and $hostname = $1;

Or:

       ( $hostname ) = $read =~ /^@([^>]+)>\s/;

>     $hostname = tr/[a-z]/[A-Z]/;

Why are you translating '[' to '[' and ']' to ']'?

       $hostname = tr/a-z/A-Z/;

Or:

       $read =~ /^@([^>]+)>\s/ and $hostname = uc $1;

>     $outputFilename = "$hostname.config.txt";
> 
>     #delete saved configuraiton file
>     unlink ($outputFilename);

You should verify that unlink worked correctly:

       unlink $outputFilename or warn "Cannot unlink '$outputFilename' $!";

But you don't really need to unlink the file as you are opening it 
read-only which will remove the previous contents upon opening.

>     # open new configuration file
>     open (OUTPUT, ">$outputFilename");

You should *always* verify that the file opened correctly:

       open OUTPUT, '>', $outputFilename or die "Cannot open 
'$outputFilename' $!";



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
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.