2013年9月19日木曜日

開発環境

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

その他参考書籍

1, 2.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
require 'Oogaboogoo/date.pm';

my($mday, $mon, $year, $wday) = (localtime)[3,4,5,6];

$wday = Oogaboogoo::date::get_day($wday);
$mon = Oogaboogoo::date::get_month($mon);
$year += 1900;

say "Today is $wday, $mon $mday, $year";

Oogaboogoo/date.pm

#!/usr/bin/env perl
package Oogaboogoo::date;
use strict;
use warnings;
use utf8;
use 5.016;

my @day = qw(ark dip wap sen pop sep kir);
my @month = qw(diz pod bod rod sip wax lin sen kun fiz nap dep);
sub get_day {
    my $n = shift;
    die unless $n >= 0 and $n <= 6;
    $day[$n];
}
sub get_month {
    my $n = shift;
    die unless $n >= 0 and $n <= 11;
    $month[$n];
}

1;

入出力結果(Terminal)

$ ./sample.pl
Today is pop, kun 19, 2013
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import time
import Oogaboogoo.date

td = time.localtime()
year = td.tm_year
mon = td.tm_mon
mday = td.tm_mday
wday = td.tm_wday

if wday < 6:
    wday += 1
else:
    wday = 0

mon = Oogaboogoo.date.getMonth(mon - 1)
wday = Oogaboogoo.date.getDay(wday)

print('Today is {}, {} {}, {}'.format(wday, mon, mday, year))

コード(BBEdit)

Oogaboogoo/date.py

#!/usr/bin/env pythonb3.3
#-*- coding: utf-8 -*-

day = ["ark", "dip", "wap", "sen", "pop", "sep", "kir"]
month = ["diz", "pod", "bod", "rod", "sip", "wax", "lin", 
    "sen", "kun", "fiz", "nap", "dep"]

def getDay(n):
    if not (type(n) == type(10) and n >= 0 and n <= 6):
        raise Exception("有効な曜日ではない")
    return day[n]

def getMonth(n):
    if not (type(10) == type(10) and n >= 0 and n <= 11):
        raise Exception("有効な月ではない")
    return month[n]

入出力結果(Terminal)

$ ./sample.py
Today is pop, kun 19, 2013
$

0 コメント:

コメントを投稿