I often use Moose to make sure my data has the appropriate default values, for example here:
package Bla; use Moose; has eins => is => 'ro', isa => 'Int'; has zwei => is => 'ro', isa => 'Int', default => 2; no Moose; __PACKAGE__->meta->make_immutable; package main; use v5.10; use Data::Dumper; use URI; my $bla = Bla->new( eins => 77 ); my $bl2 = Bla->new; print Dumper $bla, $bl2; say join "\t", %$bla; say join "\t", %$bl2; my $u = URI->new( 'http://www.example.com/ws' ); $u->query_form( %$bla ); say $u; $u->query_form( %$bl2 ); say $u;
As long as such a data container does not have any reference elements (therefore, there is no nesting), you could say that this is normal or it is recommended to use the hash table of the object as in %$object , if you want to get in the source data, say, like initializer for serialization via URI->query_form or similar methods? Or is there a better way to achieve this that is built into Muses?
UPDATE
It seems that I led people along the wrong tracks, discarding the small serialization of the words in the lines above, and in the title, and even in the tags. Please note that I am not interested in finding serializers. In my example, the URI module is a serializer. The question is how to get the data to feed the URI->query_form (or any other serializer that I can try). So what I want to know is that for a given Moose $object you can capture data (keys and values ββor, if you want, names and values ββof properties) by simply dereferencing the reference to the object, as in %$object ? This will work if my experience so far collected is something that needs to be done while the objects do not contain any reference values ββ(for example, references to arrays, other objects, etc.) And - the thing about which I'm not sure - Moose won 'use the link to the instance to store your own data, for example __MOOSE_WHATNOT => $funky_moose_addon . So can Mose use the link to the instance to store some of his own data, or is this excluded by design?
UPDATE 2
To answer the question in the title:
No, do not use %$object to get the Moose object data, even if it does not contain any reference values, so you get a copy of the lines and numbers that make up $object . This is not normal because it destroys encapsulation. This can even lead to a runtime error, because although the hash is the default data structure to form the base of the Moose object, there is no guarantee that it will always be a hash, and in fact it can be something else.
Instead, you should use MooseX::Storage , which will provide the object to the pack method.