On 2014-02-06 Ben Edwards <bedwa...@venda.com> wrote:
> 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}

I think this is what you're after::

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

See Class::MOP::Attribute, parent class of Moose::Meta::Attribute.

> 
>      my $value = $object->$name;

Another way to do this is::

       my $value = $attr->get_value($object);

which will bypass method modifiers applied to the read-accessor;
alternatively::

       my $reader = $attr->get_read_method;
       next unless $reader; # attribute does not have a reader
       my $value = $object->$reader();

this will call the correct read-accessor (even if it has a different
name from the attribute itself), and all applied modifiers.

>      print $value;
> }

-- 
        Dakkar - <Mobilis in mobile>
        GPG public key fingerprint = A071 E618 DD2C 5901 9574
                                     6FE2 40EA 9883 7519 3F88
                            key id = 0x75193F88

Man will never fly.  Space travel is merely a dream.  All aspirin is
alike.

Attachment: signature.asc
Description: PGP signature

Reply via email to