YAML and Bless to dump subset of object fails for me

YAML and Bless to dump subset of object fails for me

am 15.11.2007 23:02:44 von Rud1ger Sch1erz

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.


#!/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;



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.

Re: YAML and Bless to dump subset of object fails for me

am 16.11.2007 20:51:54 von jl_post

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.
>
>
> #!/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;
>
>

>
> 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