If you must validate user input, it is worth using a prompt module. It not only detects the errors, it gives the user a chance to correct them.
#!/usr/bin/perl use strict; use warnings; use IO::Prompt::Hooked; my $days = prompt( message => "Enter no of days older (1-90):", error => "Invalid input, please try again\n", validate => sub { my $days = shift; return ( $days =~ /^[1-9]\d?$/ and $days <= 90 ); }, ); print "You entered $days days\n\n";

The following sample run shows the recovery from several errors.

:tt.pl Enter no of days older (1-90): a Invalid input, please try again Enter no of days older (1-90): ! Invalid input, please try again Enter no of days older (1-90): 333 Invalid input, please try again Enter no of days older (1-90): 99 Invalid input, please try again Enter no of days older (1-90): 07 Invalid input, please try again Enter no of days older (1-90): 9 You entered 9 days :
Bill

In reply to Re: Validation of UserInput is a positive Integer or not? by BillKSmith
in thread Validation of UserInput is a positive Integer or not? by G Nagasri Varma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.