匿名
子例程、方法或子方法跨新,當它們不能通過名字調(diào)用時域帐,就被稱為匿名的
# named subroutine
sub double($x) { 2 * $x };
# 匿名子例程,存儲在一個具名的標量里
my $double = sub ($x) { 2 * $x };
注意肖揣,匿名子例程仍然可以有名字
# 使用 anon 關鍵字使子例程匿名
my $s = anon sub triple($x) { 3 * $x }
say $s.name; # triple
副詞
通常, 副詞是函數(shù)的命名參數(shù). 也有一些其它特殊語法形式允許副詞出現(xiàn)在某些合適的地方:
q:w"foo bar" # ":w" is a Quotelike form modifier adverb
m:g/a|b|c/ # ":g" is also
4 +> 5 :rotate # ":rotate" is an operator adverb
@h{3}:exists # ":exists" is also, but is known as a subscript adverb
副詞通常使用冒號對兒標記來表示, 因為這個原因, 冒號對兒標記法也以副詞對兒形式著稱:
:a(4) # Same as "a" => 4
Autothreading
Autothreading
是這樣的: 如果你傳遞一個 junction
給子例程, 該子例程期望的參數(shù)類型為Any
或它的子類型. 那么這個子例程調(diào)用會被執(zhí)行多次, 每次使用一個不同的 junction 狀態(tài). 這些調(diào)用的結果被組合成一個跟原 junction
同類型的 junction
.
sub f($x) { 2 * $x };
if f(1|2|3) == 4 {
say 'success';
}
這里 f()
是含有一個參數(shù)的子例程龙优,然而因為它沒有顯式的類型聲明事秀,它就被隱式的聲明為 Any
型彤断。 Junction 參數(shù)使 f(1|2|3)
調(diào)用在內(nèi)部作為 f(1)|f(2)|f(3)
執(zhí)行,而結果是跟原 junction
同類型的 junction
, 即 2|4|6
. 這種把一個 Junction
分成對多次函數(shù)調(diào)用的處理就叫做 autothreading
.
Colon Pair and Colon List
冒號對兒是用于創(chuàng)建或 Pair 對象的便捷語法. 兩種最常見的形式是:
:a(4) # Same as "a" => 4, same as Pair.new(:key<a>,:value(5))
:a<4> # Same as "a" => "4", same as Pair.new(:key<a>,:value<5>)
這也是人們熟知的副詞對兒形式. 注意, 當冒號后面括號前面的部分不是一個合法的標識符的時候, 會應用其它語義, 不是所有的副詞對兒都創(chuàng)建 Pair
對象.
另外兩個常見的形式是:
:a # Same as :a(True)
:!a # Same as :a(False)
一個 colon 列表是一個僅包含冒號對兒的列表, 不需要逗號, 甚至不需要空格:
:a(4):c:!d:c # Same as a => 4, c => True, d => False, c => True
Constraint
約束是給參數(shù)或 subset 類型添加的限制. 通過單詞 where 引入約束. 在下面的例子中, 約束用于確保 , 當調(diào)用一個名為 abbreviate 的子例程, 其參數(shù)為一個長度小于 10 個字符的字符串時,會拋出一個錯誤:
sub abbreviate (Str $thing where { .chars >= 10 }) { ... }
上例中的 Str 也是一個約束, 但是經(jīng)常作為"類型約束".
Instance
類的實例在其它編程語言中也叫對象. 對象存儲屬性, 通常是 new 方法調(diào)用的返回值, 或者是對象字面量.
大部分類型的實例被定義為 True, 例如 defined($instance)
為 True.
my Str $str = "hello"; ## 這使用內(nèi)建類型,例如 Str
if defined($str) {
say "Oh, yeah. I'm defined.";
} else {
say "No. Something off? ";
}
## if you wanted objects...
class A {
# nothing here for now.
}
my $an_instance = A.new;
say $an_instance.defined.perl;# defined($an_instance) works too.
類擁有方法和屬性的所有藍圖, 而類的實例把藍圖帶到真實世界中.
Invocant
在 Perl 6 中調(diào)用方法的對象叫做調(diào)用者. 在方法中它就是 self
引用的東西.
say 'str'.uc; # 'str' 是 方法 uc 的調(diào)用者
Literal
字面量是一塊直接代表對象的代碼, 通常指向?qū)ο笞陨?
my $x = 2; # the 2 is a literal
say $x; # $x is not a literal, but a variable
lvalue
lvalue 或者左值是能出現(xiàn)在賦值操作符左側的任何東西; 典型的左值有變量,私有屬性和 is rw
屬性, 變量列表和左值子例程.
左值的例子:
Declaration lvalue Comments
my $x; $x
my ($a, $b); ($a, $b)
has $!attribute; $!attribute Only inside classes
has $.attrib is rw; $.attrib
sub a is rw { $x }; a()
不是左值的例子:
3 # literals
constant x = 3; # constants
has $.attrib; # attributes; you can only assign to $!attrib
sub f { }; f(); # "normal" subs are not writable
sub f($x) { $x = 3 }; # error - parameters are read-only by default
Mainline
mainline
是程序中不屬于任何 block 的程序文本.
use v6; # mainline
sub f {
# not in mainline, in sub f
}
f(); # in mainline again
Slurpy
子例程或方法中的形參如果能接收任意數(shù)量的參數(shù), 那這個形參就會被認為是 slurpy
的. 它由參數(shù)名字前面的星號標出.
sub sum (*@numbers) {
return [+] @numbers;
}
Type Object
類型對象是一個代表類 /role/package/grammar/enum
的對象. 它通常和類型名相同.
class A { };
say A; # A is the type object
my $x = A.new(); # same here
my $x = class {
method greet() {
say "hi";
}
}
# $x now holds a type object returned from the
# anonymous class definition