2013年2月13日水曜日

開発環境

『続・初めての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 utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use Oogaboogoo::date qw(get_day get_month);

my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime;
print "Today is ", get_day($wday), ", ",  get_month($mon), " ", $mday, ", ",
  $year += 1900, "\n";

入出力結果(Terminal)

$ ./sample.pl
Today is sen, pod 13, 2013
$

Oogaboogoo/date.pm

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

our @EXPORT = qw(get_day get_month);
use base qw(Exporter);

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;

ちなみにJavaScriptの場合。

コード(BBEdit)

var Oogaboogoo = {},
    td = new Date(),
    o, getDay, getMonth;
Oogaboogoo.date = function () {
    var day = ["ark", "dip", "wap", "sen", "pop", "sep", "kir"],
        month = ["diz", "pod", "bod", "rod", "sip", "wax", "lin",
        "sen", "kun", "fiz", "nap", "dep"];
    this.getDay = function( n ) {
        if (! (typeof(n) === "number" && n >= 0 && n <= 6)) {
            throw {
                type: "曜日エラー",
                message: "有効な曜日ではありません"
            };
        }
        return day[n];
    };
    this.getMonth = function ( n ) {
        if(!(typeof(n) === "number" && n >= 0 && n <= 11)) {
            throw {
                type: "月エラー",
                message: "有効な月ではありません"
            };
        }
        return month[n];
    };
};
o = new Oogaboogoo.date(),
// グローバル変数に代入
getDay = o.getDay,
getMonth = o.getMonth;
    result = "Today is " + getDay(td.getDay()) +
        ", " + getMonth(td.getMonth()) + " " + td.getDate() +
        ", " + td.getFullYear();
$('#pre0').text(result);



pythonの場合。

コード(BBEdit)

sample.py

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

import datetime
# fromを利用
from Oogaboogoo.date import getDay, getMonth

td = datetime.date.today()
day = td.weekday() + 1 if td.weekday() < 6 else 0
print("Today is {0}, {1} {2}, {3}".format(  
     getDay(day), getMonth(td.month - 1), td.day, td.year))

入出力結果(Terminal)

$ ./sample.py
Today is sen, pod 13, 2013
$

0 コメント:

コメントを投稿