http://qs321.pair.com?node_id=392541


in reply to Re: Using FreezeThaw correctly?
in thread Using FreezeThaw correctly?

All I did was ask a simple question after RTFM, and after trying several different things, and after using SuperSearch. I'm not an idiot, nor am I lazy.

BUT, to humor you, from perlfaq4:

use FreezeThaw qw(cmpStr cmpStrHard); %a = %b = ( "this" => "that", "extra" => [ "more", "stuff" ] ); $a{EXTRA} = \%b; $b{EXTRA} = \%a; printf "a and b contain %s hashes\n", cmpStr(\%a, \%b) == 0 ? "the same" : "different"; printf "a and b contain %s hashes\n", cmpStrHard(\%a, \%b) == 0 ? "the same" : "different"; The first reports that both those the hashes contain the same data, while the second reports that they do not. Which you prefer is left as an exercise to the reader.

So yeah, it ought to be really simple. But if that is the case, shouldn't I be able to make the test fail when the two hashes are different?

Another thing I'm curious about: I mentioned that the test unexpectedly succeeded. There really isn't anything interesting when that happens. What information should I have provided?

In any event, I've actually tracked down the problem. There were two:

  1. Despite the verbage in perlfaq4, cmpStr and cmpStrHard return the same thing on success/failure. I'm not sure what the difference is supposed to be between these two functions.
  2. I was not actually setting the two hashes to be equal, so my results never changed because the test was always 'failing'. So even when I thought I was inverting the behavior, I wasn't. Also, I'm going to have to use is() instead of ok() since I expect 0. Data::Dumper++ for helping me find the bug in the actual code being tested. (Which, I suppose, was the original point.)

So PodMaster-- for posting and not actually helping. All I needed was confirmation that I was using FreezeThaw correctly. Too bad you couldn't do that, or at least keep silent.

Anyway, here's the only thing I had to change to the original code. Instead of the ok() line I used:

is( cmpStrHard(\%crosslinks, \%crosslinkscorrect), '0', "cmpStrHard" );

So the test now does the right thing. Now I just have to make sure ReadCrossLinks() does the right thing...

--J

Update: Added working version of the code to the bottom of this reply node.

Update 2: The confusion for me was in the text from perlfaq4 that says The first reports that both those the hashes contain the same data, while the second reports that they do not. This let me to believe that cmpStr was not cmpStrHard, and that perhaps there was a typo in the code example. Only after I understood what the two functions do did the phrase make sense.

But that's backwards. The docs are supposed to help me understand the functions, not prove my understanding after the fact. FreezeThaw wasn't any better, since it's highly geared toward OO programming (of which I admit to knowing little).

I think the text in perlfaq4 would read better as:

The first reports that the hashes %a and %b shown contain the same data, while the second reports that they are different, since $a{EXTRA} and $b{EXTRA} point to the same data, but are not equal to each other.

(This assumes that I know what I'm talking about, of course, but I think that's correct.) A little wordier, but much clearer, IMvHO.