in reply to String differs from printing to STDOUT and printing to file
To combine the points that rjt and GrandFather made, it'd be best if you inspect your string with Devel::Peek and your file with hexdump; I gave an example here.
In Section
Seekers of Perl Wisdom