I’m trying to get a moose object, which has moose objects to be referenced as just a nested set of plain perl datatypes, that I can refer to as a hashref. e.g.
my $ref = { %{ $obj } }
and the structure might be like
{
name => "bob",
phones => [
{
phone_number => "15555554698"
},
]
}
instead of
bless( {
name => "bob",
phones => [
bless( {
phone_number => "15555554698"
}, 'PhoneNumber' )
]
}, 'User' )
here’s my attempt
use overload '%{}' => '_hashref';
sub _hashref {
my $self = shift;
foreach my $attr ( $self->meta->get_all_attributes ) {
if ( $attr->has_read_method ) {
say $attr->name;
say $attr->get_value( $self );
}
}
return {};
}
unfortunately get_value seems to do something recursive, and runs until it segfaults. I tried passing it __PACKAGE__ and just 'User' but neither seem to work. Does anyone have any suggestions on how I might get the attributes and the values so that I can operate on them? or another smarter way to do this? note: I’m aware that I’m currently returning an empty hashref, at this point just trying to figure out how I might get at the values
The “recursive” thing that
get_valueis doing is trying to access$self->{$slot_name}, which is calling your%{}overload, which is callingget_value… You can work around this by temporarily defeating the overloading (there’s an example in the overload docs), or you could stop trying to reimplement what MooseX::Storage already does pretty well.