Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: YAML scalar ref strange behavior (JSON++)

by tye (Sage)
on Jun 04, 2014 at 02:45 UTC ( [id://1088558]=note: print w/replies, xml ) Need Help??


in reply to YAML scalar ref strange behavior

Yeah, YAML has such an unreasonably large spec that it ends up having plenty of room for unpleasant surprises. (It also is too large of a spec for any single person to ever even come close to fully understanding it so nobody can correctly implement it so you get disagreement on even fairly simple things between implementations, IME.)

But, no, \99 in a loop isn't guaranteed to give you references to distinct scalars, as is easily shown:

my @x; push @x, { xx => \99 } for 1, 2; print "same ref\n" if $x[0]{xx} == $x[1]{xx};

But YAML then makes the problem worse. We start out with two distinct scalars each holding references to the same constant. YAML turns that into the two hash values being aliases to the same scalar (holding a single reference to the scalar value of 99). Clearly, the YAML spec needs some additions so that distinct references to the same scalar can be encoded differently than a single reference being held in two aliases within the data structure. ;)

I can't tell you how to fix the problem as you are quick to rule out the likely ways one would fix the problem so I suspect you'll likely rule out any fix I might suggest. Several alternate fixes seem easy to come up with, so my guess is you aren't considering those as you have reason to eliminate them.

Though, I certainly encourage you to start to move to JSON (which has a spec so tiny that it borders on being trivial to understand -- other than some slight complexity around character encoding details that often just don't matter). I very much don't want my "transfer data" encoding to support the concept of aliases, which is just one of the many reasons that I like JSON.

- tye        

Replies are listed 'Best First'.
Re^2: YAML scalar ref strange behavior (JSON++)
by rodd (Scribe) on Jun 04, 2014 at 14:33 UTC
    Ok, your example made me realize that the best approach for now would be generating a unique reference using... eval:

    my @arr; push @arr, { xx=>eval '\99' } for 1..2; say YAML::XS::Dump(\@arr);
    Which prints:

    --- - xx: !!perl/ref =: 99 - xx: !!perl/ref =: 99

    which in place Loads fine, working around the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1088558]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found