in reply to Re^2: Storing/parsing perl data structure in/from a file
in thread Storing/parsing perl data structure in/from a file

Plz note: the data you posted is not a valid serialization of a HoH!

The only way I see is to assign the data to a tied hash, which automatically reacts on collisions...of course all nested hashes need to be tied too.

something like

my $datastring =slurp $file; tie %h, "CollisionHash"; eval "\%h = $datastring"
Not sure of this works, and I don't wanna trie to implement it for such a "unique" requirement...

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^4: Storing/parsing perl data structure in/from a file
by LanX (Saint) on Jun 13, 2013 at 00:41 UTC
    As I feared (though it makes sense)

    tied_hashes do it with list assignments (they are sequential) but not with ano-hash assignment (they are precompiled, hence collisions are optimized)

    FWIW

    use strict; use warnings; use Data::Dump qw/pp/; package HoA; require Tie::Hash; our @ISA = qw(Tie::StdHash); sub STORE { my ($this,$key,$value) =@_; push @{$this->{$key}}, $value; } package main; my $data_str= do { local $/;<DATA>}; tie my %h, "HoA"; my $h=\%h; %h = eval "$data_str"; pp $h; %h = eval "%{ { $data_str } }"; pp $h; __DATA__ ( alpha => 1, alpha => 2 )

    shows

    { # tied HoA alpha => [1, 2], } { # tied HoA alpha => [2], }

    Cheers Rolf

    ( addicted to the Perl Programming Language)