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

Does any one know of a perl module that allows you to visualize white spaces(tab space newline CR etc.) similar to the "visible spaces" function in many text editors? I have done some searching on cpan but evidently can formulate the right search to find anything.

Replies are listed 'Best First'.
Re: Perl module to show whitespaces?
by JavaFan (Canon) on Apr 10, 2012 at 16:15 UTC
    A simple way would be:
    s/\s/_/g;
    No need for a module for that. You can of course be more fancy and show newlines as \n or ^J, tabs as \t or ^I, etc. All trivial to do with a regexp.
Re: Perl module to show whitespaces?
by Anonymous Monk on Apr 10, 2012 at 16:47 UTC
    This substitution map that is more varied than JavaFan's quick-and-dirty solution, so by looking at the output you have a better idea what the original space was.
    use charnames ':full'; %spaces = ( "\N{CHARACTER TABULATION}" => "\N{SYMBOL FOR HORIZONTAL TABULA +TION}", "\N{LINE FEED (LF)}" => "\N{SYMBOL FOR LINE FEED}", "\N{LINE TABULATION}" => "\N{SYMBOL FOR VERTICAL TABULATION}", "\N{FORM FEED (FF)}" => "\N{SYMBOL FOR FORM FEED}", "\N{CARRIAGE RETURN (CR)}" => "\N{SYMBOL FOR CARRIAGE RETURN}" +, "\N{SPACE}" => "\N{SYMBOL FOR SPACE}", "\N{NEXT LINE (NEL)}" => "\N{DOWNWARDS ARROW WITH CORNER LEFTW +ARDS}", "\N{NO-BREAK SPACE}" => "\N{MIDDLE DOT}", "\N{OGHAM SPACE MARK}" => "\N{HYPHEN-MINUS}", "\N{MONGOLIAN VOWEL SEPARATOR}" => "\N{HYPHEN-MINUS}", "\N{EN QUAD}" => "\N{EN DASH}", "\N{EM QUAD}" => "\N{EM DASH}", "\N{EN SPACE}" => "\N{EN DASH}", "\N{EM SPACE}" => "\N{EM DASH}", "\N{THREE-PER-EM SPACE}" => "\N{HYPHEN-MINUS}", "\N{FOUR-PER-EM SPACE}" => "\N{HYPHEN-MINUS}", "\N{SIX-PER-EM SPACE}" => "\N{HYPHEN-MINUS}", "\N{FIGURE SPACE}" => "\N{FIGURE DASH}", "\N{PUNCTUATION SPACE}" => "\N{MIDDLE DOT}", "\N{THIN SPACE}" => "\N{MIDDLE DOT}", "\N{HAIR SPACE}" => "\N{MIDDLE DOT}", "\N{LINE SEPARATOR}" => "\N{DOWNWARDS ARROW WITH CORNER LEFTWA +RDS}", "\N{PARAGRAPH SEPARATOR}" => "\N{PILCROW SIGN}", "\N{NARROW NO-BREAK SPACE}" => "\N{MIDDLE DOT}", "\N{MEDIUM MATHEMATICAL SPACE}" => "\N{MIDDLE DOT}", "\N{IDEOGRAPHIC SPACE}" => "\N{IDEOGRAPHIC NUMBER ZERO}", );
    For accuracy, however, nothing beats the uniquote filter. The result does not look like in a text editor anymore, but solves the problem completely: all invisible characters (not only spaces) are quoted/made visible.
      what is the syntax to apply that map to an un chomped string?
        use encoding 'UTF-8'; $string = "this\tis a\ncool\ftest\r"; $string =~ s/(\s)/$spaces{$1}/g; print $string;