Removing White Space Perl

Removing White Space Perl

Removing White Space Perl

I am working on a module that relies on Pod::Usage to parse the calling script's POD and then send usage, help, and man text to a scalar variable. I needed to remove the final whitespace from that text, so I used a simple regex that I thought would work. And it did ... but intermittently.

Here's a demonstration of the problem. Any insights would be appreciated.

The unexpected behavior (i.e., the failure of the regex to remove final newlines) occurs consistently on my Solaris machine with Perl 5.10.1. Under Windows with Perl 5.12.1, the behavior is erratic (output supplied below).

use strict; use warnings; use Pod::Usage qw(pod2usage); use Test::More; # Baseline test to show that the regex works. my $exp = "foo\nbar\n..."; my $with_trailing_whitespace = $exp . " \n\n"; $with_trailing_whitespace =~ s!\s+\Z!!; my $ords = get_ords_of_final_chars($with_trailing_whitespace); is_deeply $ords, [46, 46, 46]; # String ends with 3 periods (not whitespace). # Run a similar test, using text from Pod::Usage. for (1 .. 2){ my $pod = get_pod_text(); $ords = get_ords_of_final_chars($pod); is_deeply $ords, [46, 46, 46]; } done_testing(); sub get_ords_of_final_chars { # Takes a string. Return array ref of the ord() of last 3 characters. my $s = shift; return [ map ord(substr $s, - $_, 1), 1 .. 3 ]; } sub get_pod_text { # Call pod2usage(), sending output to a scalar. open(my $fh, '>', \my $txt) or die $!; pod2usage(-verbose => 2, -exitval => 'NOEXIT', -output => $fh); close $fh; # This doesn't help. # Here's the same regex as above. # # If I use chomp(), the newlines are consistently removed: # 1 while chomp($txt); $txt =~ s!\s+\Z!!; return $txt; } __END__ =head1 NAME sample - Some script... =head1 SYNOPSIS foo.pl ARGS... =head1 DESCRIPTION This program will read the given input file(s) and do something useful with the contents thereof... =cut

Output on my Windows box:

$ perl demo.pl ok 1 not ok 2 # Failed test at demo.pl line 18. # Structures begin differing at: # $got->[0] = '10' # $expected->[0] = '46' not ok 3 # Failed test at demo.pl line 18. # Structures begin differing at: # $got->[0] = '10' # $expected->[0] = '46' 1..3 # Looks like you failed 2 tests of 3. $ perl demo.pl ok 1 ok 2 ok 3 1..3

picture-mode

Emacs also has a “picture-mode”, designed for drawing ASCII diagrams. I've never used it.

Open a new file. (in ErgoEmacs, 【Ctrl+n】. In GNU Emacs, call “switch-to-buffer” 【Alt+x b】, then give a random name.) Then, call “picture-mode”, then call “describe-mode” 【Ctrl+h m】 to see how to use it.

artist-mode

Emacs also has a “artist-mode”. It's a mode that lets you draw ASCII pictures with the mouse.

Open a new file. Now, you can draw with your mouse. Hold right button to erase. Middle click to switch to rectangle, ellipse, and other tools.

+--------------+ ----------- | +---+ | --/ ------- \-- | |+--+--------+---+ -/ --/ \-- \- | || | | | / / ----- \ \ | || | +-----+-+ | | | ( ) | | +-++--+--+-----+ | | \ \ ----- / / || | +-------+ | -\ --\ /-- /- |+--+------------+ --\ ------- /-- | | ----------- ●●●● | | ●●●●●●● ●●● +---+ ●●●●● ●●●●●●●●●●● ●●●● ●● ●●●●● ●●●●●●●●●● ●●●●●● ● ● ● ●●●●● ●●●●●●●●●●●●●●●● ●● ● ● ●● ●● ●●● ●●●● ●●●● ● ● ● ● ●●● ●●●● ●●●●●● ●● ●●● ● ●● ●●● ●●● ●●● ●●●● ●●● ●● ● ● ●●● ●●●●●●● ●●●●● ●●●●●●● ●● ● ● ●● ●●● ●●● ●●●● ●●● ●● ●● ●● ●●●●● ●●● ●●●●●●●●● ●● ●● ● ● ●●●● ●●● ●● ● ● ●● ● ●● ●●● ●● ● ● ●●●●● ●● ● ● ●●●●●● ● ● ● ●● ●● ●● ●● ●● ●● ● ● ● ●●●● ● ●●● ●● ●● ●● ● ●●●●●●●● ●●●●●●●●●● ●● ●● ● ●● ●●●●●●●● ●●● ●●● ●● ●● ● ●●●●●●●● ●● ●●●●●●●●● ●●● ● ● ●●●●●●● ● ●●● ●● ●● ●●● ●● ●●● ●● ●● ●● ●●● ●●● ●● ●●●●●●●● ●● ●●●●●●●●● ●● ●● ●●● ●●● ●● ●●●●●●●●●●●●●●●●● ● ●●●●●●●●●● ● ●●●●●●●●●●●●●●●●●●●●●● ●●●● ●●● ●●●●●●●●●●●●●●●●●●●●●●●● Some doodle with “artist-mode”.

You can see a video of this guy using it: Emacs Screencast: Artist Mode (2009-05-25) By Seong-Kook Shin. @ Source www.cinsk.org.

I should warn that you should not get into a habit of using ASCII art in comments, such as drawing boxes. Because, it is a form of hard-formatting. As such, it is not flexible and creates all sorts of problems. See:

  • The Harm of Hard-wrapping Lines
  • Tabs versus Spaces in Source Code
  • Unix, RFC, Line Truncation
  • A Text Editor Feature: Extend Selection by Semantic Unit
  • A Simple Lisp Code Formatter

For the same reason, i never liked any comment alignment in my source code, and i am annoyed that emacs by default aligns your comment when you call “comment-dwim” 【Alt+;】


Relevant pages: