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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |