YAML and Bless to dump subset of object fails for me

Discussion in 'Perl Misc' started by Rud1ger Sch1erz, Nov 15, 2007.

  1. Hi there,

    I'm fiddling around with YAML and try to write out a subset of a data
    object.

    The example of the YAML documentation works fine for me:

    use YAML qw(Dump Bless);

    use strict;

    my $hash = {apple => 'good', banana => 'bad', cauliflower => 'ugly'};
    print Dump $hash;
    Bless($hash)->keys(['banana', 'apple']);
    print Dump $hash;

    gives me:
    ---
    apple: good
    banana: bad
    cauliflower: ugly
    ---
    banana: bad
    apple: good

    But when I try to use Blessing of keys to filter and sort the output
    of an object reference, YAML seems to ignore my key subset.

    <code example>
    #!/usr/bin/perl -w
    #

    {
    package DailyBuildInfoSet;

    sub new {
    my($proto ) = @_;
    my $class = ref($proto) || $proto || DailyBuildInfoSet;
    my $self = {};

    bless ($self, $class);

    return $self;
    }

    sub value {
    my $self = shift;
    my $k = shift;
    if (@_) { $self->{$k} = shift }

    return $self->{$k};
    }

    1;
    }

    use YAML qw(Dump Bless);

    use strict;

    my $db = DailyBuildInfoSet->new();
    $db->value( "DOB", '20071114' );
    $db->value( "HMI_CL", '1113617' );
    $db->value( "TC_CL", '1111316' );

    print Dump $db;

    Bless($db)->keys(['HMI_CL', 'TC_CL']);
    print Dump $db;

    </code example>

    Output of this example is:

    $ t3.Al
    --- !!perl/hash:DailyBuildInfoSet
    DOB: 20071114
    HMI_CL: 1113617
    TC_CL: 1111316
    --- !!perl/hash:DailyBuildInfoSet
    DOB: 20071114
    HMI_CL: 1113617
    TC_CL: 1111316

    On the latter, I would expect (and like to have):

    --- !!perl/hash:DailyBuildInfoSet
    HMI_CL: 1113617
    TC_CL: 1111316

    BTW, perl -v gives me:
    This is perl, v5.8.8 built for cygwin-thread-multi-64int
    (with 8 registered patches, see perl -V for more detail)

    Could some kind soul point me to the reason, why YAML seems to ignore
    my key blessing?

    Cheers,
    Rudiger

    --
    Der folgende Satz ist falsch.
    Der vorherige Satz ist richtig.
    Rud1ger Sch1erz, Nov 15, 2007
    #1
    1. Advertising

  2. Rud1ger Sch1erz

    Guest

    On Nov 15, 3:02 pm, Rud1ger Sch1erz <> wrote:
    >
    > I'm fiddling around with YAML and try to write out a subset of a data
    > object.
    >
    > But when I try to use Blessing of keys to filter and sort the output
    > of an object reference, YAML seems to ignore my key subset.
    >
    > <code example>
    > #!/usr/bin/perl -w
    > #
    >
    > {
    > package DailyBuildInfoSet;
    >
    > sub new {
    > my($proto ) = @_;
    > my $class = ref($proto) || $proto || DailyBuildInfoSet;
    > my $self = {};
    >
    > bless ($self, $class);
    >
    > return $self;
    > }
    >
    > sub value {
    > my $self = shift;
    > my $k = shift;
    > if (@_) { $self->{$k} = shift }
    >
    > return $self->{$k};
    > }
    >
    > 1;
    >
    > }
    >
    > use YAML qw(Dump Bless);
    >
    > use strict;
    >
    > my $db = DailyBuildInfoSet->new();
    > $db->value( "DOB", '20071114' );
    > $db->value( "HMI_CL", '1113617' );
    > $db->value( "TC_CL", '1111316' );
    >
    > print Dump $db;
    >
    > Bless($db)->keys(['HMI_CL', 'TC_CL']);
    > print Dump $db;
    >
    > </code example>
    >
    > Output of this example is:
    >
    > --- !!perl/hash:DailyBuildInfoSet
    > DOB: 20071114
    > HMI_CL: 1113617
    > TC_CL: 1111316
    > --- !!perl/hash:DailyBuildInfoSet
    > DOB: 20071114
    > HMI_CL: 1113617
    > TC_CL: 1111316
    >
    > On the latter, I would expect (and like to have):
    >
    > --- !!perl/hash:DailyBuildInfoSet
    > HMI_CL: 1113617
    > TC_CL: 1111316
    >
    > Could some kind soul point me to the reason, why YAML seems to ignore
    > my key blessing?



    Dear Rudiger,

    Hmmm... that is strange.

    I've been tinkering around with your program, and I've discovered
    something a little strange.

    Try replacing these four lines of your code:

    my $db = DailyBuildInfoSet->new();
    $db->value( "DOB", '20071114' );
    $db->value( "HMI_CL", '1113617' );
    $db->value( "TC_CL", '1111316' );

    with the following four lines:

    $db = { DOB => '20071114',
    HMI_CL => '1113617',
    TC_CL => '1111316' };
    bless $db, "DailyBuildInfoSet";

    These new four lines do pretty much the same as the old four lines.
    And as you'd expect, the output from YAML::Dump() is exactly the same
    (which includes the "DOB" line).

    (In fact, you can change the bless() line to just "bless $db;" and the
    problem still persists.)

    But try this: Remove (or comment-out) the bless() line (the fourth
    line of the new code) and run the script again. When I run the
    script, the YAML::Dump() output no longer includes the "DOB" line.

    Apparently the fact that the $db object is bless()ed (with Perl's
    bless(), not YAML's Bless()) makes all the difference. I don't know
    why bless()ing a reference would make YAML::Bless() and/or
    YAML::Dump() work differently than what you'd expect -- it just does.

    (The "perldoc YAML" documentation says that Bless() associates a
    "normal Perl node." Maybe that means that a bless()ed object is not a
    normal Perl node. Or maybe not and it's just a bug in the YAML
    module.)

    Sorry I can't help you more, but at least we know that the
    difference is happening because the $db object was bless()ed.

    I hope this helped (if even in a small way), Rudiger.

    -- Jean-Luc Romano
    , Nov 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    173
    Paul Battley
    Aug 3, 2005
  2. Fransiscus Xaverius

    YAML Problem YAML::Object

    Fransiscus Xaverius, Dec 14, 2007, in forum: Ruby
    Replies:
    2
    Views:
    149
    Fransiscus Xaverius
    Dec 14, 2007
  3. John Trupiano
    Replies:
    1
    Views:
    168
    John Trupiano
    Sep 5, 2008
  4. Jonathan Gold
    Replies:
    0
    Views:
    131
    Jonathan Gold
    May 19, 2009
  5. Joey Zhou
    Replies:
    7
    Views:
    102
    John Carter
    Mar 14, 2011
Loading...

Share This Page