in reply to using binmode() to override default encoding specified in "use open"
I see the same as you in my local Perl (v5.20.3). Inspecting the layers shows that binmode adds to the defaults, rather than replacing them. You need to call if first with no layers in order to do a full reset. (Note: I've corrected the argument to encoding too)
#!/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 my @layers = PerlIO::get_layers(FILE); print "Layers before binmode: @layers\n"; binmode(FILE, ':encoding(UTF-8)') if 1; # override the default enco +ding under certain conditions @layers = PerlIO::get_layers(FILE); print "Layers after binmode: @layers\n"; binmode(FILE) if 1; # reset to raw binmode(FILE, ':encoding(UTF-8)') if 1; # add our new encoding @layers = PerlIO::get_layers(FILE); print "Layers after reset: @layers\n"; warn "About to print"; # primitive trace statement print FILE "$s";
And the output to terminal is
Layers before binmode: unix perlio encoding(iso-8859-1) utf8 Layers after binmode: unix perlio encoding(iso-8859-1) utf8 encoding(u +tf-8-strict) utf8 Layers after reset: unix perlio encoding(utf-8-strict) utf8 About to print at /tmp/11119633.pl line 20.
There may be a neater way but this is at least a working solution, AFAICT.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: using binmode() to override default encoding specified in "use open"
by choroba (Cardinal) on Jul 22, 2020 at 13:24 UTC | |
|
Re^2: using binmode() to override default encoding specified in "use open"
by raygun (Scribe) on Jul 22, 2020 at 11:34 UTC | |
by hippo (Archbishop) on Jul 22, 2020 at 12:25 UTC | |
by jcb (Parson) on Jul 23, 2020 at 04:21 UTC | |
by raygun (Scribe) on Jul 23, 2020 at 15:19 UTC | |
by jcb (Parson) on Jul 23, 2020 at 23:53 UTC |