Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Ok, so not a fix, but a workaround. The problem is fairly well described in this blog post and this bug report for YAML::XS. The merge keys functionality of YAML is not supported by either YAML::XS or YAML::Syck, so this script:

#!/usr/bin/perl use strict; use warnings; use YAML::XS; use Data::Dumper; my $data; {local $/;$data = <DATA>}; print Dumper Load $data; __DATA__ --- key1: &id01 subkey_a: asdf subkey_b: qwer key2: <<: *id01 subkey_a: foo subkey_c: bar ...

produces this output:

$VAR1 = { 'key2' => { 'subkey_a' => 'foo', 'subkey_c' => 'bar', '<<' => { 'subkey_b' => 'qwer', 'subkey_a' => 'asdf' } }, 'key1' => $VAR1->{'key2'}{'<<'} };

The expected functionality is for the keys of key1 to be merged into the keys of key2, with key2 taking precedence. Not having the time or resources to dig into the XS guts of either module, my solution was a simple recursion function to traverse the data structure, merging the '<<' references with their containing objects:

#!/usr/bin/perl use strict; use warnings; use YAML::XS; use Data::Dumper; my $data; {local $/;$data = <DATA>}; print Dumper mergekeys( Load $data ); sub mergekeys { my ($ref) = @_; my $type = ref $ref; if ($type eq 'HASH') { my $tmphref = $ref->{'<<'}; if ($tmphref) { die "Merge key does not support merging non-hashmaps" unless (ref $tmphref eq 'HASH'); my %tmphash = %$tmphref; delete $ref->{'<<'}; %$ref = (%tmphash, %$ref); } mergekeys($_) for (values %$ref); } elsif ($type eq 'ARRAY') { mergekeys($_) for (@$ref); } return $ref; }

This produces the much more accurate data structure:

$VAR1 = { 'key2' => { 'subkey_b' => 'qwer', 'subkey_a' => 'foo', 'subkey_c' => 'bar' }, 'key1' => { 'subkey_b' => 'qwer', 'subkey_a' => 'asdf' } };

Obviously, this is not a very robust solution. It makes no checks for cyclical data, potentially expands the data structure due to dereferencing some addresses, and its recursive nature means it would rapidly consume resources when used on very deep data structures. But for the needs that I had, it worked pretty well. Hopefully the maintainers of YAML::XS and YAML::Syck can provide this functionality soon, since it seems that their respective underlying C libraries support merge keys just fine.

UPDATE: Found a couple easy ways to avoid recursion. See my reply below.


@_=qw; Just another Perl hacker,; ;$_=q=print "@_"= and eval;

In reply to A fix for merge keys in YAML::XS, YAML::Syck by bv

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found