in reply to Replacing the last line in a file

Are you sure you wouldn't rather use an external data file? Self-modifying scripts are not for the faint of heart. I've heard more than once on this site that it's generally a Bad Idea (TM). If you're just using the __DATA__ handle at the end of the script, you might as well use an external file.