The point is, after you remove the blanks in $name, you then assign a new value to it before doing the write. The output you're seeing is the result of that second assignment, not the result of the substition.
As xmath points out below, you're copying around a lot of stuff, and it's not clear why. You may need to rethink part of your algorithm.
Seems like it is a little clunky. Not the way I imagined it but it does work. I was thinking more along the lines of:
Aaaa aaaa --> Aaaaaaaa --> 222222222 --> 22222
$name ------> $name -----> $pwd -------> Output
Read file --> Remove spaces -> Transliterate -> Print file
There's more than one way to do it, but only some of them actually work.