It may seem nonsensical, but perhaps s/\\/\\/g is what you need here - perhaps perl's s/in/out/ operator doesn't do escaping of out ?  If that *is* correct, of course, a comment would be in order !