Sewi has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I got a hash which ususally gets few or even no keys. It is given to some external module which may change it.

I'ld like to keep a copy before handing the hash to the external function and compare hash and copy after the function returns to detect if there are changes.

I'ld like to advoid writing a TIE-module for this.

Is there a module which creates a hash checksum? I searched CPAN but had no luck.

Thank you.

Replies are listed 'Best First'.
Re: Hash compare or checksum
by moritz (Cardinal) on Sep 08, 2009 at 10:42 UTC
    You can use Storable (a core module) for generating a compact (binary) string representation of a hash, but make sure to first set $Storable::canonical = 1;

    You can either use that value directly for comparison, or calculate a checksum from it.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Hash compare or checksum
by Your Mother (Archbishop) on Sep 08, 2009 at 16:27 UTC

    Another way-

    perl -MJSON::XS -MDigest::MD5=md5_hex -le 'print md5_hex(encode_json({my => "hash"}))'

    JSON::XS is comparable in speed to Storable and will not cause binary/platform/version problems.

Re: Hash compare or checksum
by BrowserUk (Patriarch) on Sep 08, 2009 at 16:46 UTC

    Do you just need to if it has changed? Or also how?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.