You should be using an editor more suited for the job. I recommend something like Smultron although you could look into using something like TextMate, BBEdit, TextWrangler or even Eclipse with EPIC (Perl dev env for Eclipse).
The problem with TextEdit is that it is a rich text editor which will greatly problematize your code as the files you save will be saved with rich text. You need to save your files as plain text which is easily done with the aforementioned editors.
Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.