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

My internet connection to YAML bug report is bad, so I firstly post my question here.
#!/usr/bin/perl use strict; use YAML qw/Dump Bless/; use YAML::Node; { package Foo; use Moose; has foo => ( is => 'rw', isa => 'Any' ); has bar => ( is => 'rw', isa => 'Any' ); } my $foo = Foo->new( foo => 'fooValue', bar => 'barValue' ); Bless($foo)->keys(['foo']); print "##### Moose obj #####\n"; print Dump $foo; my $bar = { foo => 'fooValue', bar => 'barValue' }; Bless($bar)->keys(['foo']); print "##### hashref #####\n"; print Dump $bar;
It seems that the "keys" system do not affect Moose object at all...

Replies are listed 'Best First'.
Re: Moose and YAML
by CountZero (Bishop) on Apr 26, 2010 at 18:30 UTC
    When I run your script it outputs:
    ##### Moose obj ##### --- !!perl/hash:Foo bar: barValue foo: fooValue ##### hashref ##### --- foo: fooValue
    Did you expect anything else?

    Update: Try it with any standard (non-moose) object: you will get the same result. An object is not a simple data-structure: Bless and keys only work their magic on non-object data-structures.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      So how should I work with objects?
        YAML just "works" with objects too:
        use strict; use warnings; use 5.010; use YAML qw/Dump Bless Load/; use Data::Dumper; { package Foo; use Moose; has foo => ( is => 'rw', isa => 'Any' ); has bar => ( is => 'rw', isa => 'Any' ); } my $foo = Foo->new( foo => 'fooValue', bar => 'barValue' ); print 'Dumper original object: ', Dumper(\$foo); my $yaml_foo = Dump $foo; # Serialize object to YAML my $other_foo = Load ($yaml_foo); # Deserialize to Foo say 'Dumper new object: ',Dumper(\$other_foo); say $other_foo->isa('Foo') ? 'I am a Foo!' : 'I do not know what I am. +';
        As expected it returns:
        Dumper original object: $VAR1 = \bless( { 'bar' => 'barValue', 'foo' => 'fooValue' }, 'Foo' ); Dumper new object: $VAR1 = \bless( { 'bar' => 'barValue', 'foo' => 'fooValue' }, 'Foo' ); I am a Foo!

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James