title: Why am I getting the last octet repeated when my Perl program outputs a UTF-8 encoded string in cmd.exe?
body: <h3>Update</h3>
  
  <p>As @ikegami suggested, I reported this as a bug.</p>
  
  <p><a href="https://rt.perl.org/Public/Bug/Display.html?id=121783" rel="noreferrer">Bug #121783 for perl5: Windows: UTF-8 encoded output in cmd.exe with code page 65001 causes unexpected output</a></p>
  
  <p>Consider the following C and Perl programs which both output a the UTF-8 encoding of the string "&alpha;&beta;&gamma;" on standard output:</p>
  
  <h3>C version:</h3>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      /* UTF-8 encoded alpha, beta, gamma */
      char x[] = { 0xce, 0xb1, 0xce, 0xb2, 0xce, 0xb3, 0x00 };
      puts(x);
      return 0;
  }
  </code></pre>
  
  Output:
  
  <pre>C:\&hellip;> chcp 65001
  Active code page: 65001
  
  C:\&hellip;> cttt.exe
  αβγ</pre>
  
  <h3>Perl version:</h3>
  
  <pre>C:\&hellip;>  perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3\n}"
  αβγ
  �</pre>
  
  <p>From what I can tell, the last octet, <code>0xb3</code> is being output again, on another line, which is being translated to <code>U+FFFD</code>.</p>
  
  <p>Note that redirecting output eliminates this effect.</p>
  
  <p>I can also verify that it is the last octet being repeated:</p>
  
  <pre>C:\&hellip;>  perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3xyz\n}"
  αβγxyz
  z</pre>
  
  <p>On the other hand, <a href="http://perldoc.perl.org/functions/syswrite.html" rel="noreferrer">syswrite</a> avoids this problem.</p>
  
  <pre>C:\&hellip;>  perl -e "syswrite STDOUT, qq{\xce\xb1\xce\xb2\xce\xb3xyz\n}"
  αβγxyz</pre>
  
  <p>I have observed this in cmd.exe windows on Windows 8.1 Pro 64-bit and Windows Vista Home 32-bit using both self-built perl 5.18.2 and ActiveState's 5.16.3.</p>
  
  <p>I do not see the problem in Cygwin, Linux, or Mac OS X environments. Also, Cygwin's perl 5.14.4 produces correct output in cmd.exe.</p>
  
  <p>Also, when the code page is set to 437, the output from both the C and the Perl versions is identical:</p>
  
  <pre>C:\&hellip;> chcp 437
  Active code page: 437
  
  C:\&hellip;> cttt.exe
  ╬▒╬▓╬│
  
  C:\&hellip;>  perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3\n}"
  ╬▒╬▓╬│</pre>
  
  <p>What is causing the last octet to be output twice when printing from perl program in cmd.exe when the <a href="http://msdn.microsoft.com/en-us/library/dd317756%28VS.85%29.aspx" rel="noreferrer">code page is set to 65001</a>?</p>
  
  <p>PS: I have some more information and screenshots on <a href="http://blog.nu42.com/2014/05/utf-8-ouput-from-perl-and-c-programs-in.html" rel="noreferrer">my blog</a>. For this question, I have tried to distill everything to the simplest possible cases.</p>
  
  <p>PPS: Leaving out the <code>\n</code> results in something even more interesting:</p>
  
  <pre>C:\&hellip;> perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3xyz}"
  αβγxyzxyz</pre>
  
  <pre>C:\&hellip;> perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3}"
  αβγ�γ�</pre>