hi all,
ok - i'm a perl noob. but remember that helping others makes you feel good
i just started experimenting with Perl and I'm trying ask the user for confirmation, so I'm doing the following:
print "\nDo you wish to continue? (Y/n)";
my $ok = <>; # ask for user input
if ($ok == "y" || $ok == "Y" || $ok == ""){
print "you pressed y, Y or enter";
}
elsif($ok == "n" || $ok == "N"){
print "you pressed n or N";
}
else{
print "can't you read!";
}
"yes" would be the default here so I would like to allow the user to just press enter. The problem is any input is taken as a "Y".
maybe i have syntax problems so i simplified the code to:
my $ok = <>; # ask for user input
if ($ok == "y"){
print "you pressed y";
exit;
}
print "you didn't press y";
same thing... any input is taken as a "y".
what am I doing wrong? should i use regular expressions?
i also tried:
if ($ok == m/y/)
same thing - always evaluates as true.
this (=~) seems to work a bit better:
if ($ok =~ m/y/)
y is yes
n (or anything other than y in this case) is no
but ny is yes! (as long as there's a y somewhere in the string it evaluates to true)
best regards,
Mark
ok - i'm a perl noob. but remember that helping others makes you feel good
i just started experimenting with Perl and I'm trying ask the user for confirmation, so I'm doing the following:
print "\nDo you wish to continue? (Y/n)";
my $ok = <>; # ask for user input
if ($ok == "y" || $ok == "Y" || $ok == ""){
print "you pressed y, Y or enter";
}
elsif($ok == "n" || $ok == "N"){
print "you pressed n or N";
}
else{
print "can't you read!";
}
"yes" would be the default here so I would like to allow the user to just press enter. The problem is any input is taken as a "Y".
maybe i have syntax problems so i simplified the code to:
my $ok = <>; # ask for user input
if ($ok == "y"){
print "you pressed y";
exit;
}
print "you didn't press y";
same thing... any input is taken as a "y".
what am I doing wrong? should i use regular expressions?
i also tried:
if ($ok == m/y/)
same thing - always evaluates as true.
this (=~) seems to work a bit better:
if ($ok =~ m/y/)
y is yes
n (or anything other than y in this case) is no
but ny is yes! (as long as there's a y somewhere in the string it evaluates to true)
best regards,
Mark