Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

utf8 bloody chars

by thorvald (Initiate)
on Oct 17, 2015 at 23:45 UTC ( #1145228=perlquestion: print w/replies, xml ) Need Help??

thorvald has asked for the wisdom of the Perl Monks concerning the following question:

Also new in perl. I'm following "Learning Perl" from O'Reilly and want to print greek letters. Here's what I do:

use 5.010; use utf8; $pi_char = chr(OxO3c9); print "Greek letter here $pi_char\n";

However, in the output I get:

Greek letter here

Replies are listed 'Best First'.
Re: utf8 bloody chars
by bitingduck (Chaplain) on Oct 18, 2015 at 00:14 UTC

    Be careful about zeros vs. the letter "O". If I put zeros where you have "O"s I get a lower case omega. It's sometimes hard to tell when reading, but the letter tends to be a bit rounder and the number more ovalized. Back in the day publishers were better about using zeros with slashes through them- I don't recall if O'Reilly has moved away from that.

      Thanks so much to both of you. I had never use utf before, since I have only done numerics so far

Re: utf8 bloody chars
by AnomalousMonk (Archbishop) on Oct 18, 2015 at 00:16 UTC

    Especially if you are a beginner, you should place the statements  use warnings; and  use strict; at the beginning of all your code:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use utf8; ;; my $pi_char = chr(OxO3c9); print qq{Greek letter '$pi_char' here}; " Bareword "OxO3c9" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.
    I.e., "OxO3c9" (letter "O" vice decimal digit "0") is not a hex number, as already noted. See warnings and strict.

    In other words, "Help Perl to help you."

    Give a man a fish:  <%-{-{-{-<

Re: utf8 bloody chars
by shmem (Chancellor) on Oct 18, 2015 at 10:00 UTC

    To interpolate unicode chars into strings you can use also

    print "Greek letter here \x{03c9}\n"; print "Greek letter here \N{U+03c9}\n"; print "Greek letter here \N{GREEK SMALL LETTER OMEGA}\n";

    as stated in perlunicode. If you get the warning Wide character in print, use the -C command line switch at least as -CO (that's an 'oh', not a zero :-). See perlrun.

    You can find official unicode character names at decodeunicode.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: utf8 bloody chars
by Tux (Canon) on Oct 18, 2015 at 13:51 UTC

    Another remark: the use utf8; is only required if the source code contains literal utf-8:

    $ perl -CO -wE'say "\x{03c9}"'
    $ perl -CO -wE'say "ω"'
    $ perl -CO -Mutf8 -wE'say "ω"'

    Enjoy, Have FUN! H.Merijn

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145228]
Approved by kevbot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2023-02-09 11:36 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (45 votes). Check out past polls.