in reply to Keeping Order with YAML::XS

Hi wtc,

Have been debating whether to post this or not. Was playing with this code while we were discussing your issue when a couple of people suggested using Tie::IxHash, but then found its ordering wasn't recognized by YAML::XS. Here it is for whatever it may be worth.

In an earlier post a quote from the YAML Spec shows the proper format for ordered mappings. Below is code to recursively convert a data structure containing IxHash's to one needed for output by YAML::XS. There is also a function to recursively convert a structure loaded from YAML::XS into one containing properly ordered IxHash's.

The code is presented as is, i'm sure there is a more elegant and less bug ridden version trying to get out -- would be happy to receive criticism. Mostly I hope it helps with your problem if you're still looking for a solution

use strict; use warnings; use Tie::IxHash; use Data::Dumper; use Data::Rmap qw(:types rmap_to); sub ixhash_to_yamlxs { my $item = \+shift; my $get_yaml_struct = sub { my $raw = shift; my $ix = tied(%$raw); return $raw unless defined $ix and $ix->isa('Tie::IxHash'); return [ map { { $_ => $raw->{$_} } } $ix->Keys ]; }; rmap_to { $_ = $get_yaml_struct->($_) } HASH, $item; return $item; } sub yamlxs_to_ixhash { my $item = shift; my $make_ixhash_from_array = sub { my $raw = shift; tie my(%hash), 'Tie::IxHash'; eval { no warnings; for (@$raw) { return $raw if scalar keys $_ ne 1; my ($key) = keys $_; $hash{$key} = $_->{$key}; } }; return $raw if $@; return \%hash; }; rmap_to { $_ = $make_ixhash_from_array->($_) } ARRAY, \$item; return $item; } tie my(%embed), 'Tie::IxHash', (j => 5, k => 6, l => 7); tie my(%hash), 'Tie::IxHash', (a => 1, c => { z => 26 }, b => 2, d => +[ 'j', 'k', 'l'], e => \%embed); my $out = ixhash_to_yamlxs \%hash; print Dumper $out; my $in = yamlxs_to_ixhash $out; print Dumper $in;
# IxHash converted for output by YAML::XS # Notice the embeded IxHash was handled # and also an embeded native hash (key 'c') # that is not converted to ordered structure \[ { 'a' => 1 }, { 'c' => { 'z' => 26 } }, { 'b' => 2 }, { 'd' => [ 'j', 'k', 'l' ] }, { 'e' => [ { 'j' => 5 }, { 'k' => 6 }, { 'l' => 7 } ] } ];
# ordered Tie::IxHash recovered from YAML::XS format \{ 'a' => 1, 'c' => { 'z' => 26 }, 'b' => 2, 'd' => [ 'j', 'k', 'l' ], 'e' => { 'j' => 5, 'k' => 6, 'l' => 7 } };