Stephen Ostermiller's Blog

Solve Perl “Wide Character in Print”

Here is a simple Perl program that outputs a capital gamma character (Ɣ): print "\x{194}\n". When you run it, you will probably get a warning from Perl about a "Wide character in print at line 1".

$ perl -e 'print "\x{194}\n"'
Wide character in print at -e line 1.
Ɣ

You can solve this using several different methods.

Use Command Line Flags

The first way to solve this is through command line flags. Perl supports the -C option that allows control over the default character set. (Documentation) There are several options to choose from:

  • -CSDA is the most complete. It instructs Perl to treat standard input, file handles, and command line arguments as "UTF-8" by default.
  • -CS is just for STDIN, STDOUT, and STDERR.
  • -CO is just for STDOUT.
$ perl -CSDA -e 'print "\x{194}\n"'
Ɣ

This also works if you have the Perl program in a file like gamma.pl which writes the character to a file as well as printing it to STDOUT:

#!/usr/bin/perl -CSDA
print "\x{194}\n";
open (FH, ">", "gamma.txt");
print FH "\x{194}\n";
close(FH);
$ ./gamma.pl
Ɣ
$ perl -CSDA gamma.pl
Ɣ
$ cat gamma.txt
Ɣ

Set the mode on each stream

You can individually tell perl to use UTF-8 on particular streams.

  • binmode STDOUT, ":utf8"; handles just STDOUT.
  • use open ":std", ":encoding(UTF-8)"; handles STDOUT, STDERR, and STDIN
  • ">:encoding(UTF-8)" instead of > in an open filehandle call opens the file for writing using UTF-8.
#!/usr/bin/perl
use open ":std", ":encoding(UTF-8)";
print "\x{194}\n";
open (FH, ">:encoding(UTF-8)", "gamma.txt");
print FH "\x{194}\n";
close(FH);
$ ./gamma.pl 
Ɣ
$ perl gamma.pl
Ɣ
$ cat gamma.txt
Ɣ

Use a Module

The utf8::all module sets up UTF-8 as the default for everything it can in Perl: In your program itself, standard input, standard output, file handles characters, command line arguments, and system calls.

If you don't already have it installed, it can be installed using cpan: sudo cpan install utf8::all.

here is gamma.pl:

#!/usr/bin/perl
use utf8::all;
print "\x{194}\n";
open (FH, ">", "gamma.txt");
print FH "\x{194}\n";
close(FH);
$ ./gamma.pl 
Ɣ
$ perl gamma.pl
Ɣ
$ cat gamma.txt
Ɣ
$ perl -e 'use utf8::all; print "\x{194}\n"'
Ɣ
$ perl -Mutf8::all -e 'print "\x{194}\n"'
Ɣ

Suppress the Warnings

Another option is just to suppress the warnings using no warnings 'utf8';. Again here is gamma.pl:

#!/usr/bin/perl
no warnings 'utf8';
print "\x{194}\n";
open (FH, ">", "gamma.txt");
print FH "\x{194}\n";
close(FH);
$ ./gamma.pl 
Ɣ
$ perl gamma.pl
Ɣ
$ cat gamma.txt
Ɣ
$ perl -e 'no warnings "utf8"; print "\x{194}\n"'
Ɣ

Leave a comment

Your email address will not be published.