Perl相当于htmlentities()

I am new to Perl.

I am trying to duplicate some/most of the functionality of PHP's htmlentities()

So far I have this:

$str = '" \' < >';

$str = join('&lt;', split('<', $str));
$str = join('&gt;', split('>', $str));
$str = join('&quot;', split('"', $str));
$str = join('&#039;', split("'", $str));

print $str;

It is working for my needs but the micro-optimizer in me just feels like there must be a better way.

I am terrible with regex and so many Perl tutorials deal with custom or non-standard libraries or it just seems this way due to my inexperience.

perl -v produces:

v5.10.1

Use HTML::Entities's encode_entities. (It obviously won't be 100% equivalent since the beavhour of htmlentities varies based on the arguments passed to it.)

Hey what you asked for can be achieved by using decode_entities()

Example code:-

use HTML::Entities; my $html = "Hey &amp; &lt"; print decode_entities($html), "
";

Use this as a reference :- Reference