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


In reply to Re: Perl Beginner by haukex
in thread Perl Beginner by Rasha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.