如何让Perl打印月份名称而不是数字?例如:9应该显示为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";
答案 0 :(得分: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";
}
答案 1 :(得分: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);