Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl Beginner

by haukex (Archbishop)
on Nov 26, 2016 at 09:53 UTC ( [id://1176593]=note: print w/replies, xml ) Need Help??


in reply to Perl Beginner

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found