Even on simple programs like this, CPAN modules can be a big help. In the following example, the modules require little additional code, but greatly extend the capability. The
IO::Prompt::Hooked not only takes care of the newline problem, it validates the input, and gives the user another chance if it is not valid. (There are more options available that you may wish to experiment with.)
Regexp::Common extends the validation to allow any valid real number format.
#! /usr/bin/perl -l
use strict;
use warnings;
use IO::Prompt::Hooked;
use Regexp::Common;
my %opt = (
message => "please enter a number: ",
validate => qr/^$RE{num}{real}$/,
error => "Input must be a number\n",
);
my $n1 = prompt(%opt);
my $n2 = prompt(%opt);
if ($n1 <= $n2) {
print "$n1 <= $n2\n" ;
}
else {
print "$n2 <= $n1\n" ;
}
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.