I think I must be missing something about the
if bit - it doesn't seem to do anything. If that's right, then you could get the same result with
print "$in was not a number!"; input() unless $in =~ /\d/;
which does get it on one line, though not, I agree, with a trailing if :). On the other hand if the line
$in;
is a line you need (perhaps an idiom I don't know - if so, what does it do? - in any event it has a certain classically simplicity about it) - then to get it on one line you
could use the conditional operator, although there might be some pros and cons here I don't know about:
$in =~ /\d/ ? $in : print "$in was not a number!";
That doesn't let you call
input(), but that's ok if you agree with
lesstrat that it might get a bit ropey if you call
input() from within
input().
On the broader question of how to detect numbers, I find that what I usually want to isolate is everything that isn't not a number, so my favourite idiom is
$in !~ /\D/
But this question is much more complicated than one would think, as
these and
threads explain.
§
George Sherston
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.