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


In reply to IO::Compress::Gzip and unicode by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.