I seek the wisdom of the Monks in selecting the encoding of an output stream in Perl v5.30.1.

The framework: all I/O should default to a specific encoding, but under certain conditions, one output stream (which may be stdout, a file, or a pipe) may need to have a different encoding.

The default encoding is simple to achieve: perldoc open documents pragmas for this.

Because the output stream under consideration may be STDOUT — which never has an explicit open() — it's more efficient and less repetitive to assign the output encoding via binmode() in one place (after the open() call), rather than including conditionals to either reassign STDOUT's encoding or explicitly specify an encoding at open() time.

The only minor flaw in this plan is that it doesn't work.

#!/usr/bin/perl -w use open qw/:std :encoding(iso-8859-1)/; # default I/O encoding my $s = "A \N{WHITE SMILING FACE} for you\n"; open (FILE, '> fpo'); # in the actual code, may op +en one of several things, or assign STDOUT to FILE binmode(FILE, ':encoding(utf8)') if 1; # override the default encod +ing under certain conditions warn "About to print"; # primitive trace statement print FILE "$s";

This generates the stderr:

About to print at ./fp line 11. "\x{263a}" does not map to iso-8859-1.
and, per the warning, creates an output file with the ASCII string \x{263a} in place of the UTF-8 character. The order of the stderr lines tells me the print call is what generated the mapping warning.

This snippet gives me the desired output if I remove the use open pragma, but then of course I lose the defaults needed for all other cases.

Any insights into the mysteries of I/O encoding are appreciated!


In reply to using binmode() to override default encoding specified in "use open" by raygun

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.