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.


In reply to Re: using binmode() to override default encoding specified in "use open" by hippo
in thread 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.