title: Perl - read file with encoding method?
body: <p>im not too good when it comes to encoding and I am wanting to figure out how to return data as the same encoding it started with...</p>
  
  <p>I have a file with some characters in such as <code>'»'</code> by the time I have edited and and inserted into database they have turned into &Acirc;&amp;raquo.</p>
  
  <p>decode_entities() does nothing and encode_entities encodes the chars again. So i created my own sub to fix that but it apears that when getting the data from the file it isn't retrieving in the right format.</p>
  
  <pre><code>my $file = "c:/perlscripts/" . md5_hex($md5Con) . "-code.php";
  {
      local( $/ ); # undefine the record seperator
      open FILE, "&lt;", $file or die "Cannot open:$!\n";
      my $fileContents = unicodeConvert(&lt;FILE&gt;);
      ...
      .. 
  </code></pre>
  
  <p>is there not a encoding option like;</p>
  
  <pre><code>my $file = "c:/perlscripts/" . md5_hex($md5Con) . "-code.php";
  {
      local( $/ ); # undefine the record seperator
      open FILE, "&lt;", $file or die "Cannot open:$!\n", "UTF-8";
      my $fileContents = unicodeConvert(&lt;FILE&gt;);
      ...
      .. 
  </code></pre>
  
  <p>and my sub is;</p>
  
  <pre><code>sub unicodeConvert($) {
     my $str = shift;
      my %entityRef = ("&amp;" =&gt; "&amp;amp;", '¢' =&gt; "&amp;cent;", '¤' =&gt; "&amp;curren;", '¦' =&gt; "&amp;brvbar;", '¨' =&gt; "&amp;uml;", 'ª' =&gt; "&amp;ordf;", '¬' =&gt; "&amp;not;", '®' =&gt; "&amp;reg;", '°' =&gt; "&amp;deg;", '²' =&gt; "&amp;sup2;", '´' =&gt; "&amp;acute;", '¶' =&gt; "&amp;para;", '¸' =&gt; "&amp;cedil;", 'º' =&gt; "&amp;ordm;", '¼' =&gt; "&amp;frac14;", '¾' =&gt; "&amp;frac34;", 'À' =&gt; "&amp;Agrave;", 'Â' =&gt; "&amp;Acirc;", 'Ä' =&gt; "&amp;Auml;", 'Æ' =&gt; "&amp;AElig;", 'È' =&gt; "&amp;Egrave;", 'Ê' =&gt; "&amp;Ecirc;", 'Ì' =&gt; "&amp;Igrave;", 'Î' =&gt; "&amp;Icirc;", 'Ð' =&gt; "&amp;ETH;", 'Ò' =&gt; "&amp;Ograve;", 'Ô' =&gt; "&amp;Ocirc;", 'Ö' =&gt; "&amp;Ouml;", 'Ø' =&gt; "&amp;Oslash;", 'Ú' =&gt; "&amp;Uacute;", 'Ü' =&gt; "&amp;Uuml;", 'Þ' =&gt; "&amp;THORN;", 'à' =&gt; "&amp;agrave;", 'â' =&gt; "&amp;acirc;", 'ä' =&gt; "&amp;auml;", 'æ' =&gt; "&amp;aelig;", 'è' =&gt; "&amp;egrave;", 'ê' =&gt; "&amp;ecirc;", 'ì' =&gt; "&amp;igrave;", 'î' =&gt; "&amp;icirc;", 'ð' =&gt; "&amp;eth;", 'ò' =&gt; "&amp;ograve;", 'ô' =&gt; "&amp;ocirc;", 'ö' =&gt; "&amp;ouml;", 'ø' =&gt; "&amp;oslash;", 'ú' =&gt; "&amp;uacute;", 'ü' =&gt; "&amp;uuml;", 'þ' =&gt; "&amp;thorn;", '¡' =&gt; "&amp;iexcl;", '£' =&gt; "&amp;pound;", '¥' =&gt; "&amp;yen;", '§' =&gt; "&amp;sect;", '©' =&gt; "&amp;copy;", '«' =&gt; "&amp;laquo;", '¯' =&gt; "&amp;macr;", '±' =&gt; "&amp;plusmn;", '³' =&gt; "&amp;sup3;", 'µ' =&gt; "&amp;micro;", '·' =&gt; "&amp;middot;", '¹' =&gt; "&amp;sup1;", '»' =&gt; "&amp;raquo;", '½' =&gt; "&amp;frac12;", '¿' =&gt; "&amp;iquest;", 'Á' =&gt; "&amp;Aacute;", 'Ã' =&gt; "&amp;Atilde;", 'Å' =&gt; "&amp;Aring;", 'Ç' =&gt; "&amp;Ccedil;", 'É' =&gt; "&amp;Eacute;", 'Ë' =&gt; "&amp;Euml;", 'Í' =&gt; "&amp;Iacute;", 'Ï' =&gt; "&amp;Iuml;", 'Ñ' =&gt; "&amp;Ntilde;", 'Ó' =&gt; "&amp;Oacute;", 'Õ' =&gt; "&amp;Otilde;", '×' =&gt; "&amp;times;", 'Ù' =&gt; "&amp;Ugrave;", 'Û' =&gt; "&amp;Ucirc;", 'Ý' =&gt; "&amp;Yacute;", 'ß' =&gt; "&amp;szlig;", 'á' =&gt; "&amp;aacute;", 'ã' =&gt; "&amp;atilde;", 'å' =&gt; "&amp;aring;", 'ç' =&gt; "&amp;ccedil;", 'é' =&gt; "&amp;eacute;", 'ë' =&gt; "&amp;euml;", 'í' =&gt; "&amp;iacute;", 'ï' =&gt; "&amp;iuml;", 'ñ' =&gt; "&amp;ntilde;", 'ó' =&gt; "&amp;oacute;", 'õ' =&gt; "&amp;otilde;", '÷' =&gt; "&amp;divide;", 'ù' =&gt; "&amp;ugrave;", 'û' =&gt; "&amp;ucirc;", 'ý' =&gt; "&amp;yacute;", 'ÿ' =&gt; "&amp;yuml;");
      while( ( my $key, my $obj ) = each( %entityRef ) ) {
          if( $key ne '&amp;' ) {
                  $str =~ s/$key/$obj/gis
          } else {
                  $str =~ s#&amp;((?!(quot;)|(amp;)|(cent;)|(curren;)|(brvbar;)|(uml;)|(ordf;)|(not;)|(reg;)|(deg;)|(sup2;)|(acute;)|(para;)|(cedil;)|(ordm;)|(frac14;)|(frac34;)|(Agrave;)|(Acirc;)|(Auml;)|(AElig;)|(Egrave;)|(Ecirc;)|(Igrave;)|(Icirc;)|(ETH;)|(Ograve;)|(Ocirc;)|(Ouml;)|(Oslash;)|(Uacute;)|(Uuml;)|(THORN;)|(agrave;)|(acirc;)|(auml;)|(aelig;)|(egrave;)|(ecirc;)|(igrave;)|(icirc;)|(eth;)|(ograve;)|(ocirc;)|(ouml;)|(oslash;)|(uacute;)|(uuml;)|(thorn;)|(iexcl;)|(pound;)|(yen;)|(sect;)|(copy;)|(laquo;)|(macr;)|(plusmn;)|(sup3;)|(micro;)|(middot;)|(sup1;)|(raquo;)|(frac12;)|(iquest;)|(Aacute;)|(Atilde;)|(Aring;)|(Ccedil;)|(Eacute;)|(Euml;)|(Iacute;)|(Iuml;)|(Ntilde;)|(Oacute;)|(Otilde;)|(times;)|(Ugrave;)|(Ucirc;)|(Yacute;)|(szlig;)|(aacute;)|(atilde;)|(aring;)|(ccedil;)|(eacute;)|(euml;)|(iacute;)|(iuml;)|(ntilde;)|(oacute;)|(otilde;)|(divide;)|(ugrave;)|(ucirc;)|(yacute;)|(yuml;)|(nbsp;)))#$obj#gis;   
          }
      }
      return $str;
  }
  </code></pre>