title: HTML::Entities encoding and single ampersand
body: <p>I'm attempting to use the following line of perl, as described here:  <a href="https://stackoverflow.com/questions/3054971/does-anyone-know-of-a-vim-plugin-or-script-to-convert-special-characters-to-their">Does anyone know of a vim plugin or script to convert special characters to their corresponding HTML entities?</a> - to encode HTML entities in Vim.</p>
  
  <pre><code>%!perl -p -i -e 'BEGIN { use HTML::Entities; use Encode; } $_=Encode::decode_utf8($_) unless Encode::is_utf8($_);  $_=Encode::encode("ascii", $_, sub{HTML::Entities::encode_entities(chr shift)});'
  </code></pre>
  
  <p>It works fine (£ to &amp;pound, curly quotes etc.) except for an ampersand on it's own - &amp; - which is left as it is.</p>
  
  <p>I've tried removing the uf8 decoding, and looked at the CPAN documentation for HTML::Entities.</p>
  
  <p><strong>Answer:</strong></p>
  
  <p>@ZyX has answered the original question, but as others have pointed out in the comments, this is redundant as it's not actually necessary to use HTML entities if you are serving pages with a UTF-8 character set (which I am, both with the meta tag - </p>
  
  <pre><code>&lt;meta charset="utf-8"&gt;
  </code></pre>
  
  <p>and also in the Apache configuration:</p>
  
  <pre><code>AddDefaultCharset utf-8
  </code></pre>
  
  <p>Indeed it's arguably a bad thing adding them in such cases; the filesize is bigger and the text is obfuscated should anyway want to make use of the source code.</p>
  
  <p>It's essential you ensure whatever editor(s) you use to create files are writing them in UTF-8 as well.</p>