Perl6 中的 twigil 是什么?
What is a twigil in Perl6?
我正在阅读这本精彩的书 introduction to Perl6 并且遇到了一个相当有趣的术语:
Note the !
twigil means “this is private to the class”.
class ScoreKeeper {
has %!player-points;
}
我知道 Perl5 中的内容 sigils are。但什么是 twigil?
attribute/variable 名字前面有两个印记只是一种奇特的说法吗?
似乎与变量作用域有关:
Twigils influence the scoping of a variable...
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
来自the documentation on twigils:
Attributes are variables that exist per instance of a class. They may be directly accessed from within the class via !:
class Point {
has $.x;
has $.y;
method Str() {
"($!x, $!y)"
}
}
Note how the attributes are declared as $.x
and $.y
but are still accessed via $!x
and $!y
. This is because in Perl 6 all attributes are private and can be directly accessed within the class by using $!attribute-name
. Perl 6 may automatically generate accessor methods for you though. For more details on objects, classes and their attributes see object orientation.
Public 属性有 .
twigil,私有属性有 !
twigil。
class YourClass {
has $!private;
has @.public;
# and with write accessor
has $.stuff is rw;
method do_something {
if self.can('bark') {
say "Something doggy";
}
}
}
设计文档S02 and S99都谈到了twigils。 (强调我的)。
Ordinary sigils indicate normally scoped variables, either lexical or
package scoped. Oddly scoped variables include a secondary sigil (a
twigil) that indicates what kind of strange scoping the variable is
subject to: [...]
所以它是一个次要印记或者更确切地说是一个第二个印记。声明 $*foo
不会声明 $foo
.
my $*foo = 1;
say $foo;
这将产生 变量“$foo”未声明在...。
我正在阅读这本精彩的书 introduction to Perl6 并且遇到了一个相当有趣的术语:
Note the
!
twigil means “this is private to the class”.class ScoreKeeper { has %!player-points; }
我知道 Perl5 中的内容 sigils are。但什么是 twigil?
attribute/variable 名字前面有两个印记只是一种奇特的说法吗?
似乎与变量作用域有关:
Twigils influence the scoping of a variable...
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
来自the documentation on twigils:
Attributes are variables that exist per instance of a class. They may be directly accessed from within the class via !:
class Point {
has $.x;
has $.y;
method Str() {
"($!x, $!y)"
}
}
Note how the attributes are declared as
$.x
and$.y
but are still accessed via$!x
and$!y
. This is because in Perl 6 all attributes are private and can be directly accessed within the class by using$!attribute-name
. Perl 6 may automatically generate accessor methods for you though. For more details on objects, classes and their attributes see object orientation.
Public 属性有 .
twigil,私有属性有 !
twigil。
class YourClass {
has $!private;
has @.public;
# and with write accessor
has $.stuff is rw;
method do_something {
if self.can('bark') {
say "Something doggy";
}
}
}
设计文档S02 and S99都谈到了twigils。 (强调我的)。
Ordinary sigils indicate normally scoped variables, either lexical or package scoped. Oddly scoped variables include a secondary sigil (a twigil) that indicates what kind of strange scoping the variable is subject to: [...]
所以它是一个次要印记或者更确切地说是一个第二个印记。声明 $*foo
不会声明 $foo
.
my $*foo = 1;
say $foo;
这将产生 变量“$foo”未声明在...。