Hello fellow monks,

Following up my own question in SOPW, I would like to release a module implementing the following concept.

TL;DR

use Text::Quote::Self qw(quote_text); my $safe = quote_text($user_input); # create a magic string object $safe = Text::Quote::Self->new($user_input); # ditto say $safe; # input as is say $safe->as_uri; # uri-encoded $safe =~ s/foo/bar/; # changes original, permanently my $data = { text => $safe }; # elsewhere in the app say JSON::XS->new->allow_blessed->convert_blessed->encode($data); #text as is local $Text::Quote::Self::Style = "as_html"; say $data->{text}; # now with entities # or in a Template You were searching for <a href="/search?q=[% search_term.as_uri %]">[% search_term %]</a>.

The concept

Say we have an application that produces convoluted data structure which can be presented in multi[ple ways (who said MVC?). Now depending on the presentation, some strings may need to be escaped/quoted, e.g. if we form an HTML document, we'll have to use HTML entities for some characters; but if we send it via JSON, no quoting is needed.

To encode such strings properly, we have to either know what presentation is to be used and pre-encode the strings in the data layer, or know which strings are safe and which aren't in the presentation layer. This breaks encapsulation of presentation and data, respectively.

Now my proposed solution is to create an object with overloaded stringify operation and a set of quoting methods. The preferred method is then chosen based on a package variable. This allows to localize choice to a scope, e.g. set to HTML during template processing subroutine.

The questions

Whatever is done so far is available on github.


In reply to RFC: Text::Quote::Self - A context-based self-quoting facade for unsafe strings by Dallaylaen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.