Take the 2-minute tour ×

I'm generating a Word document in Perl, and I'd like to include the degree symbol (°) in the text I generate. If I generate the code like so:

$cell .= qq/\xB0/;

This works, and generates (for a value of $cell of 55): 55°

However, perlcritic complains at me when I do this and suggests I use this construction instead:

$cell .= qq/\N{DEGREE SIGN}/;

This does not work; it generates: 55Â°

Looking through my code in perl -d, I see that running the following code:

my $cell = 55;
$cell .= qq/\N{DEGREE SIGN}/; # the PBP way
print sprintf("%x\n", ord($_)) for split //, $cell;

my $cell = 55;
$cell .= qq/\xB0/; # the non-PBP way

results in:


I'm outputting text to the Word document using Win32::OLE:

my @column_headings = @{ shift $args->{'data'} };
my @rows            = @{ $args->{'data'} };

my $select = $word->Selection;

$select->InsertAfter( $csv->string );
for my $row (@rows) {
    $csv->combine( @{$row} );
my $table = 
  $select->ConvertToTable( { 'Separator' => wdSeparateByCommas } );
$table->Rows->First->Range->Font->{'Bold'} = 1;
$table->Rows->First->Range->ParagraphFormat->{'Alignment'} =
@{ $table->Rows->First->Borders(wdBorderBottom) }{qw/LineStyle LineWidth/}
  = ( wdLineStyleDouble, wdLineWidth100pt );
$doc->SaveAs( { 'Filename' => Cwd::getcwd . '/test.doc' } );

What can I do to get rid of the extraneous Â?

share|improve this question
Sounds like an encoding issue. I'm still trying to figure out encoding myself but you might try setting your output file to UTF-8. This article might help too: forums.devshed.com/perl-programming-6/… –  scrappedcola Feb 7 '13 at 16:07
@scrappedcola It probably is some kind of encoding issue - originally, I was using the ASCII degree sign, but I found that Word didn't interpret that as such. –  Kit Peters Feb 7 '13 at 16:29
See @ikegami's comment to the answer for the solution to the problem. –  Kit Peters Feb 7 '13 at 17:05

1 Answer 1

up vote 1 down vote accepted

Of course, you are suffering from encoding issues. The degree sign is U+00B0, but this serializes to UTF-8 C2 B0, which renders as ° — if this multi byte character is correctly decoded as utf-8. If you were decoding the bytes as a single-byte encoding (say … cp1252), then the bytes would be considered seperate, and would display Â °.

Now clearly, the solution is either to tell Perl to transform the unicode string to a byte string of cp1252 chars (the horror!). You will find the my $bytestring = Encode::encode("cp1252", $string) function interesting here.

Or you tell the document that it will consider itself UTF-8. I don't know how you would do that, but there has to be an option somewhere. This would actually be preferable, as there are thousands of characters that (unlike the °) don't fit into cp1252. Like the degree Celsius ℃ (U+2103) or degree Fahrenheit ℉ (U+2109) characters ;-)

share|improve this answer
Re your last paragraph, you'd have to use the "W"IDE interface to talk to Word instead of the "A"NSI interface. That's a decision out of your hands. –  ikegami Feb 7 '13 at 16:38
Actually, I think you can do Win32::OLE->Option(CP => Win32::OLE::CP_UTF8)! Then you'd use my $bytestring = Encode::encode("UTF-8", $string);. Untested. I hope it works! –  ikegami Feb 7 '13 at 16:43
@ikegami Setting the codepage took care of it, and there was no need to call Encode::encode(). You are full of helpfulness. –  Kit Peters Feb 7 '13 at 17:04
No, you do need to call encode or it won't work for some strings. In fact, not calling encode should give you a "Wide character" error. (It's a bug in Win32::OLE that it doesn't.) –  ikegami Feb 7 '13 at 17:20

Your Answer


