Hi Rasha,
You've already gotten an answer, but in case you want to debug this issue or similar ones in the future yourself, have a look at the Basic debugging checklist, in this case especially items 3 and 4 on that list. Also, I find Data::Dumper's Useqq option helpful:
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Useqq = 1;
print "Enter something: ";
my $char = <>;
print Dumper($char);
If I enter "3", this prints $VAR1 = "3\n";, which shows you there's a newline character (\n) at the end of the string, which you need to chomp off.
For getting user input, there are modules that can help you, such as the core module Term::ReadLine, and there are several good modules on CPAN. Or, even though this is a little overkill, there's the prompt function from the module ExtUtils::MakeMaker - that isn't a user input module, it has a whole different function, but its prompt function comes in handy, it's a core module that should almost always be available, and it supports a default input.
use Term::ReadLine;
my $term = Term::ReadLine->new;
my $line = $term->readline("Enter something: ");
print Dumper($line);
use ExtUtils::MakeMaker 'prompt';
my $value = prompt("Enter something:","default");
print Dumper($value);
For reading single keypresses, there's Term::ReadKey, but that has the disadvantage that it will also return keypresses like F1 or backspace.
use Term::ReadKey qw/ReadMode ReadKey/;
print "Press a key\n";
ReadMode 'cbreak';
my $key = ReadKey(0);
ReadMode 'restore';
print Dumper($key);
Lastly, as a beginner, beginning each script with use warnings; use strict; use diagnostics; should be very helpful to you - see also Use strict and warnings.
Hope this helps, -- Hauke D |