in reply to What is the function that does the following..?
perluniintro shows a function nice_string() which essentially does what you want (i.e. render special characters as \x... escapes), and can easily be adapted to your likings. For example, here in slightly modified form:
#!/usr/local/bin/perl -l sub nice_string { join(" ", map { $_ > 255 ? # if wide character... sprintf("\\x{%04X}", $_) : # \x{...} chr($_) =~ /[[:cntrl:]]/ ? # else if control character .. +. sprintf("\\x%02X", $_) : # \x.. chr($_) # else as themselves } unpack("U*", $_[0])); # unpack Unicode characters } my $var = "Some random text.\nMore\nEven More\n"; print nice_string($var); __END__ S o m e r a n d o m t e x t . \x0A M o r e \x0A E v e n M o r e +\x0A
(It expects a decoded character string.)
|
|---|