http://www.perlmonks.org?node_id=968345


in reply to Dealing with blessed scalars when using YAML::XS::Load

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). ;)

Is there a way to keep YAML::XS from blessing objects?

http://cpansearch.perl.org/src/INGY/YAML-LibYAML-0.38/LibYAML/perl_libyaml.c makes it clear that the answer is "no":

/* Deal with possibly blessing the hash if the YAML tag has a clas +s */ 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)); }
how to declare a package with name 'java.util.UUID'

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

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

(Speaking of "overcomplicated", eh?) Or you could just use unbless instead.

Like I mentioned, I don't know what key name(s) libyaml is creating for that example. But you should be able to use $ref->{foo­Uuid}{$whatever} in place of $ref->{foo­Uuid}, regardless of the bless()ing (to get at the string that I presume that you want).

- tye