in reply to Substituting data
you seem to have mixed up the order of s/PATTERN/RELACEMENT/ but that was already told you. I just cannot stop myself from trying to give good advice ...
Have a look at perlfaq4 'What's wrong with always quoting "$vars"'.
I also wonder why you open the same file twice and why you do not iterate via while(<HANDLE>) which would save the memory consumed by the array @dat .