Having trouble getting my interactive perl script to work properly

So I'm making an interactive perl script, but I can't get it to work properly. I'm trying to make a script that tell the user to input either 'q' or 'Q' to terminate the program, or 'c' to continue it. If they input anything other than those three keys, it should prompt the user again and again until they input an appropriate key. If they input a 'q' or 'Q' the program is terminated. If they input 'c' then it continues to do two functions. The second function I'm saving for later, but the first function involves the program asking the user for two three dimensional coordinates, and then computing and outputting a distance. If any of the users inputs weren't a number it would display an error message. This is the code I have set up for all this, but everytime I input anything in the beginning when it asks for the 'q', 'Q', or 'c' inputs it just repeatedly asks the user to keep inputting something, and never exiting or continuing on to the function, even if I input a 'q', 'Q', or 'c'. Any idea what I need to do to fix this, and if there's anything else wrong with my code?

use warnings;
use strict;
use Scalar::Util 'looks_like_number';
my $i;
$i=0;
while ($i==0)
{
        print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        my $input;
        $input = <STDIN>;
        if (($input ne "q") || ($input ne "Q") || ($input ne "c"))
        {
                print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        }
        elsif (($input eq "q") || ($input eq "Q"))
        {
                exit;
        }
        else
        {
                print "Please enter one x coordinate: ";
                my $x1;
                $x1 = <STDIN>;
                print "Please enter one y coordinate: ";
                my $y1;
                $y1 = <STDIN>;
                print "Please enter one z coordinate: ";
                my $z1;
                $z1 = <STDIN>;
                print "Please enter a second x coordinate: ";
                my $x2;
                $x2 = <STDIN>;
                print "Please enter a second y coordinate: ";
                my $y2;
                $y2 = <STDIN>;
                print "Please enter a second z coordinate: ";
                my $z2;
                $z2 = <STDIN>;
                my $answer;
                if (($x1 !~ /^-?0/ && looks_like_number($x1)) && ($y1 !~ /^-?0/ && looks_like_number($y1)) && ($z1 !~ /^-?0/ && looks_like_number($z1)) && ($x2 !~ /^-?0/ && looks_like_number($x2)) && ($y2 !~ /^-?0/ && looks_like_number($y2)) && ($z2 !~ /^-?0/ && looks_like_number($z2)))
                {
                        $answer = sqrt(((($x2)-($x1))**2)+((($y2)-($y1))**2)+((($z2)-($z1))**2));
                        print "The distance between the two points is $answer";
                }
                else
                {
                        print "Error, you must only input numbers.";
                }
        }
}

My perl is non-existent, but I see

  • the $i variable not being modified anywhere in the - thus infinite - loop
  • the $input test always being true due to the || operator

To clarify RudiC's 2nd remark:

if (($input ne "q") || ($input ne "Q") || ($input ne "c"))

Any variable is always not equal to "q" or not equal to "Q" etc..
The && operator should be used here..

1 Like

I changed the || to && but I'm still getting the same thing where it just continually prompts the user to enter a key, even if they enter in 'q', 'Q', or 'c'

Remove the ENTER or RETURN key out of the captured input.

my $input = <STDIN>;
chomp $input;
if (($input ne "q") && ($input ne "Q") && ($input ne "c")) {
   ...
}

or, ignore anything else after the first character.

my $input = <STDIN>;
if($input !~ /^[Qqc]/) {
   ...
}

Thanks Aia! It's working now! I'll either update this thread or start a new one when I'm nearing the end of the full program, thanks again for the help!