Perl 6 前沿战报

刘鑫 <march.liu@gmail.com>

      金山软件 Kingsoft


      July 17, 2010




                      .    .   .   .   .   .
从创世纪到新的创世纪




         .   .   .   .   .   .
A Joke About The King Of Scripts




   Perl 是一门易学易用的语言。

               ——Larry Wall《Perl 语言编程》




                                   .   .   .   .   .   .
1987 年发布第一版




              .   .   .   .   .   .
2001 年愚人节的笑话
Python? Perl? All in one!




                       .    .   .   .   .   .
十年的努力 2001 ˜
           2010




              .   .   .   .   .   .
Parrot ˜ Pugs ˜ Parrot ˜ Rakudo




                          .   .   .   .   .   .
2010-06-30




             是的!祂来了!
               Parrot 2.5.0
             Rakudo 2010-06




                              .   .   .   .   .   .
You can do it.




   say ” h e l l o ”;




                        .   .   .   .   .   .
Or you can do it.




   say (” h e l l o ” ) ;




                            .   .   .   .   .   .
And you can do it yet.




   ” h e l l o ”. say ( ) ;




                              .   .   .   .   .   .
Of cause you can do it.




   ” h e l l o ”. say ;




                          .   .   .   .   .   .
example: grep in lines



   #!/ u s r / b i n / env p e r l 6

   u s e v6 ;

   my $exp = @∗ARGS [ 0 ] ;
   #new s t y l e l o o p and i f
   f o r $ ∗ IN . l i n e s −> $ l i n e {
         i f $ l i n e ˜˜ m/ $exp / {
                say $ l i n e ;
         }
   }



                                             .   .   .   .   .   .
Shorter is better




   #!/ u s r / b i n / env p e r l 6

   u s e v6 ;

   my $exp = @∗ARGS [ 0 ] ;
   f o r $ ∗ IN . l i n e s −> $ l i n e {
         s a y $ l i n e i f $ l i n e ˜˜ m/ $exp / ;
   }




                                                 .      .   .   .   .   .
正式的多路选择分支支持——given/when




                 .   .   .   .   .   .
g i v e n ( $x ) {
    when 5 {
        s a y ’ $x i s t h e number 5 ’ ;
    }
    when ” abc ” {
        s a y ’ $x i s t h e s t r i n g ” abc ” ’ ;
    }
    when Bool {
        s a y ’ $x i s t h e b o o l e a n q u a n t i t y ’ ˜ $x ;
    }
}




                                                 .    .    .    .     .   .
还记得 Perl 5 中的 if/unless 表达式吗?




                      .   .   .   .   .   .
现在 for 也可以。




              .   .   .   .   .   .
下面这个对象方法中使用了 for 表达式




               .   .   .   .   .   .
method p e r f o r m ( ) {
    u n l e s s $ ! done {
            . perform ( ) f o r @! dependencies ;
            &! c a l l b a c k ( ) ;
            $ ! done = True ;
    }
}




                                        .   .       .   .   .   .
C 风格循环




         .   .   .   .   .   .
l o o p (my $ i = 0 ; $ i <= 5 ; $ i ++) {
       print $i ;               # ”12345”
}




                                      .      .   .   .   .   .
my $ i = 0 ;
l o o p ( ; $ i <= 5 ; $ i ++) {
       print $i ;
}




                                   .   .   .   .   .   .
# ”12345”
l o o p (my $ i = 0 ; $ i <= 5 ; ) {
       print $i ;
       $ i ++;
}




                                       .   .   .   .   .   .
l o o p (my $ i = 0 ; ; $ i ++) {
       l a s t u n l e s s ( $ i <= 5 ) ;
       print $i ;
}




                                            .   .   .   .   .   .
my $ i = 0 ;
loop ( ; ; ) {
     l a s t u n l e s s ( $ i <= 5 ) ;
     print $i ;
     $ i ++;
}




                                          .   .   .   .   .   .
支持 === ,即 eqv 操作符




                    .   .   .   .   .   .
支持三值比较运算符 <=> ,即 leg 操作符




                       .   .   .   .   .   .
惰性..




  u s e v6 ;

  my @l = 1 . . 1 0 ;
  s a y @l ;




                        .   .   .   .   .   .
围观




 u s e v6 ;

 my %s c o r e s =
     Ana => 8 ,
     Dave => 6 ,
     C h a r l i e => 4 ,
     Beth => 4 ;




                            .   .   .   .   .   .
my $ s c r e e n w i d t h = 3 0 ;
my $ l a b e l a r e a w i d t h =
    1 + [ max ] %s c o r e s » . k e y » . c h a r s ;
my $ m a x s c o r e = [ max ] %s c o r e s » . v a l u e ;
my $ u n i t = ( $ s c r e e n w i d t h − $ l a b e l a r e a w i d t h )
               / $max score ;




                                                  .    .     .    .    .     .
say $ l a b e l a r e a w i d t h ;
f o r %s c o r e s {
      my $ f o r m a t = ’%− ’ ˜ $ l a b e l a r e a w i d t h ˜ ” s%s n
      p r i n t f $ f o r m a t , . key ,
              ’X ’ x ( $ u n i t ∗ . v a l u e ) ;
}




                                            .    .    .   .    .    .
Foreach 操作符




              .   .   .   .   .   .
%s c o r e s >>.k e y ;




                          .   .   .   .   .   .
相当于




      .   .   .   .   .   .
得到%source 每一个键值对的 key




                 .   .   .   .   .   .
Reduce 操作符




             .   .   .   .   .   .
my $label_area_width = 1 +
    [max] %scores».key».chars;




                                 .   .   .   .   .   .
max(a, b) 得到 a 和 b 中较大的一个




                    .   .   .   .   .   .
[] 执行 reduce 操作




                  .   .   .   .   .   .
[max] @list 得到 @list 中最大者




                     .      .   .   .   .   .
Capture 允许将变量封装为可修改的引用




                .   .   .   .   .   .
我们跳过与 perl5 一致的部分




              .     .   .   .   .   .
匿名函数
示例效果其实挺渣的
 我们看语法吧




            .   .   .   .   .   .
u s e v6 ;

my $da nce = ’ ’ ;
my %moves =
  hands−o v e r −head => sub {
     $ dance ˜= ’ / o  ’
  },




                                 .   .   .   .   .   .
b i r d −arms         => sub {
    $ dance ˜= ’ | / o  | ’
},




                                 .   .   .   .   .   .
left              => sub {
   $ dance ˜= ’>o ’
},




                             .   .   .   .   .   .
right              => sub {
   $ dance ˜= ’ o< ’
},




                              .   .   .   .   .   .
arms−up              => sub {
   $ dance ˜= ’  o / ’
};




                                .   .   .   .   .   .
my @awesome−dance = <arms−up
                              b i r d −arms
                              right
                              hands−o v e r −head >;
f o r @awesome−danc e −> $move {
      %moves {$move } . ( ) ;
}
s a y $ dance ;




                                          .    .   .   .   .   .
Signatures




             带参数的也可以




                       .   .   .   .   .   .
基本的




 sub o r d e r −b e e r ( $name ) {
     s a y ” H e l l o , $name ” ;
 }




                                      .   .   .   .   .   .
读写限定




 sub make−i t −more−s o ( $ i t i s rw ) {
 $ i t ˜= s u b s t r ( $ i t , $ i t . c h a r s − 1 ) x 5 ;
 }
 my $happy = ” yay ! ” ;
 make−i t −more−s o ( $happy ) ;




                                                  .    .        .   .   .   .
s a y $happy ; # yay ! ! ! ! ! !
# F a i l s ; ’cant modify a constant
make−i t −more−s o ( ” uh−oh ” ) ;




                                   .    .   .   .   .   .
复制




 sub say −i t −one−h i g h e r ( $ i t i s copy ) {
     $ i t ++;
     say $ i t ;
 }




                                            .    .    .   .   .   .
my $ u n a n s w e r = 4 1 ;
say −i t −one−h i g h e r ( $ u n a n s w e r ) ; # 42
say −i t −one−h i g h e r ( 4 1 ) ; # 42




                                                 .       .   .   .   .   .
HASH 和 ARRAY 参数也可以照常传递。




                 .   .   .   .   .   .
其实回调神马的也可以




 sub do−i t −l o t s (& i t , $how−many−t i m e s ) {
     f o r 1 . . $how−many−t i m e s {
           it ();
     }
 }




                                           .    .   .   .   .   .
do−i t −l o t s ( sub {
                       say ” Eating a s tr o op w af e l ”
                  } , 10);




                                            .    .   .       .   .   .
还记得 Python 的函数参数隐式拆封吗?




                         .   .   .   .   .   .
Perl 6 也可以




             .   .   .   .   .   .
Interpolating Arrays and Hashes




                          .       .   .   .   .   .
my %u s e r −p r e f e r e n c e s =
        ( from => ’ N o r t h e r n ’Sea ) ;
o r d e r −s h r i m p s ( 3 ,
               |% u s e r −p r e f e r e n c e s )




                                                .    .   .   .   .   .
参数默认值




        .   .   .   .   .   .
sub o r d e r −s t e a k ( $how = ’’medium ) {
s a y ”’ I d l i k e a s t e a k , $how ” ;
}
o r d e r −s t e a k ( ) ;
o r d e r −s t e a k ’ ( w e l l ’done ) ;




                                        .    .   .   .   .   .
或者可以给个空




          .   .   .   .   .   .
sub o r d e r −b u r g e r ( $ t y p e , $ s i d e ? ) {
    s a y ”’ I d l i k e a $ t y p e b u r g e r ” ˜
            ( d e f i n e d ( $ s i d e ) ??
               ” w i t h a s i d e o f $ s i d e ” ! ! ”” ) ;
}




                                             .    .    .    .   .   .
o r d e r −b u r g e r ( ” t r i p l e bacon ” ,
              ” deep f r i e d o n i o n r i n g s ” ) ;




                                                   .       .   .   .   .   .
按参数名传递参数




           .   .   .   .   .   .
sub o r d e r −b e e r ( $ t y p e , $ p i n t s ) {
s a y ( $ p i n t s == 1 ??’
       A ’pint !!
        ” $ p i n t s p i n t s ”) ˜ ” of $type , p l e a s e . ”
}




                                               .    .    .    .     .   .
o r d e r −b e e r ( t y p e => ’’ H o b g o b l i n ,
                     p i n t s => 1 ) ;
# A p i n t of Hobgoblin , p l e a s e .




                                                    .    .   .   .   .   .
o r d e r −b e e r ( p i n t s => 3 ,
                     t y p e => ’ Z l a t ý ž’Baant ) ;
# 3 p i n t s o f Z l a t ý žBaant , p l e a s e .




                                                .    .    .   .   .   .
可以要求一定讲对名字




         .   .   .   .   .   .
sub o r d e r −s h r i m p s ( $count ,
      : $from = ’North ’Sea ) {
s a y ”’ I d l i k e $ c o u n t . . . $from . . . ” ;
}




                                               .    .    .   .   .   .
# t a k e s t h e d e f a u l t v a l u e ’North ’Sea
o r d e r −s h r i m p s ( 6 ) ;
o r d e r −s h r i m p s ( 4 , from => ’ A t l a n t i c ’Ocean ) ;




                                               .    .    .    .       .   .
# n o t a l l o w e d , : $from i s named o n l y
o r d e r −s h r i m p s ( 2 2 , ’ M e d i t e r r a n e a n ’Sea ) ;




                                                      .    .     .      .   .   .
或者就当不存在




          .   .   .   .   .   .
sub d e s i g n −i c e −cream−m i x t u r e (
             : $name ! ,
             $ b a s e = ’’ V a n i l l a ,
             @mixins ) {
    s a y ” C r e a t i n g a new r e c i p e named $name ! ”
}




                                           .    .   .    .      .   .
d e s i g n −i c e −cream−m i x t u r e (
        name => ’’ P l a i n ) ;




                                            .   .   .   .   .   .
# m i s s i n g $name
d e s i g n −i c e −cream−m i x t u r e (
         b a s e => ’ S t r a w b e r r y ’ c h i p ) ;




                                                     .    .   .   .   .   .
这里不讨论重命名参数,有兴趣的朋友可以自行查询




                  .   .   .   .   .   .
有隐式拆封就有隐式封装




          .   .   .   .   .   .
sub s h o u t −them ( ∗ @words ) {
    f o r @words −> $w {
            p r i n t uc ( ” $w ” ) ;
    }
}




                                        .   .   .   .   .   .
# now you can p a s s i t e m s
s h o u t −them’ ( ’go ) ; # GO
s h o u t −them’ ( ’go , ’’home ) ; # GO HOME




                                     .   .      .   .   .   .
my @words = ’ ( ’go , ’’home ) ;
s h o u t −them ( @words ) ; # s t i l l w o r k s




                                               .     .   .   .   .   .
山寨一个修饰模式




           .   .   .   .   .   .
sub debug−w r a p p e r (& code ,
                             ∗ @positional ,
                             ∗%named ) {
    warn ” C a l l i n g ’& code . name ( ) ’ w i t h a r g u m e n t s ”
         ˜ ” @ p o s i t i o n a l . p e r l ( ) , %named . p e r l ( )  n ” ;
    code ( | @ p o s i t i o n a l , |%named ) ;
    warn ” . . . back from ’&code . name’ ( )  n ” ;
}




                                                  .    .     .    .    .    .
debug−w r a p p e r (& o r d e r −s h r i m p s ,
                     4,
                     from => ’ A t l a n t i c ’Ocean ) ;




                                           .    .   .       .   .   .
还记得 Python 的 tuple 隐式拆装吗?




                            .   .   .   .   .   .
sub menu {
     i f r a n d < 0 . 5 {’’
           f i s h , ’white ’wine
     } e l s e {’’
           steak , ’red ’wine ;
     }
}
my ( $food , $ b e v e r a g e ) = menu ( ) ;




                                                .   .   .   .   .   .
还可以指定类型




          .   .   .   .   .   .
sub mean ( Numeric $a , Numeric $b ) {
    r e t u r n ( $a + $b ) / 2 ;
}




                                   .     .   .   .   .   .
正确!结果为 2。




 say mean 2.5, 1.5;




                      .   .   .   .   .   .
乱来什么的,讨厌了!




       say mean ”some”, ”strings”;




                               .     .   .   .   .   .
Nominal type check failed for parameter ”$a”; expected Numeric
but got Str instead




                                          .    .   .    .   .    .
还记得 Haskell 的参数匹配吗?




                .   .   .   .   .   .
sub c i r c l e −r a d i u s −from−a r e a (
          Numeric $ a r e a
                 where { $ a r e a >= 0 } ) {
    ( $area / pi ) . sqrt
}




                                         .      .   .   .   .   .
s a y c i r c l e −r a d i u s −from−a r e a ( 3 ) ; # OK
s a y c i r c l e −r a d i u s −from−a r e a ( −3); # E r r o r




                                                 .    .    .      .   .   .
只接受限定值




         .   .   .   .   .   .
sub s e t −volume ( Numeric $volume where 0 . . 1 1 ) {
    s a y ” T u r n i n g i t up t o $volume ” ;
}




                                      .   .   .   .   .   .
可以使用 hash 约束参数




             .   .   .   .   .   .
Currying !




             .   .   .   .   .   .
原型




 sub o r d e r −b u r g e r ( $ t y p e , $ s i d e ? ) { . . . } ;




                                                  .    .    .    .    .   .
assuming




  &o r d e r −the −u s u a l :=
        &o r d e r −b u r g e r . a s s u m i n g ( ’’ v e g g i e ) ;
  &o r d e r −b u r g e r −and− f r i e s :=
        &o r d e r −b u r g e r . a s s u m i n g (
              s i d e => ’ f r e n c h ’ f r i e s ) ;




                                                       .    .     .      .   .   .
USAGE




  o r d e r −the −u s u a l ( ’’ s a l s a ) ;
  o r d e r −the −u s u a l ( s i d e => ’’ b r o c c o l i ) ;
  o r d e r −b u r g e r −and− f r i e s ( ’’ p l a i n ) ;
  o r d e r −b u r g e r −and− f r i e s ( : t y p e <<d o u b l e −b e e f >> ) ;




                                                       .     .    .     .    .       .
函数自省




       .   .   .   .   .   .
定一个函数




        .   .   .   .   .   .
sub l o g a r i t h m ( Numeric $x , Numeric : $ b a s e = exp ( 1 ) ) {
    l o g ( $x ) / l o g ( $ b a s e ) ;
}




                                           .   .    .   .    .    .
得到参数




       .   .   .   .   .   .
my @params = &l o g a r i t h m . s i g n a t u r e . params ;
s a y @params . elems , ’’ p a r a m e t e r s ;




                                               .    .    .       .   .   .
得到参数的详细信息




            .   .   .   .   .   .
f o r @params {
      s a y ”Name : ” , . name ;
      s a y ” Type : ” , . t y p e ;
      s a y ” named ? ” , . named ?? ’’ y e s ! ! ’’no ;
      s a y ” s l u r p y ? ” , . s l u r p y ?? ’’y e s ! ! ’’no ;
      s a y ” o p t i o n a l ? ” , . o p t i o n a l ?? ’’y e s ! ! ’
’no ;
}




                                             .    .    .    .   .    .
各种 to-json




             .   .   .   .   .   .
m u l t i to−j s o n ( R e a l $d ) { ˜ $d }




                                               .   .   .   .   .   .
m u l t i to−j s o n ( Bool $d ) { $d ?? ’’t r u e ! ! ’’f a l s e ;




                                         .    .   .    .   .   .
m u l t i to−j s o n ( S t r $d ) {
        ’” ’
       ˜ $d . t r a n s ( [ ’ ” ’ ,
                            ’ ’ ,
                            ” b ” ,
                            ” f ” ,
                            ” n ” ,
                            ” r ” ,
                            ” t ” ]




                                       .   .   .   .   .   .
=> [ ’  ” ’ ,
             ’ ’ ,
             ’b ’ ,
             ’ f ’ ,
             ’n ’ ,
             ’ r ’ ,
             ’ t ’ ] )
    ˜ ’” ’
}




                          .   .   .   .   .   .
m u l t i to−j s o n ( A r r a y $d ) {
        return ’[ ’
            ˜ $d . v a l u e s . map ( {
                       to−j s o n ( $ )
                }). j o i n ( ’ , ’)
            ˜ ’ ] ’;
}




                                           .   .   .   .   .   .
m u l t i to−j s o n ( Hash $d ) {
        r e t u r n ’{ ’
                ˜ $d . p a i r s . map ( { to−j s o n ( . k e y )
                ˜ ’ : ’
                ˜ to−j s o n ( . v a l u e ) } ) . j o i n ( ’ , ’ )
                ˜ ’ } ’;
}




                                                      .     .     .    .   .   .
m u l t i to−j s o n ( $d
             where { ! d e f i n e d $d } ) {
        ’ null ’
}




                                                .   .   .   .   .   .
m u l t i to−j s o n ( $d ) {
        d i e ”’Cant s e r i a l i z e an o b j e c t o f t y p e ”
              ˜ $d .WHAT. p e r l
}




                                               .   .    .    .    .   .
Perl 6 增强了面向对象语法,使之更合理,更易写易读。




                   .   .   .   .   .   .
下面看一个比较完整的示例




          .    .   .   .   .   .
比 P5 更好读的结构




 c l a s s Task {
          ....
 }




                    .   .   .   .   .   .
属性使用 has 标示




              .   .   .   .   .   .
c l a s s Task {
        h a s &! c a l l b a c k ;
        h a s Task @ ! d e p e n d e n c i e s ;
        h a s Bool $ . done ;
...




                                                   .   .   .   .   .   .
! 操作符表示私有限定

h a s &! c a l l b a c k ;
h a s Task @ ! d e p e n d e n c i e s ;




                                           .   .   .   .   .   .
私有变量与访问器




           .   .   .   .   .   .
h a s Bool $ ! done ;
method done ( ) {
      r e t u r n $ ! done
}




                             .   .   .   .   .   .
. 标示自动封装一个只读访问器




            .   .   .   .   .   .
h a s Bool $ . done ;




                        .   .   .   .   .   .
可以明确标示可读写




            .   .   .   .   .   .
h a s Bool $ . done i s rw ;




                               .   .   .   .   .   .
普通的成员方法




          .   .   .   .   .   .
method add−d e p e n d e n c y ( Task $ d e p e n d e n c y ) {
    push @ ! d e p e n d e n c i e s , $ d e p e n d e n c y ;
}




                                                .    .    .       .   .   .
构造函数




       .   .   .   .   .   .
# RAKUDO: S h o u l d r e a l l y be
# ’& c a l l b a c k ’ [ p e r l #69766]
method new ( C a l l a b l e $ c a l l b a c k ,
                    Task ∗ @ d e p e n d e n c i e s ) {
     return s e l f . b l e s s (∗ ,
                                    : $callback ,
                                    : @dependencies ) ;
}




                                              .    .       .   .   .   .
使用示范




       .   .   .   .   .   .
my $ e a t =
    Task . new ( { s a y ’ e a t i n g d i n n e r . NOM’ ! } ,
           Task . new ( { s a y ’making ’ d i n n e r } ,
           ...
           )
     );




                                            .    .    .   .       .   .
模式匹配——regex




              .   .   .   .   .   .
If you know regexes from other programming languages it’’s best
to forget their syntax; Perl 6 differs from PCRE or POSIX regexes.
by Perl 6 book




                                           .    .   .    .    .     .
最基本的语法没有大变化




          .   .   .   .   .   .
Note: Perl6 智能匹配符 ˜˜




                  .    .   .   .   .   .
f o r $ ∗ IN . l i n e s {
      . s a y i f m/who / ;
};




                              .   .   .   .   .   .
表达式内插




        .   .   .   .   .   .
这明明是个半成品啊喂˜




 my t o k e n word { w+ [  ’ w+]? } ;
 my r e g e x dup { <word=&word>
                    W $<word> } ;
                      +




                                      .    .   .   .   .   .
f o r $ ∗ IN . l i n e s −> $ l i n e {
      i f $ l i n e ˜˜ m/ <dup=&dup> / {
             s a y ” Found ’ { $<dup><word >}’
                     t w i c e i n ” $ l i n e  ” ” ;
      }
}




                                                 .    .   .   .   .   .
让它去读诗,结果杯具了……




           .    .   .   .   .   .
《失乐园》




        .   .   .   .   .   .
Of Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
...




                                   .   .   .     .   .   .
发生了什么事?




 Found 't' twice in "Sing Heav'nly ...
 Found 't' twice in "That Shepherd, ...
 Found 't' twice in "Delight thee more,...
 ...




                                    .   .    .   .   .   .
刚才看到了什么?




           .   .   .   .   .   .
my t o k e n word { w+ [  ’ w+]? } ;




                                     .    .   .   .   .   .
可以用于 regex 或 gammar 的表达式片段




                   .   .   .   .   .   .
Gammar ˜ Rules ˜ Regex ˜ Token




                         .   .   .   .   .   .
递归下降




       .   .   .   .   .   .
语法解析




       .   .   .   .   .   .
现在,可以用 Perl 语言直接书写语法分析




                .   .   .   .   .   .
前提! !
   !




        如果这个坑填了……




                    .   .   .   .   .   .
看一个理想的目标
rakudo-2010.06/t/spec/S05-match/perl.t




                             .   .       .   .   .   .
grammar ExprT1 {
    r u l e TOP { ˆ d+ [ <o p e r a t o r > d+ ] ∗ }
    t o k e n o p e r a t o r { ’ / ’ | ’ ∗ ’ | ’+ ’ | ’ − ’ } ;
};




                                            .    .    .   .    .   .
my r e g e x f { f } ;
my r e g e x o { o } ;
ok ” f o o ” ˜˜ /< f=&f > <o=&o>+ / ,
         ’ Regex matches ( 2 ) ’ ;
l i v e s o k { $ /. perl } , ’ l i v e s . . .




                                              .   .   .   .   .   .
谢谢大家!




        .   .   .   .   .   .

Perl 6 news at 2010-06

  • 1.
    Perl 6 前沿战报 刘鑫<march.liu@gmail.com> 金山软件 Kingsoft July 17, 2010 . . . . . .
  • 2.
  • 3.
    A Joke AboutThe King Of Scripts Perl 是一门易学易用的语言。 ——Larry Wall《Perl 语言编程》 . . . . . .
  • 4.
  • 5.
  • 6.
    十年的努力 2001 ˜ 2010 . . . . . .
  • 7.
    Parrot ˜ Pugs˜ Parrot ˜ Rakudo . . . . . .
  • 8.
    2010-06-30 是的!祂来了! Parrot 2.5.0 Rakudo 2010-06 . . . . . .
  • 9.
    You can doit. say ” h e l l o ”; . . . . . .
  • 10.
    Or you cando it. say (” h e l l o ” ) ; . . . . . .
  • 11.
    And you cando it yet. ” h e l l o ”. say ( ) ; . . . . . .
  • 12.
    Of cause youcan do it. ” h e l l o ”. say ; . . . . . .
  • 13.
    example: grep inlines #!/ u s r / b i n / env p e r l 6 u s e v6 ; my $exp = @∗ARGS [ 0 ] ; #new s t y l e l o o p and i f f o r $ ∗ IN . l i n e s −> $ l i n e { i f $ l i n e ˜˜ m/ $exp / { say $ l i n e ; } } . . . . . .
  • 14.
    Shorter is better #!/ u s r / b i n / env p e r l 6 u s e v6 ; my $exp = @∗ARGS [ 0 ] ; f o r $ ∗ IN . l i n e s −> $ l i n e { s a y $ l i n e i f $ l i n e ˜˜ m/ $exp / ; } . . . . . .
  • 15.
  • 16.
    g i ve n ( $x ) { when 5 { s a y ’ $x i s t h e number 5 ’ ; } when ” abc ” { s a y ’ $x i s t h e s t r i n g ” abc ” ’ ; } when Bool { s a y ’ $x i s t h e b o o l e a n q u a n t i t y ’ ˜ $x ; } } . . . . . .
  • 17.
    还记得 Perl 5中的 if/unless 表达式吗? . . . . . .
  • 18.
  • 19.
  • 20.
    method p er f o r m ( ) { u n l e s s $ ! done { . perform ( ) f o r @! dependencies ; &! c a l l b a c k ( ) ; $ ! done = True ; } } . . . . . .
  • 21.
    C 风格循环 . . . . . .
  • 22.
    l o op (my $ i = 0 ; $ i <= 5 ; $ i ++) { print $i ; # ”12345” } . . . . . .
  • 23.
    my $ i= 0 ; l o o p ( ; $ i <= 5 ; $ i ++) { print $i ; } . . . . . .
  • 24.
    # ”12345” l oo p (my $ i = 0 ; $ i <= 5 ; ) { print $i ; $ i ++; } . . . . . .
  • 25.
    l o op (my $ i = 0 ; ; $ i ++) { l a s t u n l e s s ( $ i <= 5 ) ; print $i ; } . . . . . .
  • 26.
    my $ i= 0 ; loop ( ; ; ) { l a s t u n l e s s ( $ i <= 5 ) ; print $i ; $ i ++; } . . . . . .
  • 27.
    支持 === ,即eqv 操作符 . . . . . .
  • 28.
    支持三值比较运算符 <=> ,即leg 操作符 . . . . . .
  • 29.
    惰性.. us e v6 ; my @l = 1 . . 1 0 ; s a y @l ; . . . . . .
  • 30.
    围观 u se v6 ; my %s c o r e s = Ana => 8 , Dave => 6 , C h a r l i e => 4 , Beth => 4 ; . . . . . .
  • 31.
    my $ sc r e e n w i d t h = 3 0 ; my $ l a b e l a r e a w i d t h = 1 + [ max ] %s c o r e s » . k e y » . c h a r s ; my $ m a x s c o r e = [ max ] %s c o r e s » . v a l u e ; my $ u n i t = ( $ s c r e e n w i d t h − $ l a b e l a r e a w i d t h ) / $max score ; . . . . . .
  • 32.
    say $ la b e l a r e a w i d t h ; f o r %s c o r e s { my $ f o r m a t = ’%− ’ ˜ $ l a b e l a r e a w i d t h ˜ ” s%s n p r i n t f $ f o r m a t , . key , ’X ’ x ( $ u n i t ∗ . v a l u e ) ; } . . . . . .
  • 33.
    Foreach 操作符 . . . . . .
  • 34.
    %s c or e s >>.k e y ; . . . . . .
  • 35.
    相当于 . . . . . .
  • 36.
  • 37.
    Reduce 操作符 . . . . . .
  • 38.
    my $label_area_width =1 + [max] %scores».key».chars; . . . . . .
  • 39.
    max(a, b) 得到a 和 b 中较大的一个 . . . . . .
  • 40.
    [] 执行 reduce操作 . . . . . .
  • 41.
    [max] @list 得到@list 中最大者 . . . . . .
  • 42.
  • 43.
  • 44.
  • 45.
    u s ev6 ; my $da nce = ’ ’ ; my %moves = hands−o v e r −head => sub { $ dance ˜= ’ / o ’ }, . . . . . .
  • 46.
    b i rd −arms => sub { $ dance ˜= ’ | / o | ’ }, . . . . . .
  • 47.
    left => sub { $ dance ˜= ’>o ’ }, . . . . . .
  • 48.
    right => sub { $ dance ˜= ’ o< ’ }, . . . . . .
  • 49.
    arms−up => sub { $ dance ˜= ’ o / ’ }; . . . . . .
  • 50.
    my @awesome−dance =<arms−up b i r d −arms right hands−o v e r −head >; f o r @awesome−danc e −> $move { %moves {$move } . ( ) ; } s a y $ dance ; . . . . . .
  • 51.
    Signatures 带参数的也可以 . . . . . .
  • 52.
    基本的 sub or d e r −b e e r ( $name ) { s a y ” H e l l o , $name ” ; } . . . . . .
  • 53.
    读写限定 sub make−it −more−s o ( $ i t i s rw ) { $ i t ˜= s u b s t r ( $ i t , $ i t . c h a r s − 1 ) x 5 ; } my $happy = ” yay ! ” ; make−i t −more−s o ( $happy ) ; . . . . . .
  • 54.
    s a y$happy ; # yay ! ! ! ! ! ! # F a i l s ; ’cant modify a constant make−i t −more−s o ( ” uh−oh ” ) ; . . . . . .
  • 55.
    复制 sub say−i t −one−h i g h e r ( $ i t i s copy ) { $ i t ++; say $ i t ; } . . . . . .
  • 56.
    my $ un a n s w e r = 4 1 ; say −i t −one−h i g h e r ( $ u n a n s w e r ) ; # 42 say −i t −one−h i g h e r ( 4 1 ) ; # 42 . . . . . .
  • 57.
    HASH 和 ARRAY参数也可以照常传递。 . . . . . .
  • 58.
    其实回调神马的也可以 sub do−it −l o t s (& i t , $how−many−t i m e s ) { f o r 1 . . $how−many−t i m e s { it (); } } . . . . . .
  • 59.
    do−i t −lo t s ( sub { say ” Eating a s tr o op w af e l ” } , 10); . . . . . .
  • 60.
  • 61.
    Perl 6 也可以 . . . . . .
  • 62.
    Interpolating Arrays andHashes . . . . . .
  • 63.
    my %u se r −p r e f e r e n c e s = ( from => ’ N o r t h e r n ’Sea ) ; o r d e r −s h r i m p s ( 3 , |% u s e r −p r e f e r e n c e s ) . . . . . .
  • 64.
    参数默认值 . . . . . .
  • 65.
    sub o rd e r −s t e a k ( $how = ’’medium ) { s a y ”’ I d l i k e a s t e a k , $how ” ; } o r d e r −s t e a k ( ) ; o r d e r −s t e a k ’ ( w e l l ’done ) ; . . . . . .
  • 66.
  • 67.
    sub o rd e r −b u r g e r ( $ t y p e , $ s i d e ? ) { s a y ”’ I d l i k e a $ t y p e b u r g e r ” ˜ ( d e f i n e d ( $ s i d e ) ?? ” w i t h a s i d e o f $ s i d e ” ! ! ”” ) ; } . . . . . .
  • 68.
    o r de r −b u r g e r ( ” t r i p l e bacon ” , ” deep f r i e d o n i o n r i n g s ” ) ; . . . . . .
  • 69.
  • 70.
    sub o rd e r −b e e r ( $ t y p e , $ p i n t s ) { s a y ( $ p i n t s == 1 ??’ A ’pint !! ” $ p i n t s p i n t s ”) ˜ ” of $type , p l e a s e . ” } . . . . . .
  • 71.
    o r de r −b e e r ( t y p e => ’’ H o b g o b l i n , p i n t s => 1 ) ; # A p i n t of Hobgoblin , p l e a s e . . . . . . .
  • 72.
    o r de r −b e e r ( p i n t s => 3 , t y p e => ’ Z l a t ý ž’Baant ) ; # 3 p i n t s o f Z l a t ý žBaant , p l e a s e . . . . . . .
  • 73.
  • 74.
    sub o rd e r −s h r i m p s ( $count , : $from = ’North ’Sea ) { s a y ”’ I d l i k e $ c o u n t . . . $from . . . ” ; } . . . . . .
  • 75.
    # t ak e s t h e d e f a u l t v a l u e ’North ’Sea o r d e r −s h r i m p s ( 6 ) ; o r d e r −s h r i m p s ( 4 , from => ’ A t l a n t i c ’Ocean ) ; . . . . . .
  • 76.
    # n ot a l l o w e d , : $from i s named o n l y o r d e r −s h r i m p s ( 2 2 , ’ M e d i t e r r a n e a n ’Sea ) ; . . . . . .
  • 77.
  • 78.
    sub d es i g n −i c e −cream−m i x t u r e ( : $name ! , $ b a s e = ’’ V a n i l l a , @mixins ) { s a y ” C r e a t i n g a new r e c i p e named $name ! ” } . . . . . .
  • 79.
    d e si g n −i c e −cream−m i x t u r e ( name => ’’ P l a i n ) ; . . . . . .
  • 80.
    # m is s i n g $name d e s i g n −i c e −cream−m i x t u r e ( b a s e => ’ S t r a w b e r r y ’ c h i p ) ; . . . . . .
  • 81.
  • 82.
  • 83.
    sub s ho u t −them ( ∗ @words ) { f o r @words −> $w { p r i n t uc ( ” $w ” ) ; } } . . . . . .
  • 84.
    # now youcan p a s s i t e m s s h o u t −them’ ( ’go ) ; # GO s h o u t −them’ ( ’go , ’’home ) ; # GO HOME . . . . . .
  • 85.
    my @words =’ ( ’go , ’’home ) ; s h o u t −them ( @words ) ; # s t i l l w o r k s . . . . . .
  • 86.
  • 87.
    sub debug−w ra p p e r (& code , ∗ @positional , ∗%named ) { warn ” C a l l i n g ’& code . name ( ) ’ w i t h a r g u m e n t s ” ˜ ” @ p o s i t i o n a l . p e r l ( ) , %named . p e r l ( ) n ” ; code ( | @ p o s i t i o n a l , |%named ) ; warn ” . . . back from ’&code . name’ ( ) n ” ; } . . . . . .
  • 88.
    debug−w r ap p e r (& o r d e r −s h r i m p s , 4, from => ’ A t l a n t i c ’Ocean ) ; . . . . . .
  • 89.
    还记得 Python 的tuple 隐式拆装吗? . . . . . .
  • 90.
    sub menu { i f r a n d < 0 . 5 {’’ f i s h , ’white ’wine } e l s e {’’ steak , ’red ’wine ; } } my ( $food , $ b e v e r a g e ) = menu ( ) ; . . . . . .
  • 91.
  • 92.
    sub mean (Numeric $a , Numeric $b ) { r e t u r n ( $a + $b ) / 2 ; } . . . . . .
  • 93.
    正确!结果为 2。 saymean 2.5, 1.5; . . . . . .
  • 94.
    乱来什么的,讨厌了! say mean ”some”, ”strings”; . . . . . .
  • 95.
    Nominal type checkfailed for parameter ”$a”; expected Numeric but got Str instead . . . . . .
  • 96.
  • 97.
    sub c ir c l e −r a d i u s −from−a r e a ( Numeric $ a r e a where { $ a r e a >= 0 } ) { ( $area / pi ) . sqrt } . . . . . .
  • 98.
    s a yc i r c l e −r a d i u s −from−a r e a ( 3 ) ; # OK s a y c i r c l e −r a d i u s −from−a r e a ( −3); # E r r o r . . . . . .
  • 99.
  • 100.
    sub s et −volume ( Numeric $volume where 0 . . 1 1 ) { s a y ” T u r n i n g i t up t o $volume ” ; } . . . . . .
  • 101.
  • 102.
    Currying ! . . . . . .
  • 103.
    原型 sub or d e r −b u r g e r ( $ t y p e , $ s i d e ? ) { . . . } ; . . . . . .
  • 104.
    assuming &or d e r −the −u s u a l := &o r d e r −b u r g e r . a s s u m i n g ( ’’ v e g g i e ) ; &o r d e r −b u r g e r −and− f r i e s := &o r d e r −b u r g e r . a s s u m i n g ( s i d e => ’ f r e n c h ’ f r i e s ) ; . . . . . .
  • 105.
    USAGE or d e r −the −u s u a l ( ’’ s a l s a ) ; o r d e r −the −u s u a l ( s i d e => ’’ b r o c c o l i ) ; o r d e r −b u r g e r −and− f r i e s ( ’’ p l a i n ) ; o r d e r −b u r g e r −and− f r i e s ( : t y p e <<d o u b l e −b e e f >> ) ; . . . . . .
  • 106.
    函数自省 . . . . . .
  • 107.
    定一个函数 . . . . . .
  • 108.
    sub l og a r i t h m ( Numeric $x , Numeric : $ b a s e = exp ( 1 ) ) { l o g ( $x ) / l o g ( $ b a s e ) ; } . . . . . .
  • 109.
    得到参数 . . . . . .
  • 110.
    my @params =&l o g a r i t h m . s i g n a t u r e . params ; s a y @params . elems , ’’ p a r a m e t e r s ; . . . . . .
  • 111.
  • 112.
    f o r@params { s a y ”Name : ” , . name ; s a y ” Type : ” , . t y p e ; s a y ” named ? ” , . named ?? ’’ y e s ! ! ’’no ; s a y ” s l u r p y ? ” , . s l u r p y ?? ’’y e s ! ! ’’no ; s a y ” o p t i o n a l ? ” , . o p t i o n a l ?? ’’y e s ! ! ’ ’no ; } . . . . . .
  • 113.
    各种 to-json . . . . . .
  • 114.
    m u lt i to−j s o n ( R e a l $d ) { ˜ $d } . . . . . .
  • 115.
    m u lt i to−j s o n ( Bool $d ) { $d ?? ’’t r u e ! ! ’’f a l s e ; . . . . . .
  • 116.
    m u lt i to−j s o n ( S t r $d ) { ’” ’ ˜ $d . t r a n s ( [ ’ ” ’ , ’ ’ , ” b ” , ” f ” , ” n ” , ” r ” , ” t ” ] . . . . . .
  • 117.
    => [ ’ ” ’ , ’ ’ , ’b ’ , ’ f ’ , ’n ’ , ’ r ’ , ’ t ’ ] ) ˜ ’” ’ } . . . . . .
  • 118.
    m u lt i to−j s o n ( A r r a y $d ) { return ’[ ’ ˜ $d . v a l u e s . map ( { to−j s o n ( $ ) }). j o i n ( ’ , ’) ˜ ’ ] ’; } . . . . . .
  • 119.
    m u lt i to−j s o n ( Hash $d ) { r e t u r n ’{ ’ ˜ $d . p a i r s . map ( { to−j s o n ( . k e y ) ˜ ’ : ’ ˜ to−j s o n ( . v a l u e ) } ) . j o i n ( ’ , ’ ) ˜ ’ } ’; } . . . . . .
  • 120.
    m u lt i to−j s o n ( $d where { ! d e f i n e d $d } ) { ’ null ’ } . . . . . .
  • 121.
    m u lt i to−j s o n ( $d ) { d i e ”’Cant s e r i a l i z e an o b j e c t o f t y p e ” ˜ $d .WHAT. p e r l } . . . . . .
  • 122.
  • 123.
  • 124.
    比 P5 更好读的结构 c l a s s Task { .... } . . . . . .
  • 125.
  • 126.
    c l as s Task { h a s &! c a l l b a c k ; h a s Task @ ! d e p e n d e n c i e s ; h a s Bool $ . done ; ... . . . . . .
  • 127.
    ! 操作符表示私有限定 h as &! c a l l b a c k ; h a s Task @ ! d e p e n d e n c i e s ; . . . . . .
  • 128.
  • 129.
    h a sBool $ ! done ; method done ( ) { r e t u r n $ ! done } . . . . . .
  • 130.
  • 131.
    h a sBool $ . done ; . . . . . .
  • 132.
  • 133.
    h a sBool $ . done i s rw ; . . . . . .
  • 134.
  • 135.
    method add−d ep e n d e n c y ( Task $ d e p e n d e n c y ) { push @ ! d e p e n d e n c i e s , $ d e p e n d e n c y ; } . . . . . .
  • 136.
    构造函数 . . . . . .
  • 137.
    # RAKUDO: Sh o u l d r e a l l y be # ’& c a l l b a c k ’ [ p e r l #69766] method new ( C a l l a b l e $ c a l l b a c k , Task ∗ @ d e p e n d e n c i e s ) { return s e l f . b l e s s (∗ , : $callback , : @dependencies ) ; } . . . . . .
  • 138.
    使用示范 . . . . . .
  • 139.
    my $ ea t = Task . new ( { s a y ’ e a t i n g d i n n e r . NOM’ ! } , Task . new ( { s a y ’making ’ d i n n e r } , ... ) ); . . . . . .
  • 140.
  • 141.
    If you knowregexes from other programming languages it’’s best to forget their syntax; Perl 6 differs from PCRE or POSIX regexes. by Perl 6 book . . . . . .
  • 142.
  • 143.
  • 144.
    f o r$ ∗ IN . l i n e s { . s a y i f m/who / ; }; . . . . . .
  • 145.
    表达式内插 . . . . . .
  • 146.
    这明明是个半成品啊喂˜ my to k e n word { w+ [ ’ w+]? } ; my r e g e x dup { <word=&word> W $<word> } ; + . . . . . .
  • 147.
    f o r$ ∗ IN . l i n e s −> $ l i n e { i f $ l i n e ˜˜ m/ <dup=&dup> / { s a y ” Found ’ { $<dup><word >}’ t w i c e i n ” $ l i n e ” ” ; } } . . . . . .
  • 148.
  • 149.
    《失乐园》 . . . . . .
  • 150.
    Of Mans FirstDisobedience, and the Fruit Of that Forbidden Tree, whose mortal tast Brought Death into the World, and all our woe, ... . . . . . .
  • 151.
    发生了什么事? Found 't'twice in "Sing Heav'nly ... Found 't' twice in "That Shepherd, ... Found 't' twice in "Delight thee more,... ... . . . . . .
  • 152.
  • 153.
    my t ok e n word { w+ [ ’ w+]? } ; . . . . . .
  • 154.
    可以用于 regex 或gammar 的表达式片段 . . . . . .
  • 155.
    Gammar ˜ Rules˜ Regex ˜ Token . . . . . .
  • 156.
    递归下降 . . . . . .
  • 157.
    语法解析 . . . . . .
  • 158.
  • 159.
    前提! ! ! 如果这个坑填了…… . . . . . .
  • 160.
  • 161.
    grammar ExprT1 { r u l e TOP { ˆ d+ [ <o p e r a t o r > d+ ] ∗ } t o k e n o p e r a t o r { ’ / ’ | ’ ∗ ’ | ’+ ’ | ’ − ’ } ; }; . . . . . .
  • 162.
    my r eg e x f { f } ; my r e g e x o { o } ; ok ” f o o ” ˜˜ /< f=&f > <o=&o>+ / , ’ Regex matches ( 2 ) ’ ; l i v e s o k { $ /. perl } , ’ l i v e s . . . . . . . . .
  • 163.
    谢谢大家! . . . . . .