是否可以创建只能在Moose的构造函数中设置的属性?我想做这样的事情:
my $foo = new Foo(file => 'foo.txt');
my $bar = new Foo(string => $str);
$foo->file('baz.txt'); # dies
Run Code Online (Sandbox Code Playgroud)
我知道我可以创建一个无法在构造函数中设置的属性,但似乎缺少补充案例.
这不仅仅是一个只读属性吗?如果我写
package Foo;
use Moose;
has 'file' => (is => 'ro', isa => 'Str');
has 'string' => (is => 'rw', isa => 'Str');
1;
Run Code Online (Sandbox Code Playgroud)
然后你的代码就死了
Cannot assign a value to a read-only accessor
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
235 次 |
最近记录: |