The usual answer is that they do not match because they are different. Issues of different character sets, "hidden" spaces at the start or the end or EOL characters may mess up your test. Do a
print "**$elem**\n"; before the test so you can see what you are comparing.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James