Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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__
First subroutine call succeeds and produces a /tmp/out file with the expected content.
Seoond subroutine call fails with the message:
Wide character in IO::Compress::Gzip::write: at <program_name> line 29.
Line 29 is the 'print' statement.
Documentation suggests IO::Compress::Gzip::binmode is a no-op.
Using "Encode::decode_utf8($opts->{'data'});" doesn't work either.
The second subroutine call produces a valid, compressed as expected /tmp/out.gz file as long as $unicode_string doesn't actually contain any unicode characters.
How do I make this work? I'd much prefer to compress in perl rather than gzip files after writing them, as the real-world code with the issue demonstrated by this minimum-reproducible test case deals with large data volumes and performance is a concern.
2018-03-03 Athanasius removed the question text from the main code block and added paragraph tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Compress::Gzip and unicode
by salva (Canon) on Mar 02, 2018 at 08:49 UTC | |
|
Re: IO::Compress::Gzip and unicode
by Corion (Patriarch) on Mar 02, 2018 at 08:35 UTC | |
|
Re: IO::Compress::Gzip and unicode
by pmqs (Friar) on Mar 02, 2018 at 23:35 UTC |