in reply to Re^2: HTML::TokeParser, get_text scrambling rsquo and lsquo
in thread HTML::TokeParser, get_text scrambling rsquo and lsquo

Check the LANG setting at your server and at home, I bet they are different. Or it is that the web server's charset defaults to utf-8. Check the config.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
  • Comment on Re^3: HTML::TokeParser, get_text scrambling rsquo and lsquo

Replies are listed 'Best First'.
Re^4: HTML::TokeParser, get_text scrambling rsquo and lsquo
by tridral (Initiate) on May 12, 2007 at 10:06 UTC
    Thank you. At home I have

    LANG=en_GB.UTF-8

    The server has

    LANG=en_US

    I'm not sure what to do with this information and whether it is significant.

    Can you give me a pointer to the web server's configuration file? (or should I ask the web host people about this)

      Ah, so it is the other way round, and very odd indeed. Runinng your code I get the same result, and I don't have UTF-8 in my LANG setting, either. It seems that HTML::TokeParser turns on the UTF-8 flag on strings returned by the get_text() method:
      #!/usr/bin/perl use HTML::TokeParser; #use Data::Dump::Streamer; use strict; use Devel::Peek; local $/; my $lines = <DATA>; my $tok_par = HTML::TokeParser->new(\$lines); my $tok_inf = $tok_par->get_token ; my $tok_typ = shift @{$tok_inf}; my $title = $tok_par->get_text() || "<NO TITLE FOUND>"; Dump ($title); __DATA__ <title>egrave: &egrave; : eacute: &eacute; : rsquo: &rsquo; : lsquo: & +lsquo;</title> __END__ SV = PV(0x81b4290) at 0x81ed950 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x8207b90 "egrave: \303\250 : eacute: \303\251 : rsquo: \342\20 +0\231 : lsquo: \342\200\230"\0 [UTF8 "egrave: \x{e8} : eacute: \x{e9} + : rsquo: \x{2019} : lsquo: \x{2018}"] CUR = 49 LEN = 52

      - that's why you see the right output on your UTF-8 terminal at home, but garbled stuff on the servers terminal.

      Hmm. I call that a bug :-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        That's not a bug, that's the correct behaviour. :-)

        Perl (modules) can switch to switch to utf-8 for any string. Since lsquo and rsquo aren't available in the default charset (latin-1) that's probably the only reasonable thing to do in this case.

        The only issue is with output/input. As long as you have your handles open in the mode with the correct character encoding (and that encoding supports the characters you're trying to print) it should all just work. (Though having the latest perl helps a lot - unicode support is still getting better).

        As a side note: the latest perls do not open STDOUT/STDIN with utf-8 encoding even if you have a UTF-8 LANG setting.

        update: side side note: just to be clear: $ENV{LANG}, "use utf8", binmode() etc strictly speaking say nothing about whether the strings will get the utf-8 flag and you really shouldn't rely on it when they do. The only time you can be sure the utf-8 flag will be on is when you insert characters outside the \x00 - \xff range.