開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
その他参考書籍
2.
コード(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";
Oogaboogoo/date.pm
#!/usr/bin/env perl
package Oogaboogoo::date;
use strict;
use warnings;
use utf8;
use 5.016;
use base qw(Exporter);
our @EXPORT = qw(get_day get_month);
our %EXPORT_TAGS = ( all => [@EXPORT]);
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, pod 14, 2013 $
ちなみに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 import *を使用
from Oogaboogoo.date import *
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))
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, pod 14, 2013 $
0 コメント:
コメントを投稿