note
tye
<p>
My advice would be to stop using YAML and switch to JSON ("overcomplicated" so often turns out to be a problem, as this example illustrates). ;)
</p><blockquote><i>
Is there a way to keep YAML::XS from blessing objects?
</i></blockquote><p>
[http://cpansearch.perl.org/src/INGY/YAML-LibYAML-0.38/LibYAML/perl_libyaml.c] makes it clear that the answer is "no":
</p><c>
/* Deal with possibly blessing the hash if the YAML tag has a class */
if (tag && strEQ(tag, TAG_PERL_PREFIX "hash"))
tag = NULL;
if (tag) {
char *class;
char *prefix = TAG_PERL_PREFIX "hash:";
if (*tag == '!') {
prefix = "!";
}
else if (strlen(tag) <= strlen(prefix) ||
! strnEQ(tag, prefix, strlen(prefix))
) croak(
loader_error_msg(loader, form("bad tag found for hash: '%s'", tag))
);
class = tag + strlen(prefix);
sv_bless(hash_ref, gv_stashpv(class, TRUE));
}
</c><blockquote><i>
how to declare a package with name 'java.util.UUID'
</i></blockquote><p>
Well, I can imagine ways to do that. But you don't even bother to show the output of your little snippet and I'm not going to bother to download [mod://YAML::XS] (which I have no plans to ever use) so I don't have a good idea what kind of "hash" libyaml is supposed to make out of the little example blob of text you included. So my demonstration may not be one that will actually work on your example:
</p><c>
BEGIN {
{
package YAML::Sucks::JavaUtilUUID::_alias;
@ISA = 'YAML::Sucks::JavaUtilUUID';
}
$main::{'java.util.UUID::'} = $YAML::Sucks::JavaUtilUUID::{'_alias::'};
}
{
package YAML::Sucks::JavaUtilUUID;
use overload '""' => \&asString;
sub asString {
my( $self ) = @_;
return join ' ', values %$self;
}
}
my $fromYaml = bless { whatever => 'some UUID?' }, 'java.util.notUUID';
print "($fromYaml)\n";
$fromYaml = bless { whatever => 'some UUID?' }, 'java.util.UUID';
print "($fromYaml)\n";
__END__
(java.util.notUUID=HASH(0x367c44))
(some UUID?)
</c><p>
(Speaking of "overcomplicated", eh?) Or you could just use [doc://unbless] instead.
</p><p>
Like I mentioned, I don't know what key name(s) libyaml is creating for that example. But you should be able to use <c>$ref->{fooUuid}{$whatever}</c> in place of <c>$ref->{fooUuid}</c>, regardless of the bless()ing (to get at the string that I presume that you want).
</p>
<div class="pmsig"><div class="pmsig-22609"><p align="right">
- [tye]<tt> </tt>
</p></div></div>
968331
968331