These two subs emulate the 'show all characters' functionality of most good text editors. Tabs are changed to ' -> ' and spaces to dots '·' (chr 183 ASCII). The funny backwards P symbol '¶' (chr 182 ASCII) is used for newlines. We use the octal naming convention for these chars for convenience. chr 0266 = '¶' and chr 0267 = '·'

In the unlikely event these special chars are included in the text to be processed we hex encode them using the standard URL encoding convention of a % followed by two hex digits. We can then remove the URL encoding to regenerate our original text.

The output of the show sub is shown in the second 4 lines of data which were the output on the first 4 lines. They provide the test case for the encoding.

Efficiency hacks courtesty bbfu and Hofmator.

sub show { my @data = @_; for (@data) { s/\266/%B6/g; s/\267/%B7/g; tr/ /\267/; s/\t/ -> /g; s/\n/\266\n/g; } return wantarray ? @data : join'',@data; } sub hide { my @data = @_; for (@data) { s/ -> /\t/g; tr/\266//d; tr/\267/ /; s/%B6/\266/g; s/%B7/\267/g; } return wantarray ? @data : join'',@data; } @data = (<DATA>)[0..7]; print "Original data, including specials\n\n"; print @data; print "\n\nShow invisible chars in data\n\n"; print show(@data); print "\n\nHide and show data - should not change\n\n"; print hide(show(@data)); __DATA__ tab 4 spaces, trailing tab tab and 4 spaces -> tab¶ ····4·spaces,·trailing·tab -> ¶ -> ····tab·and·4·spaces¶ -> -> ¶ ¶

In reply to Show All Characters in Text by tachyon

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.