開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9) の14章(オブジェクトに関する高度なトピック)、14.7(練習問題)1を解いてみる。
1.
コード(TextWrangler)
sample.pl
#!/usr/bin/env perl use strict; use warnings; { package MyDate; my %methods = ("day" => 3, "month" => 4, "year" => 5); my @indexes = (0,0,0,0,1,1900,0,0,0); sub AUTOLOAD{ use Carp; our $AUTOLOAD; (my $method = $AUTOLOAD) =~ s/.*:://s; if(!( exists $methods{$method})){ carp "メソッド名($method)が分からない"; return; } my $index = $methods{$method}; return (localtime)[$index] + $indexes[$index]; } } print "day: " , MyDate->day , "\n"; print "month: " , MyDate->month, "\n"; print "year: ", MyDate->year,"\n"; print "perl: " , MyDate->perl,"\n";
入出力結果(Terminal)
$ ./sample.pl day: 2 month: 8 year: 2012 メソッド名(perl)が分からない at ./sample.pl line 26 perl: $
0 コメント:
コメントを投稿