Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以创建一个只能在 Moose 的构造函数中设置的属性?我想做这样的事情:
my $foo = new Foo(file => 'foo.txt'); my $bar = new Foo(string => $str); $foo->file('baz.txt'); # dies
我知道我可以创建一个无法在构造函数中设置的属性,但似乎缺少补充案例。
这不只是一个只读属性吗?如果我写
package Foo; use Moose; has 'file' => (is => 'ro', isa => 'Str'); has 'string' => (is => 'rw', isa => 'Str'); 1;
然后你的代码死了
Cannot assign a value to a read-only accessor