ラベル Perl 6 の投稿を表示しています。 すべての投稿を表示
ラベル Perl 6 の投稿を表示しています。 すべての投稿を表示

2008年11月23日日曜日

Perl 6超入門(その3)Perl 5との基本的な違い

Perl Seminar NYという集まりでPerl 6の文法をおさらいした。会場で習ってきたことをインストールしたてのPerl 6を使って自分で試してみる。

まずはバージョン表示

% ./perl6 -v
This is Rakudo Perl 6, revision 0 built on parrot 0.8.1
for darwin-thread-multi-2level.

Copyright 2006-2008, The Perl Foundation.

%

文字列連結

Perl 5の「.」は「~」になった。メソッド呼び出しが「obj->method()」から「obj.method()」になったため。

Perl 5

my $x = 'a';
my $y = 'b';
print $x . $y; # abを表示

Perl 6

my $x = 'a';
my $y = 'b';
say $x ~ $y;

三項演算子(条件演算子)

「条件 ? 正の場合の値 : 偽の場合の値」という書き方はCからの伝統だったが、「条件 ?? 正の場合 !! 偽の場合」になった。

Perl 5

my $x = 1;
print $x == 1 ? 'yes' : 'no'; # yesを表示

Perl 6

my $x = 1;
say $x == 1 ?? 'yes' !! 'no';

配列に対するx演算子はxx

Perl 6

my @array = (1, 2, 3);
my @longarray = @array xx 3;
say join',', @longarray; # 1,2,3,1,2,3,1,2,3を表示

ジャンクション

Perl 6

「3」は「1または2または3」にマッチするので「yes」を表示する。

if (3 == (1 | 2 | 3)) {
say "yes";
}
else {
say "no";
}

「1かつ2かつ3」にはマッチしないので「no」を表示する。

if (3 == (1 & 2 & 3)) {
say "yes";
}
else {
say "no";
}

その他

エラーメッセージが(まだ)不親切。セミナーの会場でもエラーメッセージがわからずにみんなではまった。

たとえば、これを見せられて「=」ではなくて「==」にしなければいけないんだな、とわかるのは無理だ。ドキュメントの整備はこれから行われるとのこと。

% ./perl6 -e 'say "yes" if 3 = (1|2|3)'   
Method 'lvalue' not found for invocant of class 'PAST;Val'
current instr.: 'parrot;PAST;Compiler;as_post' pc 2924 (src/PAST/Compiler.pir:742)
called from Sub 'parrot;PAST;Compiler;if' pc 3461 (src/PAST/Compiler.pir:934)
called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368)
called from Sub 'parrot;PAST;Compiler;as_post' pc 2060 (src/PAST/Compiler.pir:500)
called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368)
called from Sub 'parrot;PAST;Compiler;pirop' pc 3061 (src/PAST/Compiler.pir:796)
called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368)
called from Sub 'parrot;PAST;Compiler;as_post' pc 2408 (src/PAST/Compiler.pir:614)
called from Sub 'parrot;PCT;HLLCompiler;compile' pc 434 (src/PCT/HLLCompiler.pir:303)
called from Sub 'parrot;PCT;HLLCompiler;eval' pc 868 (src/PCT/HLLCompiler.pir:502)
called from Sub 'parrot;PCT;HLLCompiler;command_line' pc 1450 (src/PCT/HLLCompiler.pir:774)
called from Sub 'parrot;Perl6;Compiler;main' pc 16140 (perl6.pir:168)
%

関連記事

2008年11月22日土曜日

Perl 6(Rakudo Perl)をインストール

Perl 6の実装は複数あり、PugsRakudo Perlが有名である。今回は手もとのLeopardでRakudoを試してみる。

ダウンロード

CPANで最新版のParrotをダウンロードする。MacportsにもParrotはあるが、月一度のリリースに追いついていないのでCPANを使ったほうがよいようだ。

展開

% tar zxvf parrot-0.8.1.tar.gz 
parrot-0.8.1/CREDITS
parrot-0.8.1/ChangeLog
parrot-0.8.1/Configure.pl
parrot-0.8.1/DEPRECATED.pod
parrot-0.8.1/DONORS.pod
parrot-0.8.1/LICENSE
parrot-0.8.1/MANIFEST
parrot-0.8.1/MANIFEST.SKIP
parrot-0.8.1/MANIFEST.generated
parrot-0.8.1/META.yml
parrot-0.8.1/Makefile.PL
parrot-0.8.1/NEWS
parrot-0.8.1/PBC_COMPAT
parrot-0.8.1/PLATFORMS
(略)
parrot-0.8.1/tools/util/templates.json
parrot-0.8.1/tools/util/update_copyright.pl
parrot-0.8.1/xconf/samples/testfoobar
parrot-0.8.1/xconf/samples/yourfoobar
%

コンパイル

LeopardだとXcodeが必要。

% cd parrot-0.8.1
% perl Configure.pl
Parrot Version 0.8.1 Configure 2.0
Copyright (C) 2001-2008, The Perl Foundation.

Hello, I'm Configure. My job is to poke and prod your system to figure out
how to build Parrot. The process is completely automated, unless you passed in
the `--ask' flag on the command line, in which case I'll prompt you for a few
pieces of info.
(略)
Okay, we're done!

You can now use `make' to build your Parrot.
After that, you can use `make test' to run the test suite.

Happy Hacking,
The Parrot Team
%
% make
Compiling with:
xx.c
(略)
-L/Users/mint/tmp/parrot-0.8.1/blib/lib -L/Users/mint/tmp/parrot-0.8.1/blib/lib -lparrot -lm -lgmp -lreadline -framework OpenGL -framework GLUT -lcrypto -lintl -undefined dynamic_lookup -L/opt/local/lib
%
% make test
Compiling with:
xx.c
(略)
Test Summary Report
-------------------
t/stm/runtime.t (Wstat: 256 Tests: 5 Failed: 1)
Failed test: 4
Non-zero exit status: 1
Files=396, Tests=11657, 367 wallclock secs ( 3.43 usr 2.24 sys + 169.59 cusr 70.23 csys = 245.49 CPU)
Result: FAIL
make: *** [test] Error 1
%

失敗するテストもあるが気にしない。

% ./parrot -V
This is parrot version 0.8.1 built for nojit.
Copyright (C) 2001-2008, The Perl Foundation.

This code is distributed under the terms of the Artistic License 2.0.
For more details, see the full text of the license in the LICENSE file
included in the Parrot source tree.

%

コンパイル完了。実行可能なParrotのバイナリができる。

Perl 6をビルドするのは簡単で

% make perl6
make -C compilers/pct
make[1]: Nothing to be done for `all'.
(略)
perl -MExtUtils::Command -e cp languages/perl6/perl6 ./perl6
perl -MExtUtils::Command -e ExtUtils::Command::chmod 0755 ./perl6
./perl6 -e"say 'Hello, world.'"
Hello, world.
%

こうするとperl 6のバイナリもできる。最後のところで「Hello, world」を実際にPerl 6で実行している。

「make reallyinstall」をすると/usr/localにParrotがインストールされる(perl6は自動でインストールされない)が、遊んでみるだけなら自分の作業ディレクトリで十分である。

関連記事