I'm not sure if I really understand how things are working, so maybe my idea might be totally wrong.
I'm thinking about introducing (text, md5_checksum) pairs into in the equation. Giving the Perl script the already existent md5_checksums as arguments and getting not only the text, but also the md5_checksum for it as result.
Calculating md5 checksums isn't cheap, but also not really expensive - so, maybe it might work ?