Re: Checking if a Moose object has a value
[email protected] (Gianni Ceccarelli) Thu, 6 Feb 2014 13:02:09 +0000
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <20140206130209.5da0201f@nautilus> |
On 2014-02-06 Ben Edwards <[email protected]> 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.
signature.asc
(application/pgp-signature, 198 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) iEYEARECAAYFAlLzh9cACgkQQOqYg3UZP4jJowCcCKMtEHhS5DqHZ7GAkCG40h1j CBMAn3vRqWVYosy8A3NjON6qdQ8JcJZ/ =H+UX -----END PGP SIGNATURE-----