Powered by GeSHi

Source code for leapyear.pl

(view source code of leapyear.pl as plain text)

  1. #! perl
  2.  
  3. if ( ( index( $ARGV[0], "?" ) > -1 ) or ( $ARGV[1] ) or ( $ARGV[0] < 0 ) or ( $ARGV[0] > 10000 ) or ( ( $ARGV[0] ) and ( $ARGV[0] == NaN ) ) ) {
  4. 	print "\nLeapYear.pl,  Version 1.10\n",
  5. 	      "Check if the specified year is a leap year\n\n",
  6. 	      "Usage:    LEAPYEAR.PL  [ year ]\n\n",
  7. 	      "Where:    \"year\" is a year between 1 and 10000\n",
  8. 	      "          default is the current year\n\n",
  9. 	      "Returns:  message on screen;\n",
  10. 	      "          return code 0 if the specified year is not a leap year;\n",
  11. 	      "          return code 1 if it is;\n",
  12. 	      "          return code 9 for invalid arguments\n\n",
  13. 	      "Written by Rob van der Woude\n",
  14. 	      "http://www.robvanderwoude.com\n";
  15. 	exit 9;
  16. } else {
  17. 	$year = abs( $ARGV[0] );
  18. }
  19.  
  20. # Use default if no (valid) year was specified
  21. if ( ( $year < 0 ) or ( $year > 10000 ) or !( $year ) ) {
  22. 	# Parse time string
  23. 	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  24. 	$year = $year + 1900;
  25. }
  26.  
  27. # Leap years are divisible by 4, but not by 100, unless by 400
  28. if ( ( $year % 4 == 0 ) xor ( $year % 100 == 0 ) xor ( $year % 400 == 0 ) ) {
  29. 	print( "\n$year IS a leap year\n" );
  30. 	$rc = 1;
  31. } else {
  32. 	print( "\n$year is NOT a leap year\n" );
  33. 	$rc = 0;
  34. }
  35.  
  36. # Terminate with return code 1 if the year IS a leap year
  37. exit $rc
  38.