to print to a file instead of printing at the screen, we just have to change:
print $OUT_FILE $text;
Well, that's ignoring the most important distinction between say
: that say
adds a newline at the end. And you can
add a filehandle argument to say
. So you'd better do:
say $OUT_FILE $text;
If you set
$OUT_FILE = \*STDOUT;
$OUT_FILE = select;
then you don't even have to swap code out code.
As far as your problem concerns: look at Perl I/O Layers, in particular the :utf8 and :encoding layers.