in reply to Error opening file handle within while loop
This is always worth a read Basic debugging checklist as it contains lots of useful suggestions.
BTW, I'm surprised that your code works, you write to m2.maf & then ask phastCons to read it, but you haven't closed the file. It may be better to close it first so that any buffering gets flushed, and you'll be sure that the program gets to see all the data.
In Section
Seekers of Perl Wisdom