If you are trying to work with specific password files, then you may want to have a look at any of the vast number of perl modules that already have this functionality created. If the password is a *nux password file, then take a look at
Passwd::Linux,
Passwd::Solaris, or
Unix::PasswdFile to name just a few. If it's for apache, check out
Apache::Htpasswd. It would also be helpful if you could have provided us with some example data.