in reply to Writing to a file: problem

You might consider using the -i switch to the perl interpreter for such editing. Type perldoc perlrun for documentation.