my $var = "Hello World \x{263a}\cM\x0A This bit is trimmed"; print "\$var is $qesc_to{$var}{30}\n"; # output: $var is "Hello World \x{263A}\r\n..."

Interpolation uses tied hashes to transform data before interpolating it into a string. Here is a snippet to display an escaped version, possibly smart truncated with String::Escape's elide.

I didn't use S::E's printable as it doesn't try to provide valid perl strings, making it less usefull for debugging.

#!/usr/bin/perl -wl use strict; use String::Escape qw(printable qprintable elide); use YAML qw(Dump); use Interpolation ( 'elide:$$->$' => sub { elide($_[0],$_[1]) }, 'esc' => \&esc, 'esc_to:$$->$' => sub { elide(esc($_[0]),$_[1]) }, 'qesc' => sub { '"'.esc(@_).'"' }, 'qesc_to:$$->$' => sub { '"'.elide(esc($_[0]),$_[1]).'"' }, 'uniescape' => \&uniescape, 'yaml' => sub { "\n".Dump($_[0]) }, ); my %escaped = map { eval qq{"\\$_"} => "\\$_" } qw(a t e f b); # perlo +p # String::Escape::printable doesn't always produce valid perl strings sub esc { local $_ = shift; s/([\$"@])/\\$1/g; # interpolators s/([\t\e\f\b\a])/$escaped{$1}/g; # portable escapes s/\r/\\r/g, s/\n/\\n/g if("\r\n" eq "\cM\cJ"); # non-portable s/([^ [:graph:]])/sprintf '\\x{%02X}', ord $1/ge; # \x{} form for +clarity $_ = uniescape($_); return $_; } # From /usr/share/perl/5.8.7/dumpvar.pl sub uniescape { join("", map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) } unpack("U*", $_[0])); } my $var = "Hello World \x{263a}\cM\x0A This bit is trimmed"; print "\$var is $qesc_to{$var}{30}\n"; # OUTPUT: # $var is "Hello World \x{263A}\r\n..." my $long_str = "Some long, long, long, long string"; my $ugly_str = "Some \f string with\r\n\tnewlines,\b tabs & the like \ +x{263a}\n"; print "long string is $elide{$long_str}{25}"; print "ugly_str is $esc{$ugly_str}"; print "ugly_str is $qesc{$ugly_str}"; print "ugly_str is $esc_to{$ugly_str}{35}"; print "ugly_str is $qesc_to{$ugly_str}{35}"; print "ugly_str is $uniescape{$ugly_str}"; # Bonus recipe: my $ds = [ 1,undef, { b => 3 }]; print "\$ds is $yaml{$ds}"; # OUTPUT: # $ds is # --- # - 1 # - ~ # - b: 3

In reply to String Escaping recipe for Interpolation by bsb

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.