Zai*_*aid 11 perl terminology perl6 raku
我正在读这篇关于Perl6的精彩介绍,并且遇到了一个相当有趣的术语:
请注意,
!twigil意味着"这是私人的".Run Code Online (Sandbox Code Playgroud)class ScoreKeeper { has %!player-points; }
我知道Perl5中有什么符号.但是什么是一个小事?
是否只是一种奇特的方式来说,在属性/变量名称之前有两个符号?
sim*_*que 11
普通的sigils表示通常作用域的变量,无论是词法还是包括作用域.奇怪的范围变量包括一个二级sigil(一个twigil),表明变量受到什么样的奇怪范围:[...]
所以这是次要的印记,或者说是第二印第安人.声明$*foo不会声明$foo.
my $*foo = 1;
say $foo;
Run Code Online (Sandbox Code Playgroud)
这将导致变量'$ foo'未在...处声明.
它似乎与变量范围有关:
Twigils影响变量的范围......
Run Code Online (Sandbox Code Playgroud)Twigil Scope ------ ---------------------------------------------- none Based only on declarator * Dynamic ! Attribute (class member) ? Compile-time variable . Method (not really a variable) < Index into match object (not really a variable) ^ Self-declared formal positional parameter : Self-declared formal named parameter = Pod variables ~ The sublanguage seen by the parser at this lexical spot
http://doc.perl6.org/language/variables#Twigils
属性是每个类实例存在的变量.可以通过!在课堂上直接访问它们:
class Point {
has $.x;
has $.y;
method Str() {
"($!x, $!y)"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意属性是如何声明的
$.x,$.y但仍然可以通过$!x和访问$!y.这是因为在Perl 6中,所有属性都是私有的,可以通过使用在类中直接访问$!attribute-name.Perl 6可能会自动为您生成访问器方法.有关对象,类及其属性的更多详细信息,请参阅面向对象.
公共属性有.两个,私人有!两个.
class YourClass {
has $!private;
has @.public;
# and with write accessor
has $.stuff is rw;
method do_something {
if self.can('bark') {
say "Something doggy";
}
}
}
Run Code Online (Sandbox Code Playgroud)