tshabet has asked for the wisdom of the Perl Monks concerning the following question:

I looked around for a while but couldn't find the answer to this question. I was showing my mom (a brilliant woman with a PhD who is scared of programming) a script I was writing. I was explaing to her how I could match and replace with regexes and such, and showed her an output like this:
This is sample code with a tab in it. this line is preceded by a \n
So she was sort of catching on, and then she asked me how I can tell by looking at an input sample whether I'm looking at a tab character or whitespace or what. I sort of dodged the question by saying that the computer knows what is what, but the fact is I didn't know. So now my question to the monks:
Is there any editor/emacs mode/etc such that I can view an input or output sample that actually shows, say, \t instead of actually tabbing. Does that explanation make sense? I mean, I'd like to be able to look at a paragraph or whatever without having to resort to octal or something but still be able to see what's what in terms of metachars and such. Is there an easy way to do this? If this has already been addressed somewhere, please point the way and accept my apology. Thanks Monks!

Replies are listed 'Best First'.
Re: Viewing metasymbols in output?
by davorg (Chancellor) on Aug 07, 2001 at 19:04 UTC

      I just add that in vim there is also :help 'listchars' to give you further control on what to display for a tab, eol and trailing spaces.

      -- Hofmator

Re: Viewing metasymbols in output?
by dragonchild (Archbishop) on Aug 07, 2001 at 19:04 UTC
    Well, it sounds like what you want is the Symbol view (or whatever it's called) in a wordprocessor. You can write a quick Perl script to convert a text file into that. Use something like:
    local $\ = ''; my $filename = "/mydir/myfile"; open FILE, "<$filename" or die "Cannot open $filename for reading\n"; my $file = <FILE>; close FILE; $file =~ s/\t/\\t/g; open FILE, ">$outfilename" or die "Cannot open $outfilename for writing\n"; print FILE $file; close FILE;
    And do that substitution like for every metacharacter you want to replace.

    The reason to use s/// instead of tr/// is that you are replacing in a 1:n relationship and not the 1:1 that tr requires.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      or in one line: perl -pe 's/\t/\\t/g' infile > outfile to allow for more substitutions consider a hash like

      my %mapping = ( # or whatever you like to display "\t" => '\t', "\n" => '\n', " " => '_', ); my $pattern = qr/([@{[join '', keys %mapping]}])/; while ( <> ) { s/$pattern/$mapping{$1}/g; print "$_\n"; # you still want to start a new line }
      This works fine as long as your keys are only characters, otherwise you have to use an alternation instead of a character class.

      -- Hofmator

Re: Viewing metasymbols in output?
by ariels (Curate) on Aug 07, 2001 at 20:02 UTC
    It is unthinkable that the vi heathenlovers will have a feature which we enlightened (X)Emacs users lack. whitespace-mode is what you need!

    You might need to load the library (either add "(load-library "whitespace-mode")" to your .emacs, or if you just want it once, say "M-x load-library RET whitespace-mode RET"). Then, to activate it, hit "M-x whitespace-mode RET" and watch your spaces and tabs light up in different colours.

    Note to the unenlightened: you never really type in these long sequences at (X)Emacs: you use TAB or SPACE to get completion.

Re: Viewing metasymbols in output?
by clemburg (Curate) on Aug 07, 2001 at 19:25 UTC

    A good way to show your mom might be using a hex editor, so that you can see the actual bytes in the file. In emacs, that would done by using "M-x hexl-mode" when viewing the file.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Viewing metasymbols in output?
by tachyon (Chancellor) on Aug 07, 2001 at 21:16 UTC

    This is a nice way to map it out and preserve the spatial relationships of the text. Tabs are changed to ' -> ' and spaces to dots '·' (chr 183 ASCII). This is how my editor displays them when I select the display all chars option. chr 182 ASCII is that funny backwards P symbol ¶ 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.

    sub show { my @data = @_; for (@data) { s/(\266|\267)/sprintf"%%%02X",ord $1/egm; s/ /chr(0267)/egm; s/\t/ -> /gm; s/\n/chr(0266)."\n"/egm; } return wantarray ? @data : join'',@data; } sub hide { my @data = @_; for (@data) { s/ -> /\t/gm; s/\266//gm; s/\267/ /gm; s/%([0-9a-f][0-9a-f])/chr hex $1/eigm; } 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 ····4·spaces,·trailing·tab -> ¶ -> ····tab·and·4·spaces¶ -> -> ¶ ¶

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Viewing metasymbols in output?
by tshabet (Beadle) on Aug 07, 2001 at 19:35 UTC
    Cool, I'll try out these solutions. Thanks guys! Before you know it my mom will think I have a respectable job :-)
Re: Viewing metasymbols in output?
by tshabet (Beadle) on Aug 07, 2001 at 20:12 UTC
    Ho ho! An Emacs solution would make me happiest of all, I'll most definitely try that one out. These suggestions are great, I never imagined I'd have so many options. Thank you all so much!
Re: Viewing metasymbols in output?
by Rudif (Hermit) on Aug 08, 2001 at 04:22 UTC
    You could also write a hex dump perl module for your mom. Or show her an existing one (or two).

    These are available on CPAN and on ActiveState PPM:

    #!perl -w use strict; my $buf = "Look, Ma:\nfoo\tbar\tsee those tabs?\nFOO BAR look here +- no tabs, just spaces.\n"; print "$buf\n"; use Data::HexDump; print "HexDump:\n", HexDump "$buf"; use Data::Hexdumper; print "Hexdumper:\n", Data::Hexdumper::Hexdump(data => $buf);

    Rudif