Welcome to the Monastery | |
PerlMonks |
Re: Perl Beginnerby haukex (Archbishop) |
on Nov 26, 2016 at 09:53 UTC ( [id://1176593]=note: print w/replies, xml ) | Need Help?? |
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:
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.
For reading single keypresses, there's Term::ReadKey, but that has the disadvantage that it will also return keypresses like F1 or backspace.
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,
In Section
Seekers of Perl Wisdom
|
|