Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: problems using Expect.pm in foreach loop

by duyet (Friar)
on Sep 10, 2011 at 06:12 UTC ( [id://925211]=note: print w/replies, xml ) Need Help??


in reply to Re: problems using Expect.pm in foreach loop
in thread problems using Expect.pm in foreach loop

indeed, what if you run your code without the check?
  • Comment on Re^2: problems using Expect.pm in foreach loop

Replies are listed 'Best First'.
Re^3: problems using Expect.pm in foreach loop
by gg48gg (Sexton) on Sep 11, 2011 at 00:11 UTC

    The reason I was doing that is because if the '/^\s*exit\s*$/i' matched, that means the isql code is an exit statement, which would cause the isql program to terminate, thus I would not want to expect another '\d+\>' prompt.

    Taking the if statement out, makes the code work as expected, although I'm not sure why. Also, it does not seem to cause a problem when the exit statement is given. It doensn't hang expecting another prompt. The program just exits, but I really don't understand why.

    Can someone help explain? What if I do want to only expect a prompt if the $line does not match '/^\s*exit\s*$/i' ?

    Thanks for helping me get this code working and thanks in advance for helping me understand the behavior better.

    -G

      The postfix if (! $line=~m/^\s*exit\s*$/i) is not doing what you expected.

      #!/usr/bin/env perl use strict; my $line = 'not an exit statment'; print "line matched ! =~\n" if (! $line =~ m/^\s*exit\s*$/i ); print "line matched !(=~)\n" if (! ($line =~ m/^\s*exit\s*$/i) ); print "line matched not =~ \n" if (not $line =~ m/^\s*exit\s*$/i ); print "line matched if !~\n" if ( $line !~ m/^\s*exit\s*$/i ); print "line matched unless =~\n" unless ($line =~ m/^\s*exit\s*$/i); __END__ line matched !(=~) line matched not =~ line matched if !~ line matched unless =~

      The ! operator has a much higher precedence than the not operator does. Using parentheses with the ! applies it to the result of the match; not with its lower precedence does so by default.

      When $exp->send sends the exit command to isql, isql disconnects and Expect.pm notices and handles that. If you are writing a production-quality program - especially one that will run automatically - you'll want to code your own checks for errors, disconnects, etc. OTOH, if this is for your own use only, run manually to simply automate a task, then you can usually get away with letting the Expect module handle anomalies. (I assume you're backing up whatever DB objects are being modified prior to letting a program make changes...)


      Update: added != and unless tests as additional examples

        That was truly an excellent response keszler. Thank you for that! I understand now what was happening with my attempt to match "exit".

        Also, thank you for the added info about error checking the isql exit.

        Thanks everyone for your posts.
        just wanted to thank you for your exellent replies. Very helpful! THANK YOU!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://925211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found