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



  • 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, details, 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, summary, 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.