in reply to Reading in utf-8 txt file gives garbled data when printed as part of utf-8 html...

You need to open the $file as utf-8 (or set the utf-8 layer using binmode), and set the STDOUT mode to utf-8 too:
binmode(STDOUT,":utf8"); open(FILE,"<:utf8", "$file"); my @Lines = <FILE>; close(FILE); $page = "<html>". join("",@Lines) ."</html>"; print "Content-Type: text/html; encoding=utf-8\n\n"; print $page;
See also perlunicode

update: also perlio - and note that you should never set the :raw layer (i.e. use binmode(FILEHANDLE) - without a second argument) on a (unicode) text file unless you're sure you know what you're doing.

  • Comment on Re: Reading in utf-8 txt file gives garbled data when printed as part of utf-8 html...
  • Download Code

Replies are listed 'Best First'.
Re^2: Reading in utf-8 txt file gives garbled data when printed as part of utf-8 html...
by isync (Hermit) on Aug 27, 2007 at 23:24 UTC
    Woa! Got it!! Was a stupid error:
    open(FILE,"<:utf8", "$file"); binmode(FILE);

    Your hint "never set the :raw layer (i.e. use binmode(FILEHANDLE) - without a second argument)" in mind I could spot this double-declaration in my code. The problem is always in front of the screen...
Re^2: Reading in utf-8 txt file gives garbled data when printed as part of utf-8 html...
by isync (Hermit) on Aug 27, 2007 at 23:06 UTC
    Doh! I need to engrave this :utf8 thing on filehandles in wood! I forget it all the time... But, it did not quite solve the problem. So I tried Ikegami's suggestion (see below).