title: How to convert symbols "&'<> to entities &quot; &amp; &apos; &lt; &gt; in Perl
body: <p>What is the simplest method in Perl to convert special symbols <code>"&amp;'&lt;&gt;</code> to entities <code>&amp;quot; &amp;amp; &amp;apos; &amp;lt; &amp;gt;</code> in Perl? It is easy to write functions like this, but I think this problem has been solved a lot of times and there is no need to write your own functions.</p>
  
  <pre><code>sub add_entities {
      my ($text) = @_;
  
      $text =~ s/&amp;/&amp;amp;/g;
      $text =~ s/"/&amp;quot;/g;
      $text =~ s/'/&amp;apos;/g;
      $text =~ s/&lt;/&amp;lt;/g;
      $text =~ s/&gt;/&amp;gt;/g;
  
      return $text;
  }
  
  sub remove_entities {
      my ($text) = @_;
  
      $text =~ s/&amp;quot;/"/g;
      $text =~ s/&amp;amp;/&amp;/g;
      $text =~ s/&amp;apos;/'/g;
      $text =~ s/&amp;lt;/&lt;/g;
      $text =~ s/&amp;gt;/&gt;/g;
  
      return $text;
  }
  </code></pre>