Hello, The code below prompts the user for a start and end date for scraping historical temperature data from a website. I've used a few while loops to make sure the input is in the correct format and range. The problem is that once the range exception is caught and the user is prompted again, the format exception can't be caught. So the user could accidentally input a date before the earliest available date and the program would alert them of this, but then an incorrect format input would be accepted.

Is there a better way for me to handle these exceptions?

Thanks,

Dave

# First, the current date is determined use Date::Simple::D8 (':all'); my $today = Date::Simple::D8->today(); my $end_date = $today; # Prompt user for start date and verify it print "Please enter the collection start date (YYYYMMDD): "; my $start_date = <>; chomp ($start_date); while ($start_date !~ (/\d\d\d\d\d\d\d\d/)) { print "ERROR: Invalid format! Start date (YYYYMMDD): "; $start_date = <>; chomp ($start_date); } while ($start_date < 18960101) { print "ERROR: Data prior to 18960101 is not available! Start date +(YYYYMMDD): "; $start_date = <>; chomp ($start_date); } while ($start_date > "$today") { print "ERROR: Start date is in the future! Start date (YYYYMMDD): +"; $start_date = <>; chomp ($start_date); } print "Thank you, collection will begin with $start_date\n"; # Ask user if most recent available date should be used as end date print "Would you like to collect through the most recent available dat +e? (y/n): "; my $ans = <>; chomp ($ans); if ($ans eq "n" || $ans eq "N") { print "Please enter the collection end date (YYYYMMDD): "; my $end_date = <>; chomp ($end_date); while ($end_date !~ (/\d\d\d\d\d\d\d\d/)) { print "ERROR: Invalid format! End date (YYYYMMDD): "; $end_date = <>; chomp ($end_date); } while ($end_date < 18960101) { print "ERROR: Data prior to 18960101 is not available! End dat +e (YYYYMMDD): "; $end_date = <>; chomp ($end_date); } while ($end_date > "$today") { print "ERROR: End date is in the future! End date (YYYYMMDD): +"; $end_date = <>; chomp ($end_date); } print "Thank you, collection will end with $end_date\n"; }

In reply to Handling User Input by cheech

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.