読者です 読者をやめる 読者になる 読者になる

無精・短気・傲慢

perlの事 いろいろ

perl6 memo

array etc..

$ perl6 -e 'say ("a".."z")[5]'
f
$ perl6 -e 'say first-index { $_ > 5 }, (1..10)'
5
$ perl6 -e 'say [+] 1..10'
55
$ perl6 -e 'say join ",", 1..10 '
1,2,3,4,5,6,7,8,9,10
$ perl6 -e 'for 1..10 -> $x {say "[$x]";}'
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
$ perl6 -e '(1..10).reverse.say'
10 9 8 7 6 5 4 3 2 1
$ perl6 -e 'say join ":",map {$_ * 2}, (1..10).reverse'
20:18:16:14:12:10:8:6:4:2
$ perl6 -e 'my @x=(1..4);unshift(my @y,pop(@x));push(@y,shift(@x));say join ",",@y'
4,1
$

hash etc..

$ perl6 -e 'my %hash = "a"=>1,"i"=>2,"u"=>3,"e"=>4,"o"=>5;for %hash.kv -> $k,$v { say "$k:$v"}'
a:1
i:2
u:3
e:4
o:5
$ perl6 -e 'my %hash = "a"=>1,"i"=>2,"u"=>3,"e"=>4,"o"=>5;say %hash'
("a" => 1, "e" => 4, "i" => 2, "o" => 5, "u" => 3).hash
$ perl6 -e 'my %hash = "a"=>1,"i"=>2,"u"=>3,"e"=>4,"o"=>5;say %hash<u>'
3
$ perl6 -e 'my %hash = "a"=>1,"i"=>2,"u"=>3,"e"=>4,"o"=>5;say %hash.keys'
a i u e o
$ perl6 -e 'my %hash = "a"=>1,"i"=>2,"u"=>3,"e"=>4,"o"=>5;say %hash.values'
1 2 3 4 5
$

tertiary operator

$ perl6 -e 'say map {($_%3??""!!"foo") ~ ($_%5??""!!"baz") or $_},1..31'
1 2 foo 4 baz foo 7 8 foo baz 11 foo 13 14 foobaz 16 17 foo 19 baz foo 22 23 foo baz 26 foo 28 29 foobaz 31
$ 

perl5 vs perl6

微妙な違いがあって…

$ perl -E 'say join ",",map {$_*2} (1..5) x 2'
2,4,6,8,10,2,4,6,8,10
$
$ perl6 -e 'say join ",",map {$_*2},(1..5) xx 2'
2,4,6,8,10,2,4,6,8,10
$
$ perl -E 'say "ABC" . "DEF"'
ABCDEF
$
$ perl6 -e 'say "ABC" ~ "DEF"'
ABCDEF
$

コマンドライン

  • ハノイの塔 (う~ん。なんか面倒くさい)
$ perl -E 'sub h{return if(!$_[0]);push(@_,$_[0]-1);h(@_[-1,1,3,2,4]);$_[4]->(@_);h(@_[-1,2,1,3,4]);} h(pop,A..C,sub {say "$_[1]($_[0])=>$_[3]";})' 3
A(1)=>C
A(2)=>B
C(1)=>B
A(3)=>C
B(1)=>A
B(2)=>C
A(1)=>C
$ perl6 -e 'sub h{return if !@_[0] ;push(@_,@_[0]-1);h(@_[5,1,3,2,4]);@_[4].(@_);h(@_[5,2,1,3,4]);};sub MAIN($x){h($x ,"A".."C",sub {say "@_[1]\(@_[0]\) =\> @_[3]";})}' 3
A(1) => C
A(2) => B
C(1) => B
A(3) => C
B(1) => A
B(2) => C
A(1) => C
$ 

コマンドラインの引数をチェックしてくれるらしい

$ perl6 -e 'sub MAIN($a,$b){say $a**$b;}'
Usage:
  -e '...' <a> <b> 
$ perl6 -e 'sub MAIN($a,$b){say $a**$b;}' 2
Usage:
  -e '...' <a> <b> 
$ perl6 -e 'sub MAIN($a,$b){say $a**$b;}' 2 4
16
$ perl6 -e 'sub MAIN($a,$b){say $a**$b;}' 2 4 5
Usage:
  -e '...' <a> <b> 
$ 
  • ディスパッチ
$ perl6 -e 'multi MAIN("add",$x,$y){say $x+$y};multi MAIN("sub",$x,$y){say $x-$y;};multi MAIN("div",$x,$y){say $x/$y;}' add 2 3
5
$ perl6 -e 'multi MAIN("add",$x,$y){say $x+$y};multi MAIN("sub",$x,$y){say $x-$y;};multi MAIN("div",$x,$y){say $x/$y;}' sub 2 3
-1
$ perl6 -e 'multi MAIN("add",$x,$y){say $x+$y};multi MAIN("sub",$x,$y){say $x-$y;};multi MAIN("div",$x,$y){say $x/$y;}' div 2 3
0.666667
$ 

正規表現素因数分解する

$ perl -E 'sub f{my $x=shift;my @ans =();my $N=('o' x $x);for(;$N=~/^(oo+?)\1+$/;$N=~s/$1/o/g){push(@ans,length($1));}push(@ans,length($N)); join("x",@ans);} say f(102);'
2x3x17
$
$ perl6 -e 'sub f($x){my @ans =();my $N= "o"x$x;my $s;loop (;$N ~~ /^(oo+?)$0+$/;$N ~~ s:g/$s/o/) {$s=$0;push(@ans,chars($s));};push(@ans,chars($N)); join("x",@ans);} ;say f(102);'
2x3x17
$