in reply to Change a line in a file

It looks to me like you've mixed up the usage of $file and $trace. $trace looks like it contains the contents, but you are using it as the filename passed to open just after 'now start testing >>>>'. Previously, you used $file.