Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?

by Anonymous Monk
on Jul 15, 2019 at 02:39 UTC ( #11102842=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I made a class in Moo for some objects. So i wanted to write one as yaml file. Then i could edit the yaml file and easily add more types of objects when i load it in. But YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that? Or is this madness and i should just dump the objects some other way?
  • Comment on YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?

Replies are listed 'Best First'.
Re: YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?
by Haarg (Curate) on Jul 15, 2019 at 13:15 UTC

    I would consider unblessing a bad idea. My recommendation would be to provide a method on your class to provide a hashref representation of your object, and use that in the value you send to YAML::Tiny. Moo just uses normal hashref objects, so the method can be pretty simple and just do a shallow clone, without re-blessing the result.

    sub as_hashref { return { %{ $_[0] } }; }

    This gives you an easy place to change how the object is serialized if you need it in the future, but doesn't over-complicate things. The more "correct" way to do this would involve using an explicit list of attributes and using their accessors, but it isn't really a problem to rely on it being a normal hashref based object. You are breaking encapsulation, but it's your object, so you can choose if that matters to you.

    While Moo can work with objects that aren't based on hashrefs or use other non-standard storage, that isn't the default and is something you'd have to explicitly enable with a MooX module.

Re: YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?
by tinita (Parson) on Jul 16, 2019 at 09:30 UTC
Re: YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?
by dsheroh (Monsignor) on Jul 15, 2019 at 10:48 UTC
    Data::Structure::Util includes an unbless function which can do that.

    However, my personal preference for situations like this is to give my objects an ->unblessed method which copies the object's contents into a new, unblessed hashref and returns that, leaving the original object intact. It takes up a little more time and memory than unblessing the original reference, of course, but it allows me to continue using the object as an object afterwards, If you're just going to free the object after unblessing and dumping it, then that's not a concern, so Data::Structure::Util::unbless would probably make more sense in that situation.

Re: YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?
by daxim (Curate) on Jul 15, 2019 at 07:49 UTC
    I don't think it's mad, just awfully inconvenient. When you run into limitations, upgrade to MooseX::Storage/KiokuDB.
Re: YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?
by Anonymous Monk on Jul 15, 2019 at 03:15 UTC
    this works but it uses a regex and eval
    #!/usr/bin/env perl package Deblessify; use strictures 2; use feature 'say'; use Data::Dumper; use YAML::Tiny; use Moo; use MooseX::MungeHas 'is_rw'; # attrs has 'abc'; has 'def'; has 'foo'; has 'bar'; has 'name'; run(); sub run { my $self = __PACKAGE__->new; $self->abc({aaa => 123, bbb => 456,}); $self->def([qw/123 456 789 abc def ghi jkl mno/]); $self->foo('foo bar baz xyzzy'); $self->bar(123.456789); $self->name('testfile'); $self->deblessify; } sub deblessify { my ($self) = @_; my $dumpstr = Dumper($self); say "dumpstr = $dumpstr"; $dumpstr =~ s/^(\$VAR.*? =) bless\(/\$deblessed_obj =/; $dumpstr =~ s/\s*?},\s+'.*?'\s+\);$/};/; say "new str = $dumpstr"; my $deblessed_obj = ''; eval $dumpstr; say "deblessified obj: $deblessed_obj"; my $yaml = YAML::Tiny->new($deblessed_obj); my $filename = $self->name . ".yaml"; $yaml->write($filename); } 1; __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11102842]
Approved by GrandFather
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-12-03 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (29 votes). Check out past polls.

    Notices?