Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I having trouble comparing lines from input in Perl?

I don't know what I could be doing wrong with this simple transaction, but it's not working:

print "OK? (y or n)\n";
$ans = <>;
print "\n";
if($ans eq "y"){print $ans;}

I basically want to know how to test the user input. This little bit of code won't work for me. I'm just trying to print $ans if y is entered by the user.

Any suggestions?

EDIT: - I have also tried single quotes

like image 866
CheeseConQueso Avatar asked Dec 05 '25 06:12

CheeseConQueso


1 Answers

You're doing a couple things wrong.

(1) Don't use the diamond operator (<>) when you want <STDIN>. The diamond operator will also read files from @ARGV, which you probably don't want.

(2) $ans will never be equal to "y" unless you chomp it first. It will have a newline at the end.

like image 136
Matt K Avatar answered Dec 07 '25 22:12

Matt K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!