On 22/02/2012 20:48, Igor Dovgiy wrote:

TL;DR: the core of your program may be rewritten as...

print 'Please, enter an integer number, as I really need it: ';
chomp (my $user_input =<STDIN>);
if ($user_input =~ /^-?\d+$/) {
   print "My hero! You've actually entered<$user_input>, which is an
integer! I salute you!", "\n";
}
else {
   print "How dare you insult me with pathetic<$user_input>!?", "\n";
}

Hope this'll be helpful. )

Negative numbers aside, it seems more straightforward to insist that
there are no non-digit numbers in the input, hence

  if ( $number !~ /[^0-9]/ ) {
    print "Your number: $number\n"
  }

or

  unless ( $number =~ /[^0-9]/ ) {
    print "Your number: $number\n"
  }

or even

  unless ( $number =~ /\D/ ) {
    print "Your number: $number\n"
  }

(/\D/ being the inverse of /\d/)

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to