I'm creating a web front end for automated testing that allows users to enter commands and compare them against expected results. I'm storing these commands and expected results as json strings in a database. The commands usually contain normal strings or TT2 templates, while the expected results can contain normal strings, tt2 templates and/or regular expressions.

I ran into a problem when I tried to include a % PERL % block inside my expected results template because it contains escaped characters and double quoted strings.
cmd>statistics standard summary result>ORIG ATTMPT ORIG COMPL TERM ATTMPT TERM COMPL + Summary 2555 2543 2543 2543
#Inline Perl [% PERL %] my $result = $stash->get('result'); my ($orig_attmpt, $orig_compl, $term_attmpt, $term_compl) = ($result + =~ /Summary.*(\d)\s+(\d)\s+(\d)\s+(\d)/ ); if (($orig_compl/$orig_attmpt) > 0.95) { print 1; } else { print 0; +} [% END %] OR #More Advanced Template [% matches = result.match('Summary\s+(\d)\s+(\d)\s+(\d)\s+(\d)') %] [% IF match.2/match.1 > 0.95 %]1[% ELSE %]0[% END %]
So, after all that, I guess my question is, what is the best way to use JSON.pm (JSON::XS backend) to allow for encoding and decoding of these strings while maintaining the correct structure for regex escaping, etc..?
BEGIN { $ENV{PERL_JSON_BACKEND} = 2 } # with JSON::XS use JSON qw( -support_by_pp -convert_blessed_universally ); my $json = new JSON; $json->allow_nonref->allow_blessed->convert_blessed->loose;

In reply to JSON.pm, TT2 and perl code inside json by josh803316

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.