in reply to simple substitution
Note: open OUT,">$file"; the > mode creates the file if it doesn't exist or truncates it if it does. This is why the file is blank.
As to your stated problem, aside from doing odd things with >+ mode and seek, the easiest way would probably be to open the file in read mode (<,or no mode) read the file in to an array doing any alteration, then close the file and open it in write mode and write your array. This isn't very secure though, as if your script crashes right after it opens the file in write mode you just lost all your data. The general way around this is to write the modified file data to a seperate file and then just rename that new file to the filename of the old one.