tags:

views:

79

answers:

1

What is the difference between cmpStr and cmpStrHard in FreezeThaw?

They are mentioned in the FAQ How do I test whether two arrays or hashes are equal?

+5  A: 

The documentation is very vague and the source code is hard to follow but the example in the FAQ provides some insight. After studying all of them, I think I grok what the functions do and what the documentation means.

CmpStr compares the serialized representations of two data structures. It returns 0 if they are equivalent. It returns non-zero otherwise. (Technically, it returns -1 if the first data structure is less than the second one and +1 if it's greater, but the concept of "less than" and "greater than" aren't particularly useful for hashes.)

CmpStrHard is similar but stricter. It returns 0 only if the two data structures are exactly the same.

The difference between equivalence and equality is in the references that are used to build complex data structures. Consider the following:

my ($x, $y, $z);
$x = [1];
$y = [1];
$z = $y;

$x and $y are equivalent: they are both references to an array which contains the single value "1". $y and $z are more than equivalent, they're equal: they are both references to the same anonymous array. If you say

$y->[0] = 2;

then $x and $y will no longer be equivalent, but $y and $z will still be equal.

I believe that when the FreezeThaw documentation says "considered as a group" it's speaking of data structures that could contain references to the same objects. (And if so, that a freeze/thaw cycle should preserve those relationships.)

Michael Carman
Would $z[0] be 2 as well ?
biznez
Yes, `$z->[0]` would be 2 as well, because both `$y` and `$z` are references to the same array.
Michael Carman