How to print this string properly in Perl?

Tag: perl Author: a760553156 Date: 2012-02-10

I want to print out this string:

[2/16/2012] emailed...I honestly do not know - I am an endpoint in sales - I would try contacting our corporate office. <STOP>

When I try to print this string, this is what it looks like in the terminal:

[2/16/2012] emailed...I honestly do not know û I am an endpoint in sales û I would try contacting our corporate office. <STOP>

How do I get the terminal to print "-" characters instead of "û" characters?

Best Answer

The terminal is trying to use the OEM code page (typically 437 or 850), in which character 151 is indeed a ù. You need to do two things:

  1. Change the console to use a Unicode font, instead of raster fonts (which always use the OEM code page)
  2. Change the code page to (say) 1252

comments:

On the DOS terminal, I can only select Lucida font instead of Raster fonts. There doesn't seem to be an option for changing the code page. How do I do this?
@Andrew the CHCP command changes the code page.

Other Answer1

You might try to change the encoding of STDOUT, by something like:

use encoding 'utf8', STDOUT => 'cp850'; # or 'cp1252', change to what your terminal needs

What system do you use, and what locale is set? You may start with just use encoding 'utf8'; as well.

The other way around is replacing these pesky characters with simple dashes. There are many ways to do it, $string_to_print =~ tr/$pesky_character/-/ is probably the best.

comments:

I tried this (both cp850 and cp1252) and I get this error: "\x{fffd}" does not map to cp1252