use strict; use FileHandle; use IO::Compress::Gzip; my $unicode_string = "Smiley Face: \x{263A}\n"; writefile({ 'filename' => "/tmp/out", 'gzip' => 0, 'data' => $unicode_string, }); writefile({ 'filename' => "/tmp/out.gz", 'gzip' => 1, 'data' => $unicode_string, }); sub writefile { my($opts) = @_; my $fh = ($opts->{'gzip'}) ? IO::Compress::Gzip->new( FileHandle->new("> $opts->{'filename'}"), ) : FileHandle->new("> $opts->{'filename'}"); binmode($fh, ':utf8'); print $fh $opts->{'data'}; $fh->close; } __DATA__