in reply to MD5 -- not digest::md5

Oromis92:

In a case like this when you're teaching yourself or improving code with a known solution, it's worthwhile to go ahead and use the original code as a "check" so you can tell when you've got it right and/or wrong. Something like:

use Digest::MD5 qw(md5); my $message = ...blah blah blah...; my $md5_test = my_md5($message); my $md5_chek = md5($message); print "MESSAGE: '$message'\n" . "TEST: '$md5_test'\n" . "CHECK: '$md5_chek'\n"; print "*** MISMATCHED ***\n" unless $md5_test ne $md5_chek; sub my_md5 { ...blah blah blah... }

This way, you know you're passing the same thing to the test code and the reference code. Then you can have some confidence in your test code. (Especially if you use multiple values to verify corner cases, fencepost errors and the like.)

...roboticus

Replies are listed 'Best First'.
Re^2: MD5 -- not digest::md5
by JavaFan (Canon) on Aug 15, 2009 at 17:04 UTC
    I'm not sure what your point is. The OP already knows "it doesn't work". Assuming that Digest::MD5::md5 does "work", I fail to see what your suggestion offers the OP in finding his problem.
      JavaFan:

      The program in the OP doesn't show both his code and the use of the reference code. I've seen far too many cases where a tiny difference is unnoticed (perhaps an extra blank, a fat-fingered character or something) that makes the difference. By calling both the reference function and the new function in the same program it's easier to confirm that the *exact* same data is passed to both.

      In fact, I had that problem occur last year. For my son's Webelos troop we were working on encryption and decryption of messages. A similar mistake I made caused a problem, and we could decrypt only half of the message I had prepared.

      ...roboticus