Perhaps you have a specific application that would be better served by a tool meant for that application. Text::CSV and DBI for example have ways of working with data that do not require you to worry about escaping strings in your code.

That being said, many useful things are already written more than once. Unfortunately they are often already written more than once in ways which do you little good for your specific problem. The most general and simplest answer to your query seems to be the narrowly named but broadly general Text::EscapeDelimiters which will escape more than delimiters. There is also the corresponding Text::DelimMatch for retrieving delimited text.

String::Escape does what you are asking and more but only in certain narrow cases. Unfortunately, those cases do not include your example specification. You can add escaping methods to it, but then you'd have to write them.

Encode::Escape wraps up several different types of escaping into one module.

URI::Escape works well with URLs and URIs.

Regexp::Common has many tasty treats in its namespace, including Regexp::Common::delimited.


In reply to Re: string escaping / de-escaping functions by mr_mischief
in thread string escaping / de-escaping functions by fearless_fool

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.