cal.pl: ~~~~~~ #!/usr/bin/perl # コマンドライン引数をもらう die "usage: cal month year\n" unless @ARGV == 2; ($mon, $year) = @ARGV; # その月の末日を計算(2行目は閏年の計算) $lastday = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$mon - 1] + ($mon == 2 && ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0)); print " $year/$mon\n S M Tu W Th F S\n"; foreach((" ") x &getweek($year, $mon), 1 .. $lastday){ printf ('%2.2s ' , $_); print "\n" unless ++$days % 7; } print "\n"; exit; # 曜日を得る関数 sub getweek{ local($year, $month) = @_; if($month == 1 || $month == 2) {$year--;$month += 12;} int($year + int($year/4) - int($year/100) + int($year/400) + int((13*$month+8)/5) + 1) % 7; } cal.pl 9 2000 に対する出力: ~~~~~~~~~~~~~~~~~~~~~~~~~~ 2000/9 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30