title: Perl UTF8 in CGI problems
body: <p>I have a very simple Perl script which works right on the terminal but when run as a CGI script it produces garbage.  The script basically take a HTML entities encoded data and converts it to print it. I have tried all the different setup like using "Encode" to change the output and set the STDOUT to utf8 mode and it does not help. I have also tried to change the environment of CGI to see if things will work like the terminal environment.  Still no luck. </p>
  
  <p>Here is the script</p>
  
  <pre><code>#!/usr/bin/perl 
  use HTML::Entities qw(encode_entities_numeric decode_entities);
  use Encode qw/encode decode/;
  binmode(STDOUT, ":utf8");
  #$ENV{'PERL_UNICODE'} = 'D';
  #$ENV{'LANG'} = 'en_US.UTF-8';
  #$ENV{'TERM'} = 'vt100';
  #$ENV{'SHELL'} = '/bin/bash';
  #binmode(STDOUT, ":utf8");
  print "Content-type: text/html\n\n";
  my $y = decode_entities("&amp;#x3A3;&amp;#x3C5;&amp;#x3C3;&amp;#x3C4;&amp;#x3B7;&amp;#x301;&amp;#x3BC;&amp;#x3B1;&amp;#x3C4;&amp;#x3B1;_&amp;#x
  391;&amp;#x3BD;&amp;#x3B9;&amp;#x301;&amp;#x3C7;&amp;#x3BD;&amp;#x3B5;&amp;#x3C5;&amp;#x3C3;&amp;#x3B7;&amp;#x3C2;_&amp;#x395;&amp;#x3B9;&amp;#x3C3;.pd
  f");
  #print encode("UTF8",$y);
  print $y;
  </code></pre>
  
  <p>The output on terminal it is clean like
  perl test.pl
  Content-type: text/html</p>
  
  <p>Συστήματα_Ανίχνευσης_Εισ.pdf </p>
  
  <p>But on the CGI print it is garbled
  Î£Ï…ÏƒÏ„Î·ÌÎ¼Î±Ï„Î±_Î‘Î½Î¹ÌÏ‡Î½ÎµÏ…ÏƒÎ·Ï‚_Î•Î¹Ïƒ.pdf</p>
  
  <p>I am sort of stuck as I cannot find any simple way to solve this.  Tried  "encode_utf8" and utf8::upgrade of the variable but still no luck.  Anyone's experience here will help a lot!</p>
  
  <p>Thanks
  Vijay</p>