Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 14, 2019 09:07
Show Gist options
  • Save tomcha/f08849d03dcf82eb507552c308a1119c to your computer and use it in GitHub Desktop.
Save tomcha/f08849d03dcf82eb507552c308a1119c to your computer and use it in GitHub Desktop.
Perl入学式 第1回 ピザ会 課題 2019/04/13 回答
#!/usr/bin/env perl
# guess-ad-year.pl - 和暦を引数に与えると西暦年を応えてくれる
# ad は A.D. (Anno Domini = 西暦年) の略
#
# 例:
# guess-ad-year.pl 平成30年
# 2018年
# guess-ad-year.pl 昭和54年
# 1979年
use strict;
use warnings;
my $jp_year = shift || ""; # 後述の正規表現マッチで穏当にマッチを外すよう、未定義だった場合は空文字を設定する
if ( my ($jp_era_name, $jp_era_year) = $jp_year =~ /^(.*?)(\d+|元)年$/ ) {
$jp_era_year = 1 if $jp_era_year eq "元";
my $base_ad_year;
if ($jp_era_name eq "令和") {
$base_ad_year = 2018;
} elsif ( $jp_era_name eq "平成" ) {
$base_ad_year = 1988;
} elsif ( $jp_era_name eq "昭和" ) {
$base_ad_year = 1925;
} elsif ( $jp_era_name eq "大正" ) {
$base_ad_year = 1911;
} elsif ( $jp_era_name eq "明治" ) {
$base_ad_year = 1867;
}
my $ad_year = $base_ad_year + $jp_era_year;
print "西暦${ad_year}年\n";
} else {
die "認識できない書式です\n";
}
https://gist.github.com/xtetsuji/63c24bbda558c966fca0646c3a1fe211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment