Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Problem with print chr()

by ccarden (Monk)
on Sep 17, 2004 at 17:24 UTC ( [id://391847]=perlquestion: print w/replies, xml ) Need Help??

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

Why would

print "Copyright " . chr(169) . " 2004 ...\n";

come out as

Copyright © 2004 ...
?

Replies are listed 'Best First'.
Re: Problem with print chr()
by Zaxo (Archbishop) on Sep 17, 2004 at 17:35 UTC

    Your code works fine for me, but I'm in latin1 by default. You're getting unicode and I can reproduce it with use encoding 'utf8'; before the print. Try,

    use encoding 'latin1'; print "Copyright " . chr(169) . " 2004 ...\n";

    After Compline,
    Zaxo

      That did it.

      All these years and I've never come across this before. Is it the OS that is set up to use unicode? I imagine it must be a default system setting.

      I notice that if I put the copyright symbol in comments or POD documentation it shows up as unicode, too, so I wonder how I might get it to work there. Any ideas?

      It's just an annoyance, but it's nice knowing what's going on. Thanks for the tip about the encoding.

        Is it the OS that is set up to use unicode? I imagine it must be a default system setting.

        It depends on the OS, and how your user environment is set up, and which perl version you're using -- e.g. on Redhat 9, the default user environment includes "utf8" as part of the locale setting, and perl 5.8.0 in that setup would treat the locale setting as the default encoding for all i/o. A lot of people got tripped up by that, so as of 5.8.1, perl doesn't behave that way anymore.

        You just have to study the relevant details for your particular setup. The perlunicode and perllocale man pages you have there should help to figure out what's going on.

Re: Problem with print chr()
by Aristotle (Chancellor) on Sep 18, 2004 at 02:55 UTC
      I got a warning a couple weeks ago from a friend who saw a wide character in the buffet line, but no, never any such warnings from Perl. Just the ugly output. Thanks for the link!
Re: Problem with print chr()
by sweetblood (Prior) on Sep 17, 2004 at 17:47 UTC
    or try
    printf "Copyright %c 2004\n", 169;

    Sweetblood

      That seems to work only if there is a use encoding 'latin1'; line either before or after (!) the printf statement.

      Thanks, though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-18 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found