Have you tried the following from the commandline?
perl -MDigest::MD5 -e'print Digest::MD5::md5_hex("user:realm:password")'
(that should be all on one line)? It should produce the MD5 password string. If that works, but the following into your perl script:
use Digest::MD5;
...
my $MDpasswd=Digest::MD5::md5_hex("$user:$realm:$password");
where $user is the 'user' variable generated within your script, $realm is the 'realm' variable generated within your script and $password is the plain text password generated within your script (perhaps the code between 'use Digest...' and 'my $MDpasswd=...' opens up the files you were mentioning and reads from it the user, realm and uncoded passwords).
Hope this helps