2013年9月26日木曜日

開発環境

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

その他参考書籍

1.

コード(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';
use Oogaboogoo::date;

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

$wday = get_wday($wday);
$mon = get_month($mon);
$year += 1900;

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

コード(BBEdit)

Oogaboogoo/date.pm

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

our @ISA = qw(Exporter);
our @EXPORT = qw(get_wday get_month);
our @EXPORT_OK = qw();

our %EXPORT_TAGS = ();

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_wday {
    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 26, 2013
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

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

import time
from Oogaboogoo.date import *

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

wday = getWDay(wday)

# import *では下線で始まる属性はインポートできない
try:
    mon = _getMonth(mon - 1)
except Exception as err:
    print(type(err), err)

# 明示的にインポート
from Oogaboogoo.date import _getMonth
mon = _getMonth(mon - 1)

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 getWDay(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
<class 'NameError'> name '_getMonth' is not defined
Today is pop, kun 26, 2013
$

0 コメント:

コメントを投稿