Perl 驼鹿属性存在

Perl 驼鹿属性存在,perl,introspection,moose,Perl,Introspection,Moose,我试图迭代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 h

我试图迭代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类本身

i、 e.上述代码中“除非存在下一个”行的更优雅的替代方案

感谢您的帮助和考虑:)

阅读文档将指向和

然后,您可以编写如下代码:

foreach my $attr ($object->meta->get_all_attributes) {
  my $name = $attr->name;

  next unless $attr->has_value($object);

  # Or, perhaps get_value(), depending on your requirements.
  say $attr->get_raw_value($object);
}