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" ;
}