Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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        


In reply to Re: Dealing with blessed scalars when using YAML::XS::Load by tye
in thread Dealing with blessed scalars when using YAML::XS::Load by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2018-06-23 21:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (125 votes). Check out past polls.

      Notices?