in reply to
How to handle encoding for STDERR
Whatever other problems may exist, your open (Ln 9) comes up a little short of a dozen; a few degrees off plumb; or, more directly, wrong:
Your open( my $in, '<', 'non-existing-file' ) || die $!; should be
open( my $in, '<', 'path-to-file-name' ) or die "Can't open path-to-f
(with a [now deprecated, at least by some] bareword and variant quote symbols)
open (OUT, ">", $datafile ) or die "Can't open $datafile for write, $!
# for a write