I am trying to write a Perl script using the "utf8" pragma, and I'm getting unexpected results. I'm using Mac OS X 10.5 (Leopard), and I'm editing with TextMate. All of my settings for both my editor and operating system are defaulted to writing files in utf-8 format.
However, when I enter the following into a text file, save it as a ".pl", and execute it, I get the friendly "diamond with a question mark" in place of the non-ASCII characters.
#!/usr/bin/env perl -w
use strict;
use utf8;
my $str = 'Çirçös';
print( "$str\n" );
Any idea what I'm doing wrong? I expect to get 'Çirçös' in the output, but I get '�ir��s' instead.
You also want to say, that strings in your code are utf-8. See Why does modern Perl avoid UTF-8 by default?. So set not only
PERL_UNICODE=SDAL
but alsoPERL5OPT=-Mutf8
.do in your shell: $ env |grep LANG
This will probably show that your shell is not using a utf-8 locale.
You can use the open pragma.
For eg. below sets STDOUT, STDIN & STDERR to use UTF-8....
Thanks, finally got an solution to not put utf8::encode all over code. To synthesize and complete for other cases, like write and read files in utf8 and also works with LoadFile of an YAML file in utf8
where cache.yaml is:
use utf8;
does not enable Unicode output - it enables you to type Unicode in your program. Add this to the program, before yourprint()
statement:See if that helps. That should make
STDOUT
output in UTF-8 instead of ordinary ASCII.TMTOWTDI, chose the method that best fits how you work. I use the environment method so I don't have to think about it.
In the environment:
on the command line:
or with binmode:
or with PerlIO:
or with the open pragma: