2012年2月11日土曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の15章(Exporter), 15.7(練習問題)、2を解いてみる。

2.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;
use Oogaboogoo qw(:all);

my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime;

my $od = number_to_wday_name($wday);
my $om = number_to_month_name($mon);
$year += 1900;
print "Today is $od, $om $mday, $year\n";

Oogaboogoo.pm、コード(TextWrangler)

#!/usr/bin/env perl
package Oogaboogoo;
use strict;
use warnings;
our @EXPORT = qw(number_to_wday_name number_to_month_name);
use base qw(Exporter);
our %EXPORT_TAGS = (all => [@EXPORT]);

my @wday = qw(ark dip wap sen pop sep kir);
my @month = qw(diz pod bod rod sip wax lin sen kun fiz nap dep);

sub number_to_wday_name{
  my $num = shift;
  die "曜日名に変換出来ません。" if $num < 0 || 7 < $num;
  $wday[$num];
}

sub number_to_month_name{
  my $num = shift;
  die "月名に変換出来ません。" if $num < 0 || 11 < $num;
  $month[$num];
}

1;

入出力結果(Terminal)

$ ./perl_program
Today is kir, pod 11, 2012
$

0 コメント:

コメントを投稿