1

如何让 Perl 打印月份名称而不是数字,例如:9 应该显示为九月。我希望下面的程序打印月份的名称而不是它们的数字。

#!/usr/bin/perl

use strict;
use warnings;
use Date::Easter;

print "Please enter a year";
chomp (my $year = <STDIN>);

my ($m1, $d1) = easter ($year);
my ($m2, $d2) = julian_easter ($year);
my ($m3, $d3) = orthodox_easter ($year);

print "Gregorian => Month: $m1 Day: $d1\n";
print "Julian    => Month: $m2 Day: $d2\n";
print "Orthodox  => Month: $m3 Day: $d3\n";
4

2 回答 2

5

我正在使用内置POSIX模块:

#!/usr/bin/perl

use strict;
use warnings;
use Date::Easter;
use POSIX qw(strftime);

my %dispach_sub = (
    easter          => \&easter,
    julian_easter   => \&julian_easter,
    orthodox_easter => \&orthodox_easter,
);

print "Please enter a year: ";
chomp( my $year = <STDIN> );

foreach my $key ( keys %dispach_sub ) {

    print "[$key]\t";
    my ( $month, $day ) = $dispach_sub{$key}->($year);
    print POSIX::strftime( "Month: %B Day: %d",
        0, 0, 0, $day, $month - 1, $year - 1900 ),
      "\n";
}
于 2013-04-04T01:26:53.180 回答
4

我懒得下载这个模块来查看月份是从 0 到 11 还是从 1 到 12。为此,我假设月份是从 0 到 11。

最简单的方法是使用月份名称创建一个数组:

my @month_list = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

my ($m1, $d1) = easter ($year);
my ($m2, $d2) = julian_easter ($year);
my ($m3, $d3) = orthodox_easter ($year);

print "Gregorian => Month: $month_list[$m1] Day: $d1\n";
print "Julian    => Month: $month_list[$m2] Day: $d2\n";
print "Orthodox  => Month: $month_list[$m3] Day: $d3\n";

如果月份从 1 到 12 编号,则在数组的开头添加一个假月份:

my @month_list = qw(Foo Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
于 2013-04-04T01:17:09.607 回答