Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: A fix for merge keys in YAML::XS, YAML::Syck

by Anonymous Monk
on Oct 10, 2015 at 16:48 UTC ( [id://1144382]=note: print w/replies, xml ) Need Help??


in reply to Re: A fix for merge keys in YAML::XS, YAML::Syck
in thread A fix for merge keys in YAML::XS, YAML::Syck

The original version may fail to merge keys if the merged hash is a sibling of the merging hash and itself merges a sibling. Success or failure then depends on the order in which hash values are returned by keys()!
key1: &key1 a: a b: b c: c key2: &key2 <<: *key1 d: d e: e f: f key3: <<: *key2 g: g h: h i: i ...
If key2 is pushed onto @_ before key3 all is well, but if not there will still be a merge key pointing at key1 after key2 has been merged into key3. Replacing the check for the existence of a mergekey with a while loop fixes that, because then a new merge will be performed and the outer while loop traversing @_ will not move on while there is still/again a merge key.
sub mergekeys_loop { my ( $orig ) = @_; while ( my $ref = shift ) { my $type = ref $ref; if ( $type eq 'HASH' ) { # my $tmphref = $ref->{'<<'}; # if ( $tmphref ) { while ( my $tmphref = $ref->{'<<'} ) { die "Merge key does not support merging non-hashmaps" unless ( ref $tmphref eq 'HASH' ); my %tmphash = %$tmphref; delete $ref->{'<<'}; %$ref = ( %tmphash, %$ref ); } push @_, grep { ref eq 'HASH' or ref eq 'ARRAY' } values % +$ref; } elsif ( $type eq 'ARRAY' ) { push @_, grep { ref eq 'HASH' or ref eq 'ARRAY' } @$ref; } } return $orig; }

Replies are listed 'Best First'.
Re^3: A fix for merge keys in YAML::XS, YAML::Syck
by Anonymous Monk on Oct 10, 2015 at 17:11 UTC
    I wrote:

    The original version may fail to merge keys if the merged hash is a sibling of the merging hash and itself merges a sibling. Success or failure then depends on the order in which hash values are returned by keys()!

    That should have read:

    The original version of mergekeys_loop() may fail to merge keys if the merged hash is a sibling of the merging hash and itself merges a sibling. Success or failure then depends on the order in which hash values are returned by values()!

    Sorry!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found