Acme合体で猫-トースト装置をつくる
(firstly written in Japanese, later in English)
ワタシハ makamaka コンゴトモヨロシク・・・
皆さん、猫-トースト装置(バター猫のパラドックス)ってご存じですよね?
A - バターの塗られたトーストを落とすとバターの面が下になって床に落ちる
B - 猫を高いところから落とすと足から着地するAとBより、猫の背中にバターの塗られた面を上にしたトーストをつけて落とすと永久に床に着地しない
今日はこの猫-トースト装置のAcmeモジュールをつくってみたいと思います。ご用意するものは
- 猫 1Acme
- トースト 1Acme
CPANには猫に関するモジュールがいくつかありますが、本日はAcme::Meowを使いましょう。このモジュール、機能は少ないですがPerl上で子猫が飼える優れもの。
しかしここで残念なお知らせ。CPANにはAcme::Toastが今のところ存在しません。どうする!?
そこでバター云々の話は、いわゆるマーフィーの法則であることを思い出しましょう。そうするとAcme::Murphyがあります。このモジュール、「うまくいかなそうなことはなんであれ、うまくいかない」というマーフィーの法則に従い、useすると必ず失敗(die)する優れもの。
両者を合体させれば、猫-トースト装置になるんじゃないの!?
package Acme::CatToastDevice; use warnings; use strict; use Sex qw( Acme::Murphy Acme::Meow ); our $VERSION = '0.01'; 1;
合体には昨年のアドベントカレンダーでも取り上げられたSexを使います。いざAcme合体!
$ perl -I./lib -MAcme::CatToastDevice
MORE! Baby. There! Let's talk about our relationship. YEAH! OOOooooh... Baseball... I thought eight inches was longer than that. OOOooooh... YES! There! OOOooooh... Harder! Recursive inheritance detected in package 'Exporter' at ......../Sex.pm line 71. BEGIN failed--compilation aborted at lib/Acme/CatToastDevice.pm line 5. Compilation failed in require. BEGIN failed--compilation aborted.
エラーが出て失敗。終わった……
悔しいのでAcme::MeowのExport部分をコメントアウトします。
package Acme::Meow; use warnings; use strict; #require Exporter; #use base qw[ Exporter ]; ....
でもよく考えたら(よく考えなくても)Acme::Meow、Acme::Murphyどちらのモジュールも落下に関する機能がありません。そこで、ついでにAcme::CatToastDeviceにfallというメソッドを用意しましょう。呼ぶと無限ループします。
package Acme::CatToastDevice; use warnings; use strict; use Sex qw( Acme::Murphy Acme::Meow ); our $VERSION = '0.01'; sub fall { sleep 1 while 1; } 1;
下記のようなサンプルコードをつくって、再チェレンジ。
use strict; use Acme::CatToastDevice; print "This is a cat-toast device!\n"; Acme::CatToastDevice->new->fall; print "With which side did it fall?\n";
コンゴトモヨロシク・・・
$ perl test.pl MORE! Does it get bigger? Yes! Does it get bigger? Mmmmmm... Mmmmmm... OOOooooh... YES! Does it get bigger? OOOooooh... Yes! This is a cat-toast device!
よし、成功! ずっと着地しない! ……むなしいですね!!!
そんなこんなで、このお正月はあなたもAcme合体で新しいモジュールを創造してみては如何でしょうか。
* ----------------------------------------------------
* English version
* ----------------------------------------------------
TITLE: Make a cat-toast device from Acme-union.
I AM makamaka. NICE TO MEET YOU...
Do you know cat-toast device(Buttered cat paradox)?
A - If you throw a slice of buttered toast into the air it will always fall with the buttered side down.
B - A Cat will always land on its foots.From A and B, a slice of toast with butter to a cat's back may just not fall. (from uncyclopedia)
I'm going to make this device. Preparation:
- cat 1Acme
- toast 1Acme
There are some modules about cats on CPAN and I take Acme::Meow today. It has a only few functions but a nice module becase of keeping a kitten in Perl.
Regrettable information: Acme::Toast currently does not exist on CPAN.
Well, we remember the story of a sliec of toast with butter is the so-called Murphy's law. And Acme::Murphy on CPAN. It is a nice module because it always fails (die) to the letter 'if anything can go wrong, it will'.
Those two modules are combined, is it a cat-toast device!?
package Acme::CatToastDevice; use warnings; use strict; use Sex qw( Acme::Murphy Acme::Meow ); our $VERSION = '0.01'; 1;
We can use Sex module which was taken in last yeat advent calendar. Let's Acme-unite!
$ perl -I./lib -MAcme::CatToastDevice
MORE! Baby. There! Let's talk about our relationship. YEAH! OOOooooh... Baseball... I thought eight inches was longer than that. OOOooooh... YES! There! OOOooooh... Harder! Recursive inheritance detected in package 'Exporter' at ......../Sex.pm line 71. BEGIN failed--compilation aborted at lib/Acme/CatToastDevice.pm line 5. Compilation failed in require. BEGIN failed--compilation aborted.
Error.... Good-bye!
How disappointing! I comment out lines about Export in Acme::Meow!
package Acme::Meow; use warnings; use strict; #require Exporter; #use base qw[ Exporter ]; ....
But I thought deeply, both Acme::Meow and Acme::Murphy do not have a method relates to fall. So we added fall method into Acme::CatToastDevice. Calling it will cause an infinite loop.
package Acme::CatToastDevice; use warnings; use strict; use Sex qw( Acme::Murphy Acme::Meow ); our $VERSION = '0.01'; sub fall { sleep 1 while 1; } 1;
Retry with the below sample code.
use strict; use Acme::CatToastDevice; print "This is a cat-toast device!\n"; Acme::CatToastDevice->new->fall; print "With which side did it fall?\n";
NICE TO MEET YOU...
$ perl test.pl MORE! Does it get bigger? Yes! Does it get bigger? Mmmmmm... Mmmmmm... OOOooooh... YES! Does it get bigger? OOOooooh... Yes! This is a cat-toast device!
Successful! It never land! It's in vain!!!
Well, in this winter vacation, would you like to unite Acme modules into a new Acme.