本連載では第一線のPerlハッカーが回替わりで執筆していきます。今回は同人誌
Perlの徒花(あだばな)Acmeモジュール
Perlでいつもお世話になるCPANモジュールですが、
もともとCPANには特に名前付けのルールもなくジョークモジュールがリリースされていました。しかしそのようなモジュールが多くなるにつれ、
たとえばよく知られているAcmeモジュールの一つに、
AcmeモジュールをはじめとしてCPANモジュールのインストールにはcpanmがお勧めです。コマンドラインから、
$ curl -L http://cpanmin.us | perl - App:cpanminus
$ cpanm Acme::EyeDrops
などでインストールします。
次に、'print "Hello World\n"'
と指定しています。これが変換対象となるコードです。
そして、
use strict;
use warnings;
use Acme::EyeDrops qw(sightly);
my $type = $ARGV[0] || 'beer';
print sightly( {
Shape => $type, # (1)
SourceString => 'print "Hello World\n"', # (2)
Regex => 1,
} );
$ perl eyedrops.pl coffee > coffee.pl 1 shapes completed. $ cat coffee.pl ''=~('('.'?'.'{'.('`'|'%').('['^'-').('`'| '!').( '`'|',').'"'.('['^'+').('['^')').('`'|')') .('`'|'.' ).('['^'/').('{'^'[').'\\'.'"'.('`'^'(').('`' |(( '%'))).('`'|',').('`'|',').('`'|'/').("\{"^ '[' ).('{'^',').('`'|'/').('['^')').('`'|','). ((( '`'))|'$').'\\'.'\\'.('`'|'.').'\\'.'"'. '"' .'}'.')');$:='.'^'~';$~='@'|'(';$^="\)"^ '['; $/='`'|'.';$,='('^'}';$\='`'|('!');$:= ')'^ '}';$~='*'|'`';$^='+'^'_';$/='&'|'@' ;$,= '['&'~';$\=','^'|';$:='.'^'~';$~=('@')| '(';$^=')'^'[';$/='`'|'.';$,='('^'}' ;$\='`'|'!';$:=')'^'}';$~='*'| '`';$^='+'^'_';$/=('&')| '@';$,='['&'~';#;#
もちろん実行すれば期待どおりの動作をします。
$ perl coffee.pl
Hello World
このように、
なお本稿の実行環境は、
さまざまなジョークモジュール
ここでは、
Acme::Tests──テストする側からテストされる側に
通常、
図2のように次々と問題が出題され、
$ cpanm --look Acme::Tests
Acme-Tests-0.03.tar.gz が展開されてディレクトリを移動します
$ perl Makefile.PL
$ make test
Who plays on slashdot.org ?
(1) Cowboy Neal
(1) Cowboy Neal
(1) Cowboy Neal
(1) Cowboy Neal
==> 1
Who Invents Perl ?
(1) Larry Nelson
(2) Larry Wall
(3) Larry King
(4) Some guy with "Perl" in his name
==> 2
2 + 2 = ?
==> 4
Who writes Acme.pm ?
(1) acme
(2) spoon
(3) ingy
(4) all of them
==> 1
Verifing your answer...
t/00.simple.t .. 1/?
# Failed test at t/00.simple.t line 18.
# Looks like you failed 1 test of 4.
t/00.simple.t .. Dubious, test returned 1 (wstat 256,
0x100)
Failed 1/4 subtests
テストを自作してみる
すばらしいことに、
Acme::Testsをuseして、__
行以降に問題文、----
)
package MyTests;
use strict;
use Acme::Tests -Base;
our $VERSION = '1.00';
__DATA__
コミックマーケット84 の3 日目は何曜日?
(1) 金曜日
(2) 土曜日
(3) 日曜日
(4) 月曜日
Ans: 4
----
今回、
use strict;
use MyTests;
use Test::More;
my $t = MyTests->new;
my (@questions, @answers);
while (my $q = $t->next_question) {
print STDERR "\b\n$q\n==> ";
my $ans = <>;
push @questions,$q;
push @answers,$ans;
}
print STDERR " 結果は...\n";
for (0..$#questions) {
ok( $t->is_correct($questions[$_],$answers[$_]) )
}
done_testing;
$ prove t/mytest.t
コミックマーケット84 の3 日目は何曜日?
(1) 金曜日
(2) 土曜日
(3) 日曜日
(4) 月曜日
==>
ちゃんとテストが出題されました。とても楽しいですね!
Acme::Addslashes── PHPのあの関数を導入してみる
ところで、
リスト4を実行すると、
use 5.012;
use strict;
use Acme::Addslashes qw(addslashes);
my $unsafe_str = "evil code";
my $slashed_str = addslashes( $unsafe_str );
say $slashed_str;
$ perl addslases.pl
