perldata-dumper

Perl: Checksum of complex data-structure


Is there a package/function in Perl that gives me in an easy way

Best idea I have in mind is

  1. serialize my structure to a string (e.g. with Data::Dumper)

  2. Hash over the string with MDx

But maybe there is some more elegant way.


Solution

  • In the past, I used the Data::Dumper (with sorted keys, as pointed by @mob) + Digest::MD5 approach for creating checksums of complex data structures. In my case, the purpose was to compare two or more data structures for equality.

    (Very) Simple snippet:

    use Data::Dumper qw( Dumper ) ;
    use Digest::MD5 qw( md5_hex) ;
    
    sub digest {
        my $data = shift ;
        local $Data::Dumper::Sortkeys = 1;
        return md5_hex( Dumper($data) ) ;
    }
    

    Synopsis:

    my $cplx_data_checksum = digest({ 
        c => 1 , 
        b => [ 1 , { a => 2 } ]
    }) ;
    

    For insights about Digest algo's speed please take a look to the Digest Perl module at https://metacpan.org/pod/Digest#Digest-speed

    Hope this helps