tomazos has asked for the wisdom of the Perl Monks concerning the following question:

I have a scalar string from an untrusted source and I want to place it in a utf8 web page as verbatim text outside of a PRE tag. Is there a module that with perform the necessary encoding such that any markup and other sillyness will appear as is and not interpretted by the browser?

Update: Yes, that is what I mean. Entity encoding. Will try HTML::Entities. Thanks.

Cheers, Andrew.


Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com

Replies are listed 'Best First'.
Re: Scalar string to web page
by bmann (Priest) on Jun 27, 2005 at 01:38 UTC
    If you mean having the text displayed as entered (including >, <, & and so forth), have a look at HTML::Entities encode_entities function.

    Update:

    use HTML::Entities; print encode_entities '<a href="#">linky</a>'; __END__ Output: &lt;a href=&quot;#&quot;&gt;linky&lt;/a&gt;
Re: Scalar string to web page
by GrandFather (Saint) on Jun 27, 2005 at 01:13 UTC

    HTML::FromText may fill the bill for you.


    Perl is Huffman encoded by design.
Re: Scalar string to web page
by rlucas (Scribe) on Jun 27, 2005 at 01:35 UTC
    There are many ways to do what you describe; you need to decide exactly what it is that you *want*.

    If you want the text to show up verbatim but subject to all of HTML's vagaries (like collapsing whitespace) and render angle brackets as angle brackets, you should proabably entify (entityfy?) the text. Look for HTML entity encoding.

    If you want to put the text reasonably formatted, use a module that will at the very least insert <p> or <br> tags as appropriate for line breaks.

    If you want to allow some HTML markup, like font tags, breaks, rules, etc., but not all, look at a tag filter or scrubber.

    For all of this, keep in mind that if you reasonably expect it to contain utf8 non-ascii characters, you'll want to make sure to let perl know about that by using decode_utf8 or similar function to tag the scalar, so that it doesn't get oddly transcoded (vagueness intentional: I am not an expert on this and it's a very different process from perl 5 to 5.6 to 5.8; your mileage will vary).

Re: Scalar string to web page
by GrandFather (Saint) on Jun 27, 2005 at 01:01 UTC
    Why outside a <pre>?

    Perl is Huffman encoded by design.
Re: Scalar string to web page
by Anonymous Monk on Jun 27, 2005 at 01:18 UTC