raygun has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.About to print at ./fp line 11. "\x{263a}" does not map to iso-8859-1.
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!
|
|---|