|
|
The Perl script in Listing C.23 uses an array to store the number of days in each month (including leap years) and the Perl split function in order to validate a date string.
Listing C.23 verifyDate1.pl.
my($date) = "11/25/2003"; my($month) = 0; my($day) = 0; my($year) = 0; my(@daysInMonth) = (0,31,28,31,30,31,30,31,31,30,31,30,31); ($month, $day, $year) = split(/\//, $date); if( $year % 4 == 0 ) { if( ($year % 100 == 0) && ($year % 400 != 0) ) { $daysInMonth[1] = 29; } } if( $year >= 1 ) { if( ($day <= $daysInMonth[$month]) && ($day >= 1) ) { if( ($month > 0) && ($month <= 12) ) { print "Date $date is valid\n"; } else { print "Month $month is out of range\n"; } } else { print "Day $day is out of range\n"; } } else { print "Year $year must be at least 1\n"; }
You can invoke the Perl script verifyDate1.pl from the command line as follows,
perl -w verifyDate1.pl
and the output will be as follows:
Date 11/25/2003 is valid
|
|