Hello Monks,

In some reporting code, I've decided to implement some simple markup to alleviate much of the tedium of including frequently used dynamic text in string outputs. It lets me turn something like this:

$obj->print(sprintf("Page %d - Section %d - %s", $obj->current_page, $obj->section->num, $obj->section->name, ) );

Into this (short codes shown here; they can also be spelled in full):

    $obj->print("Page ^(pg) - Section ^(sec:num) - ^(sec)");

(N.B.: I contrived this example so its purpose is immediately obvious. It still shows the expressiveness I want and bonus ability to re-use the same input string later with different results. The actual codes are extremely domain-specific.)

I've already written code for this that works, but my question is: what do you monks feel is the best syntax to allow escaping of a literal ^(foo) string? Suppose ^(foo) interpolates to 'BAR'. Here are the three options I thought of:

Option A - Backslash escapes

(Try to emulate Perl escapes, with limited success.)

BEFORE | AFTER | COMMENT -------------+--------+---------------- '^(foo)' | BAR | Regular use '\^(foo)' | ^(foo) | Escaped ^ "\^(foo)" | BAR | Perl squashes \^ | | in double quotes '\\^(foo)' | \^(foo)| Confused with '\BAR'? "\\^(foo)" | ^(foo) | Double quotes

Option B - ^^ escapes ^

(Similar to %% in printf)

BEFORE | AFTER | COMMENT -------------+--------+---------------- '^^(foo)' | ^(foo) | ^^ = escaped ^ '^^^(foo)' | ^BAR | ^^ = ^, ^(foo) '^^^^(foo)' | ^^BAR | ^^ = ^ twice '^ ^(foo)' | ^ BAR | No need to escape | ^ unless part of | ^(foo) match.

Option C - Special ^(...) code for literal carat

(Similar to HTML escapes or Pod C<$a E<lt>=E<gt> $b>, if a bit more concise.)

BEFORE | AFTER | COMMENT -------------+--------+----------------- '^(^)(foo)' | ^(foo) | ^(^) = escaped ^ '^^(foo)' | ^BAR | ^ literal if not | part of pattern '^(^)^(foo)' | ^BAR | Explicit escape '^^(^)(foo)' | ^^BAR | Or ^(^)^(^)^(foo)

I think most would agree that each of these invite potentially frustrating syntax (at least the errors are likely to be readily apparent), but I'd like to be as obvious as possible without a large documentation burden (while still supporting this sort of interpolation, of course).

A couple of other notes:

Which syntax (including anything I haven't considered here) would you most prefer/most hate to use (and troubleshoot)? Why? I'd very much appreciate any relevant suggestions.


In reply to Best practice on escapes in interpolated strings by wanna_code_perl

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.