Hello enderw2014, welcome to the monastery!
As choroba already said, the angle bracket operator returns everything that was read, including the final newline. Use chomp, or alternatively chop to remove it (the former, essentially being a smarter version of the latter, is usually the better choice). You can even chomp a variable at the same time as assigning to it:
chomp($number = <STDIN>);
A few other tips, since I imagine you're just getting started with Perl:
- Format your code to look nice. It may not matter for throwaway scripts you only run once to get a specific task done, but for anything you'll be coming back to later, it really pays off.
- use strict;. This will catch all sorts of issues that might come back to bite you later on. use warnings; may also be worth considering.
- Depending on the Perl version(s) you're targetting, enable modern features and make use of them, e.g. say.
- Also, just for the future: if your script produces errors or warnings you don't understand, use diagnostics; to get long-form explanations of what's wrong.
Have fun with Perl!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.