Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number


by Yoda_Oz (Sexton)
on Jan 11, 2007 at 15:22 UTC ( [id://594176]=perlquestion: print w/replies, xml ) Need Help??

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

why is it when i print a (pound) symbol out in perl it comes out as a ? (question mark)?
its making it very difficult for me!
UPDATE... I'm using Mac OS X, if that makes any difference.

Replies are listed 'Best First'.
Re: symbols
by Tux (Canon) on Jan 11, 2007 at 15:34 UTC
    Is the text where ₤ is part of coded UTF8 or something else?
    Do you have warnings enabled?
    Did you explicitely encode/decode the string?
    Did you use binmode STDOUT, ":utf8";?
    Is your terminalwindow capable of printing a ₤?
Re: symbols
by SheridanCat (Pilgrim) on Jan 11, 2007 at 15:38 UTC
    First start with Juerd's perlunitut to get a grasp of handling Unicode in Perl.

    You'll also need to make sure your terminal is set to handle UTF-8 characters. You're on a Mac, so I'm guessing you're okay with that. On Windows you'd have to do "chcp 65001" at the command prompt to get proper output.

    The following works for me:

    use Encode; print encode_utf8( "" );
Re: symbols
by Melly (Chaplain) on Jan 11, 2007 at 15:35 UTC

    The pound symbol is part of the extended ascii set, which is why it causes you problems.... try

    print chr(156)

    (156 is the ascii value for '')

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly,
      But the pound is not 156 in every coding :)
      e.g. the utf8 codepoint for the pound is U00A3, which is 163 in dec, not 156.

      See pound sign

      It is 163 (0x00a3) in 8859-1, 8859-13, 8859-14, 8859-15, 8859-3, 8859-7, 8859-8, 8859-9, CP1252, CP1253, CP1254, CP1255, CP1256, CP1257, CP1258, CP864, ROMAN, SAMI_WIN, SAMI_MAC, and CENTEURO
      It is 156 (0x009c) in CP437, CP775, CP850. CP857, CP860, CP861, CP862, CP863, CP865, CP869, CP1116, and VENTURA_INT
      It is 177 (0x00b1) in CP1122

      Maybe it is better to use UTF8 after all
      # perl -C2 -le'use charnames qw(:full); print "\N{POUND SIGN}"'
      In most character sets, including iso-8859-1 and Unicode, the pound symbol is 163 decimal, or 0xA3 hexadecimal, so you should use "chr(163)". If you use the correct encoding on the output, that will be translated to the correct character.

      E.g. this

      perl -e 'binmode STDOUT, ":encoding(cp437)"; print chr(0xA3), "\n"' \ | od -c -t x1 0000000 234 \r \n 9c 0d 0a
      (Code page 437 is a common code page for a DOS box on Windows, but it can be changed with CHCP.)

      and this

      perl -e 'binmode STDOUT,":encoding(latin-1)"; print chr(0xA3),"\n"' \ | od -c -t x1
      0000000 243 \r \n a3 0d 0a

      But, note that 156 (0x9c) does not work with the Encode translations:

      perl -e 'binmode STDOUT, ":encoding(cp437)"; print chr(0x9C), "\n"' \ | od -c -t x1
      "\x{009c}" does not map to cp437. 0000000 \ x { 0 0 9 c } \r \n 5c 78 7b 30 30 39 63 7d 0d 0a

      If you are outputting to HTML, you also have to make sure the Content-type charset gets set correctly or output the characters as entities, e.g. £ or £

      156 is not the ASCII value for anything. ASCII is 7 bit, and only goes to 127.

      163 in Unicode and Latin-1 (aka ISO-8859-8) is the pound sign.

Re: symbols
by graff (Chancellor) on Jan 11, 2007 at 22:24 UTC
    why is it when i print a (pound) symbol out in perl it comes out as a ? (question mark)?

    The answer depends on a lot of things. Since you're using macosx, I'll assume you're using the nifty Terminal app, with the character set encoding configured to the default (utf8 -- if you check the "Window preferences / Display" menu, you can change that setting, e.g. to "ISO Latin 1", if you want to).

    If your terminal window is using utf8, then you should see a (pound) symbol rather than a question mark if you do this:

    binmode STDOUT, ":utf8"; print "Here is the pound sign: \xA3\n";
    If your text containing the pound sign is coming from some external source, then you have to know what encoding is being used by that source -- then either convert it to utf8, or not, but make sure that the character encoding of the agrees with that of the output file handle.

    If you don't want to set STDOUT to utf8 like that, then you'll probably want to set your terminal to use ISO Latin 1 (as explained above).

    If the output file handle has not been set to utf8 mode, then byte/character values in the range 128-255 (0x80-0xff) will be output as-is without conversion to utf8 encoding, and in that case, you just want to be sure that the whatever uses that output knows to expect non-utf8 data.

    But if you want ut8 output, you have to tell perl that the file handle is supposed to do that (using binmode).

    (updated to finish the paragraph that followed the code snippet).

      my terminal is set to utf8 and when i type in a pound sign it get "\302\243".

      i cant actually get the terminal to display a sign no matter what encoding!
        Geez. You are using "", right? What font are you using? I use Monaco, and I see the symbol just fine. In case it helps, here are what I guess would be the relevant shell environment variables -- if you type "env" as a command, you should see similar things (I don't actually know what TERM_PROGRAM_VERSION and __CF_USER_TEXT_ENCODING are about):
        $ env TERM_PROGRAM=Apple_Terminal TERM=xterm-color SHELL=/bin/bash TERM_PROGRAM_VERSION=133 __CF_USER_TEXT_ENCODING=0x3B28:0:0 ... $ perl -v This is perl, v5.8.6 built for darwin-thread-multi-2level (with 3 registered patches, see perl -V for more detail) ... $ perl -e 'binmode STDOUT,":utf8"; print "sign: \xa3\n"' sign:
        The fact that you're seeing just the escaped-octal-digit rendition of the byte values suggests that it's not strictly a perl problem.
Re: symbols
by halley (Prior) on Jan 11, 2007 at 19:55 UTC

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-07-13 10:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.