#!/usr/bin/perl -w use strict; my $prompt = "Enter the start date (mm/dd/yyyy hh:mm): "; my $line; while ( (print "$prompt"), $line =) { next if $line =~ /^\s*$/; #simple reprompt on blank line my ($month, $day, $year, $hour, $minute) = $line =~ m|^\s*(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)\s*$|; if (!defined $minute) #regex failed initial "laugh test" { print "illegal format!\n"; next; } if ($month <1 or $month>12) { print "got month of $month - must be 1-12\n"; next; } if ($day <1 or $day>31) { print "got day of $day - must be 1-31\n"; next; } # more conditions here #finally try to convert the date to an epoch and see #if that works, eg 2/31/2012 should fail... #all tests pass, you have a "good date time" } __END__ C:\TEMP>commandloop5.pl Enter the start date (mm/dd/yyyy hh:mm): fjfjf 97609876 afadfa illegal format! Enter the start date (mm/dd/yyyy hh:mm): 0/0/2012 99:00 got month of 0 - must be 1-12 Enter the start date (mm/dd/yyyy hh:mm): 1/0/2012 99:00 got day of 0 - must be 1-31 Enter the start date (mm/dd/yyyy hh:mm): I stopped program