Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: HTML::Entities encode_entities for perlmonks [ ]

by kcott (Chancellor)
on Jun 15, 2013 at 09:35 UTC ( #1039095=note: print w/replies, xml ) Need Help??

in reply to HTML::Entities encode_entities for perlmonks [ ]

Using the HTML::Entities documentation:

#!/usr/bin/env perl -l use strict; use warnings; use utf8; binmode(STDOUT => ':utf8'); use HTML::Entities; my $unsafe_chars = "<&>'\"[]\200-\377"; my $string = "<[Here's my \"2\" worth]>"; print $string; print encode_entities($string, $unsafe_chars);


$ <[Here's my "2" worth]> &lt;&#91;Here&#39;s my &quot;2&cent;&quot; worth&#93;&gt;

Update: Oops! just noticed &Acirc; in the output (just before &cent;). Fixed by adding:

use utf8; binmode(STDOUT => ':utf8');
  • use utf8; — because source code contains UTF-8, i.e. the character.
  • binmode(STDOUT => ':utf8'); — doesn't change any of the character entity references but, without it, print $string; gives <[Here's my "2?" worth]> (note the ? instead of ) now that use utf8; has been added.

-- Ken

Replies are listed 'Best First'.
Re^2: HTML::Entities encode_entities for perlmonks [ ]
by Anonymous Monk on Jun 15, 2013 at 09:47 UTC
    Thanks, that looks almost complete, I think cntrl is missing I tried using [:cntrl:] but that didn't work -- OTOH using  my $unsafe_chars = q{\x00-\x1f<&>'"[]\200-\377}; worked

      Fair comment. I hadn't previously used this module so, as indicated, I was working from the doco: I missed the "control chars" part of:

      The default set of characters to encode are control chars, high-bit chars, and the <, &, >, ' and " characters.

      Also, note the update regarding the utf8 pragma.

      -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039095]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-12-15 18:50 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (70 votes). Check out past polls.