Moose::Manual::Attributes州:
As an alternative to using a subroutine reference [for default],you can instead supply a builder method for your attribute:
…
This has several advantages. First,it moves a chunk of code to its own named method,which improves readability and code organization.
因此,您的属性可以定义默认值:
has attr => ( is => 'ro',builder => 'subroutine' ); sub subroutine { # figure out and return default value }
我不明白为什么这必须与默认分开.难道你不能只是传递对命名子程序的引用吗?
has attr => ( is => 'ro',default => \&subroutine );
那不是更好的编程实践,因为你保证不会意外地引用一个不存在的子程序吗?您将使用逻辑引用而不是符号引用来引用该方法.