5. My pretty Parrot (Cockatiel) 可愛的小奧姆
Moshi-moshi ?
My Parrot can say “Hello” when the telephone rang.
6. 另外的 Parrot 算術擅長
Parrot Assembler (PASM)
PASM
set I1, 33
set I2, 5
mod I3, I1, I2
if I3, REMA
print "5 is an integer divisor of 33"
branch DONE
REMA: print "5 divides 33 with remainder "
print I3
DONE: print "¥n"
end
33÷5=6、余り2 を計算する
(5 into 33 is 6, remainder 2)
55. // は C言語で単一行コメント(PL/I起源)
/* C comment style */
// #include <stdio.h>
必ず Syntax Error になる記号
# Shell - single line comment
# Perl - single line comment
56. JavaScript // problem
var r = new RegExp(“pattern”);
var r = /pattern/;
これは何?
//
// start a single-line comment.
To specify an empty regular expression,
use /(?:)/
67. 複数行モード/m 修飾子の廃止
Perl 5 Perl 6
/^ pattern $/m /^^ pattern $$/
正規表現の修飾子/mで「^」と「$」のマッチ規則が変わる
my $lines = "aaa¥nbbb¥nccc¥n"; my $lines = "aaa¥nbbb¥nccc¥n";
if ($lines =~ /^b/m) { print "ok¥n" } if $lines ~~ /^^b/ { say "ok" }
if ($lines =~ /bb$/m) { print "ok¥n" } if $lines ~~ /bb$$/ { say "ok" }
if ($lines =~ /^bbb$/m) { if $lines ~~ /^^bbb$$/ {
print "ok¥n"; say "ok" ;
} }
68. 文字列の先頭「¥A」末尾「¥z」の廃止
Perl 5 Perl 6
/¥A pattern ¥z/ /^ pattern $/
複数行モード/m に影響しないゕンカー記号「¥A」「¥z」
my $str = "abracadabra"; my $str = "abracadabra";
if ($str =~ /¥Aabra/) { print "ok¥n" } if $str ~~ /^abra/ { say "ok" }
if ($str =~ /abra¥z/) { print "ok¥n" } if $str ~~ /abra$/ { say "ok" }
if ($str =~ /¥Aabracadabra¥z/) { if $str ~~ /^abracadabra$/ {
print "ok¥n" ; say "ok" ;
} }
69. スペースとコメントが自由に
挿入できるモード/x がデフォルトに
Perl 5 Perl 6
/ pattern1 # コメントをここに書く / pattern1 # コメントをここに書く
pattern2 pattern3 pattern2 pattern3
/x /
my $str = "abracadabra"; my $str = "abracadabra";
if ( $str =~ / abra # ゕブラ if ( $str ~~ / abra # ゕブラ
ca # カ ca # カ
d abra # ダブラ d abra # ダブラ
/x # 終了 / # 終了
) { print "ok¥n" } ) { say "ok" }
if ($str =~ / abra cad abra /x) if $str ~~ / abra cad abra /
{ print "ok¥n" } { say "ok" }
70. スペースのマッチ
Perl 5 Perl 6
/¥ /x /¥ /
/¥Q ¥E/x /<' '>/
/[ ¥t¥r¥n]/x /<space>/
/[ ¥t¥r¥n]+/x /<ws>/
my $str = "This is¥ta ¥n pen."; my $str = "This is¥ta ¥n pen .";
if ($str =~ / This ¥ is /x) if $str ~~ / This ¥ is /
{ print "ok¥n" } { say "ok" }
if ($str =~ / This ¥Q ¥E is /x) if $str ~~ / This <' '> is /
{ print "ok¥n" } { say "ok" }
if ($str =~ / is [ ¥t¥r¥n] a /x) if $str ~~ / is <space> a /
{ print "ok¥n" } { say "ok" }
if ($str =~ / a [ ¥t¥r¥n]+ pen /x) if $str ~~ / a <ws> pen /
{ print "ok¥n" } { say "ok" }
71. 単一行モード/s の廃止
Perl 5 Perl 6
/./s # どんな文字(改行を含む) /./ # 改行を含むどんな文字
/./ # どんな文字も(改行は除く) /¥N/ # 改行以外のどんな文字も
/[¥x0D¥x0A]|¥x0D¥x0A/ /¥n/ # どんな端末の改行にもマッチ
my $c = '/*********************** my $c = '/***********************
* This is a C comment * * This is a C comment *
***********************/ ***********************/
#include <stdio.h> #include <stdio.h>
/* first */ /* first */
main() { printf("C"); }'; main() { printf("C"); }';
if ($c =~ m/(¥/¥*.+?¥*¥/)/) { if $c ~~ m{( ¥/¥* ¥N+? ¥*¥/ )} {
if ($1 eq "/* first */") { print "ok¥n" } if $0 eq "/* first */" { say "ok" }
} }
if ($c =~ m/(¥/¥*.+?¥*¥/)/s) { if $c ~~ m{( ¥/¥* [.|¥n]+? ¥*¥/ )} {
my $lines = $1; my $lines = $0;
if ($lines =~ /comment/) { print "ok¥n" } if $lines ~~ /comment/ { say "ok" }
} }
72. /e 修飾子の廃止 どこでも {code}
Perl 5 Perl 6
s/pattern/code/e s/pattern/{code}/
sub cm2inch { sub cm2inch($cm) {
my ($cm) = @_; sprintf('%.01f-inch', $cm / 6.5);
sprintf('%.01f-inch', $cm / 6.5); }
}
my $floppy = "3.5-inch FDD"; my $floppy = "3.5-inch FDD";
if ($floppy =~ s/(¥d+¥.¥d+)-inch/{ sprintf('%.02f-cm', if $floppy ~~ s/(¥d+¥.¥d+)-inch/{ sprintf('%.02f-cm', $0 * 6.5) }/ {
$1 * 6.5) }/ex) { if $floppy eq "22.75-cm FDD" { say "ok" }
if ($floppy eq "22.75-cm FDD") { print "ok¥n" } if $floppy ~~ s/(¥d+¥.¥d+)-cm/{ &cm2inch($0) }/ {
if ($floppy =~ s/(¥d+¥.¥d+)-cm/{ &cm2inch($1) }/ex) { if $floppy eq "3.5-inch FDD" { say "ok" }
if ($floppy eq "3.5-inch FDD") { print "ok¥n" } }
} }
}
cf. Acme::Hyde(単位変換) on CPAN
73. 新しい修飾子の書き方
Perl 5 Perl 6
m/pattern/i m/:i pattern/
s/pattern/replace/ig s:g/pattern/replace/
my $imas = "とかしつくして"; my $imas = "とかしつくして";
if ($imas =~ s/し/ち/g) { if $imas ~~ s:g/し/ち/ {
if ($imas eq "とかちつくちて") { if $imas eq "とかちつくちて" {
print "ok¥n"; say "ok";
} }
} }
80. 変数展開 <$var>
Perl 5 Perl 6
/ $var /x / <$var> /
my $var = "(This|That) is a (pen|book)"; my $var =
my $text1 = "This is a pen"; "(This|That)<space>is<space>a<space>(pen|book)";
my $text2 = "That is a book"; my $text1 = "This is a pen";
if ($text1 =~ /$var/) { my $text2 = "That is a book";
print "ok¥n"; if ($text1 ~~ m/<$var>/) {
} say "ok";
if ($text2 =~ /$var/) { }
print "ok¥n"; if ($text2 ~~ m/<$var>/) {
} say "ok";
}
81. 文字列 <'string'> のリテラル展開
Perl 5 Perl 6
¥Q$var¥E $var
¥Qstring¥E <'string'>
¥$¥Qstring¥E <'$string'>
my $err = "Can't locate Boofy.pm in ¥@INC"; my $err = "Can't locate Boofy.pm in ¥@INC";
my $soozy = "Boofy.pm"; my $soozy = "Boofy.pm";
if ($err =~ /¥Q$soozy¥E/x) { if $err ~~ /$soozy/ {
print "ok¥n"; say "ok";
} }
if ($err =~ /¥@INC/x) { if $err ~~ /<'@INC'>/ {
print "ok¥n"; say "ok";
} }