開発環境
- 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) の11章(オブジェクト入門)、11.11(練習問題)2を解いてみる。
2.
コード(TextWrangler)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
{ package LivingCreature;
sub speak{
my $class = shift;
if(@_){
print "a $class goes @_!\n";
} else {
print " a $class goes ", $class->sound, "!\n";
}
}
}
{ package Person;
our @ISA = qw(LivingCreature);
sub sound{ "hummmm" }
}
{ package Animal;
our @ISA = qw(LivingCreature);
sub sound{ die "動物は話をしない"; }
sub speak{
my $class = shift;
die "動物は話をしない" if @_;
$class->SUPER::speak;
}
}
{ package Cow;
our @ISA = qw(Animal);
sub sound{ "moooo" }
}
{ package Horse;
our @ISA = qw(Animal);
sub sound{"neigh"}
}
{ package Sheep;
our @ISA = qw(Animal);
sub sound{"baaaah"}
}
{ package Mouse;
our @ISA = qw(Animal);
sub sound { 'squeak' }
sub speak{
my $class = shift;
$class->SUPER::speak;
print "[but you can barely hear it!]\n";
}
}
my @lcs = qw(Cow Horse Sheep Mouse Person);
for(@lcs){
$_->speak;
}
Person->speak("Hello, World");
Cow->speak("Hello, World");
入出力結果(Terminal)
$ ./sample.pl a Cow goes moooo! a Horse goes neigh! a Sheep goes baaaah! a Mouse goes squeak! [but you can barely hear it!] a Person goes hummmm! a Person goes Hello, World! 動物は話をしない at ./sample.pl line 24. $
0 コメント:
コメントを投稿