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 / ;
}
. . . . . .
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 ;
}
}
. . . . . .
惰性..
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 ) ;
}
. . . . . .
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 ;
. . . . . .
基本的
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
. . . . . .
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 )
. . . . . .
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 ) ;
. . . . . .
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 ” ) ;
. . . . . .
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 .
. . . . . .
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 ) ;
. . . . . .
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 ) ;
. . . . . .
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 ) ;
. . . . . .
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 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 >> ) ;
. . . . . .
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 ;
}
. . . . . .
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
}
. . . . . .
# 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 ) ;
}
. . . . . .
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
. . . . . .
这明明是个半成品啊喂˜
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 ” ” ;
}
}
. . . . . .
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 . . .
. . . . . .