Upload
march-liu
View
1.462
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
. . . . . .
从创世纪到新的创世纪
. . . . . .
A Joke About The King Of Scripts
Perl 是一门易学易用的语言。
——Larry Wall《Perl 语言编程》
. . . . . .
1987 年发布第一版
. . . . . .
2001 年愚人节的笑话Python? Perl? All in one!
. . . . . .
十年的努力 2001 2̃010
. . . . . .
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
use v6 ;
my $exp = @∗ARGS [ 0 ] ;#new s t y l e l oop and i ff 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
use v6 ;
my $exp = @∗ARGS [ 0 ] ;f o r $∗ IN . l i n e s −> $ l i n e {
say $ l i n e i f $ l i n e ˜˜ m/ $exp / ;}
. . . . . .
正式的多路选择分支支持——given/when
. . . . . .
g i v en ( $x ) {when 5 {
say ’ $x i s the number 5 ’ ;}when ” abc ” {
say ’ $x i s the s t r i n g ” abc ” ’ ;}when Bool {
say ’ $x i s the boo l ean q u a n t i t y ’ ˜ $x ;}
}
. . . . . .
还记得 Perl 5 中的 if/unless 表达式吗?
. . . . . .
现在 for 也可以。
. . . . . .
下面这个对象方法中使用了 for 表达式
. . . . . .
method per fo rm ( ) {u n l e s s $ ! done {
. pe r fo rm ( ) f o r @! dependenc i e s ;&! c a l l b a c k ( ) ;$ ! done = True ;
}}
. . . . . .
C 风格循环
. . . . . .
l oop (my $ i = 0 ; $ i <= 5 ; $ i++) {p r i n t $ i ; # ”12345”
}
. . . . . .
my $ i = 0 ;l oop ( ; $ i <= 5 ; $ i++) {
p r i n t $ i ;}
. . . . . .
# ”12345”loop (my $ i = 0 ; $ i <= 5 ; ) {
p r i n t $ i ;$ i ++;
}
. . . . . .
l oop (my $ i = 0 ; ; $ i++) {l a s t u n l e s s ( $ i <= 5 ) ;p r i n t $ i ;
}
. . . . . .
my $ i = 0 ;l oop ( ; ; ) {
l a s t u n l e s s ( $ i <= 5 ) ;p r i n t $ i ;$ i ++;
}
. . . . . .
支持 === ,即 eqv 操作符
. . . . . .
支持三值比较运算符 <=> ,即 leg 操作符
. . . . . .
惰性..
use v6 ;
my @l = 1 . . 1 0 ;say @l ;
. . . . . .
围观
use 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 = 30 ;my $ l a b e l a r e a w i d t h =
1 + [ max ] %s c o r e s » . key» . cha r s ;my $max score = [ 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 $ fo rmat = ’%− ’ ˜ $ l a b e l a r e a w i d t h ˜ ” s%s \n ” ;p r i n t f $ format , . key ,
’X ’ x ( $ u n i t ∗ . v a l u e ) ;}
. . . . . .
Foreach 操作符
. . . . . .
%sco r e s >>.key ;
. . . . . .
相当于
. . . . . .
得到%source 每一个键值对的 key
. . . . . .
Reduce 操作符
. . . . . .
my $label_area_width = 1 +[max] %scores».key».chars;
. . . . . .
max(a, b) 得到 a 和 b 中较大的一个
. . . . . .
[] 执行 reduce 操作
. . . . . .
[max] @list 得到 @list 中最大者
. . . . . .
Capture 允许将变量封装为可修改的引用
. . . . . .
我们跳过与 perl5 一致的部分
. . . . . .
匿名函数示例效果其实挺渣的我们看语法吧
. . . . . .
use v6 ;
my $dance = ’ ’ ;my %moves =
hands−over−head => sub {$dance ˜= ’/ o\ ’
} ,
. . . . . .
b i rd−arms => sub {$dance ˜= ’ |/ o \ | ’
} ,
. . . . . .
l e f t => sub {$dance ˜= ’>o ’
} ,
. . . . . .
r i g h t => sub {$dance ˜= ’o< ’
} ,
. . . . . .
arms−up => sub {$dance ˜= ’\ o/ ’
} ;
. . . . . .
my @awesome−dance = <arms−upb i rd−armsr i g h thands−over−head >;
f o r @awesome−dance −> $move {%moves{$move } . ( ) ;
}say $dance ;
. . . . . .
Signatures
带参数的也可以
. . . . . .
基本的
sub orde r−bee r ( $name ) {say ” He l l o , $name ” ;
}
. . . . . .
读写限定
sub make−i t −more−so ( $ i t i s rw ) {$ i t ˜= s u b s t r ( $ i t , $ i t . c ha r s − 1) x 5 ;}my $happy = ” yay ! ” ;make−i t −more−so ( $happy ) ;
. . . . . .
say $happy ; # yay ! ! ! ! ! !# F a i l s ; ’cant modi fy a con s t an tmake−i t −more−so (” uh−oh ” ) ;
. . . . . .
复制
sub say−i t −one−h i g h e r ( $ i t i s copy ) {$ i t ++;say $ i t ;
}
. . . . . .
my $unanswer = 41 ;say−i t −one−h i g h e r ( $unanswer ) ; # 42say−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 imes ) {f o r 1 . . $how−many−t imes {
i t ( ) ;}
}
. . . . . .
do−i t − l o t s ( sub {say ” Eat i ng a s t r o o p w a f e l ”
} , 1 0 ) ;
. . . . . .
还记得 Python 的函数参数隐式拆封吗?
. . . . . .
Perl 6 也可以
. . . . . .
Interpolating Arrays and Hashes
. . . . . .
my %user−p r e f e r e n c e s =( from => ’Northern ’Sea ) ;
o rde r−sh r imps (3 ,|% user−p r e f e r e n c e s )
. . . . . .
参数默认值
. . . . . .
sub orde r−s t e ak ( $how = ’’medium ) {say ”’ I d l i k e a s teak , $how ” ;}orde r−s t e ak ( ) ;o rde r−s t e ak’( w e l l ’done ) ;
. . . . . .
或者可以给个空
. . . . . .
sub orde r−bu rge r ( $type , $ s i d e ?) {say ”’ I d l i k e a $ type bu rge r ” ˜
( d e f i n e d ( $ s i d e ) ??” wi th a s i d e o f $ s i d e ” ! ! ”” ) ;
}
. . . . . .
orde r−bu rge r (” t r i p l e bacon ” ,” deep f r i e d on ion r i n g s ” ) ;
. . . . . .
按参数名传递参数
. . . . . .
sub orde r−bee r ( $type , $ p i n t s ) {say ( $ p i n t s == 1 ??’
A ’p i n t ! !” $ p i n t s p i n t s ”) ˜ ” o f $type , p l e a s e . ”
}
. . . . . .
orde r−bee r ( type => ’’Hobgobl in ,p i n t s => 1 ) ;
# A p i n t o f Hobgobl in , p l e a s e .
. . . . . .
orde r−bee r ( p i n t s => 3 ,type => ’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 orde r−sh r imps ( $count ,: $ from = ’North ’Sea ) {
say ”’ I d l i k e $count . . . $ from . . . ” ;}
. . . . . .
# ta ke s the d e f a u l t v a l u e ’North ’Seaorde r−sh r imps ( 6 ) ;o rde r−sh r imps (4 , from => ’A t l a n t i c ’Ocean ) ;
. . . . . .
# not a l l owed , : $ from i s named on l yo rde r−sh r imps (22 , ’Med i t e r r anean ’Sea ) ;
. . . . . .
或者就当不存在
. . . . . .
sub des i gn−i c e−cream−mix tu r e (: $name ! ,$base = ’’V a n i l l a ,@mix ins ) {
say ” C r e a t i n g a new r e c i p e named $name ! ”}
. . . . . .
des ign−i c e−cream−mix tu r e (name => ’’P l a i n ) ;
. . . . . .
# m i s s i n g $namedes ign−i c e−cream−mix tu r e (
base => ’St r awbe r r y ’ ch i p ) ;
. . . . . .
这里不讨论重命名参数,有兴趣的朋友可以自行查询
. . . . . .
有隐式拆封就有隐式封装
. . . . . .
sub shout−them (∗@words ) {f o r @words −> $w {
p r i n t uc (”$w ” ) ;}
}
. . . . . .
# now you can pas s i t emsshout−them’(’go ) ; # GOshout−them’(’go , ’’home ) ; # GO HOME
. . . . . .
my @words = ’(’go , ’’home ) ;shout−them ( @words ) ; # s t i l l works
. . . . . .
山寨一个修饰模式
. . . . . .
sub debug−wrapper (&code ,∗ @ p o s i t i o n a l ,∗%named ) {
warn ” C a l l i n g ’&code . name ( ) ’ w i th arguments ”˜ ” @ 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−wrapper (&orde r−shr imps ,4 ,from => ’A t l a n t i c ’Ocean ) ;
. . . . . .
还记得 Python 的 tuple 隐式拆装吗?
. . . . . .
sub menu {i f rand < 0 .5 {’’
f i s h , ’wh i t e ’wine} e l s e {’’
s teak , ’ r ed ’wine ;}
}my ( $ food , $beve rage ) = 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 Numericbut got Str instead
. . . . . .
还记得 Haskell 的参数匹配吗?
. . . . . .
sub c i r c l e −r a d i u s−from−a r ea (Numeric $a rea
where { $a rea >= 0 }) {( $a rea / p i ) . s q r t
}
. . . . . .
say c i r c l e −r a d i u s−from−a r ea ( 3 ) ; # OKsay c i r c l e −r a d i u s−from−a r ea (−3); # E r r o r
. . . . . .
只接受限定值
. . . . . .
sub se t−volume ( Numeric $volume where 0 . . 1 1 ) {say ” Turn ing i t up to $volume ” ;
}
. . . . . .
可以使用 hash 约束参数
. . . . . .
Currying !
. . . . . .
原型
sub orde r−bu rge r ( $type , $ s i d e ? ) { . . . } ;
. . . . . .
assuming
&order−the−u s u a l :=&orde r−bu rge r . assuming ( ’’ v e g g i e ) ;
&orde r−burger−and− f r i e s :=&orde r−bu rge r . assuming (
s i d e => ’ f r e n c h ’ f r i e s ) ;
. . . . . .
USAGE
orde r−the−u s u a l ( ’’ s a l s a ) ;o rde r−the−u s u a l ( s i d e => ’’ b r o c c o l i ) ;o rde r−burger−and− f r i e s ( ’’ p l a i n ) ;o rde r−burger−and− f r i e s ( : type<<double−beef>> ) ;
. . . . . .
函数自省
. . . . . .
定一个函数
. . . . . .
sub l o g a r i t h m ( Numeric $x , Numeric : $base = exp ( 1 ) ) {l o g ( $x ) / l o g ( $base ) ;
}
. . . . . .
得到参数
. . . . . .
my @params = &l o g a r i t h m . s i g n a t u r e . params ;say @params . e lems , ’’ pa ramete r s ;
. . . . . .
得到参数的详细信息
. . . . . .
f o r @params {say ”Name : ” , . name ;say ” Type : ” , . t ype ;say ” named? ” , . named ?? ’’ye s ! ! ’’no ;say ” s l u r p y ? ” , . s l u r p y ?? ’’ye s ! ! ’’no ;say ” o p t i o n a l ? ” , . o p t i o n a l ?? ’’ye s ! ! ’
’no ;}
. . . . . .
各种 to-json
. . . . . .
m u l t i to−j s o n ( Rea 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 ( Ar ray $d ) {r e t u r n ’ [ ’
˜ $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 ( . key )˜ ’ : ’˜ to−j s o n ( . v a l u e ) } ) . j o i n ( ’ , ’ )˜ ’ } ’ ;
}
. . . . . .
m u l t i to−j s o n ( $dwhere { ! d e f i n e d $d }) {
’ n u l l ’}
. . . . . .
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 type ”
˜ $d .WHAT. p e r l}
. . . . . .
Perl 6 增强了面向对象语法,使之更合理,更易写易读。
. . . . . .
下面看一个比较完整的示例
. . . . . .
比 P5 更好读的结构
c l a s s Task {. . . .
}
. . . . . .
属性使用 has 标示
. . . . . .
c l a s s Task {has &! c a l l b a c k ;has Task @! dependenc i e s ;has Bool $ . done ;
. . .
. . . . . .
! 操作符表示私有限定
has &! c a l l b a c k ;has Task @! dependenc i e s ;
. . . . . .
私有变量与访问器
. . . . . .
has Bool $ ! done ;method done ( ) {
r e t u r n $ ! done}
. . . . . .
. 标示自动封装一个只读访问器
. . . . . .
has Bool $ . done ;
. . . . . .
可以明确标示可读写
. . . . . .
has Bool $ . done i s rw ;
. . . . . .
普通的成员方法
. . . . . .
method add−dependency ( Task $dependency ) {push @! dependenc i e s , $dependency ;
}
. . . . . .
构造函数
. . . . . .
# RAKUDO: Should 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 ∗@dependenc i e s ) {r e t u r n s e l f . b l e s s (∗ ,
: $ c a l l b a c k ,: @dependenc i e s ) ;
}
. . . . . .
使用示范
. . . . . .
my $eat =Task . new ({ say ’ e a t i n g d i n n e r . NOM’ ! } ,
Task . new ({ say ’making ’d i n n e r } ,. . .)
) ;
. . . . . .
模式匹配——regex
. . . . . .
If you know regexes from other programming languages it’’s bestto 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 {. say i f m/who / ;
} ;
. . . . . .
表达式内插
. . . . . .
这明明是个半成品啊喂˜
my token word { \w+ [ \ ’ \w+]? } ;my r egex 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> / {
say ”Found ’{$<dup><word>} ’tw i c e i n \” $ l i n e \”” ;
}}
. . . . . .
让它去读诗,结果杯具了……
. . . . . .
《失乐园》
. . . . . .
Of Mans First Disobedience, and the FruitOf that Forbidden Tree, whose mortal tastBrought 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 token 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+ [ <ope ra to r> \d+ ]∗ }token o p e r a t o r { ’ / ’ | ’∗ ’ | ’+ ’ | ’− ’ } ;
} ;
. . . . . .
my regex f { f } ;my r egex o { o } ;ok ” foo ” ˜˜ /< f=&f> <o=&o>+ / ,
’ Regex matches ( 2 ) ’ ;l i v e s o k { $ / . p e r l } , ’ l i v e s . . .
. . . . . .
谢谢大家!