in reply to search and replace with a temp file at a certain substr position

It sounds like you may have some other issues, but one thing you should definitely be aware of is that s/// does not return the modified string! So instead of
print NEW s/$ns/"blah"/;
you should do
s/$ns/"blah"/; print NEW $_;