【发布时间】:2014-02-06 10:59:58
【问题描述】:
我正在尝试遍历 Moose 对象的属性,在不调用任何惰性构建器的情况下打印属性值(如果属性值存在则打印)。
到目前为止,我的代码如下所示:
for my $attr ($object->meta->get_all_attributes) {
my $name = $attr->name;
# Lazy attributes that have not already been generated will not
# exist in the object hash.
next unless exists $object->{$name}
my $value = $object->$name;
print $value;
}
有没有一种使用 Moose 检查对象的方法,它会告诉我属性值是否存在,而无需修改 Moose 类本身?
即上面代码中“除非存在下一个”行的更优雅的替代方案
感谢您的帮助和考虑 :)
【问题讨论】:
标签: perl introspection moose