blib/lib/Whitespace.pm

filter: Statementstmt 10.7% 19 / 177 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 23.8% 5 / 21 Podpod 0.0% 0 / 10 totaltotal 7.3% 24 / 326 CC 55 SCAR 57.9
CC
55
Cov
7%
detect
702.0
cleanup
317.1
_leadtrailclean
20.0
CRAP 327.2
↑ Summary
1 package Whitespace;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 #
5 # $Id: Whitespace.pm,v 1.4 2001/05/23 21:36:50 rv Exp $
6 #
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
11  
12 1 $VERSION = 1.02;
13 1 @ISA = qw(Exporter);
14 1 @EXPORT = qw(&new &detect &cleanup &error &status);
15 1 @EXPORT_OK = qw(&leadclean &trailclean &indentclean &spacetabclean
16 &eolclean &DESTROY);
17 1 %EXPORT_TAGS = ();
18 }
19  
20 =head1 NAME
21  
22 Whitespace - Cleanup various types of bogus whitespace in source files.
23  
24 =head1 SYNOPSIS
25  
26 use Whitespace;
27  
28 # Instantiate a whitespace object with
29 # both input and output files specified
30 $ws = new Whitespace($infile, $outfile);
31  
32 # Instantiate a whitespace object with
33 # only the input files specified (in-place cleanup)
34 $ws2 = new Whitespace($infile);
35  
36 # Detect the whitespaces
37 $ret = $ws->detect();
38  
39 C<detect> returns B<undef> if it is unable to operate on the given
40 file.
41  
42 The error that caused the undef can be retrieved using C<error>
43  
44 print $ws->error() . "\n" unless defined $ret;
45  
46 C<detect> returns the types of whitespaces detected as a hash which
47 can be retrieved using the method C<status>. The populated hash might
48 look like this, if the file only had leading, trailing and end-of-line
49 spaces (say on 3 lines).
50  
51 %stat = %{$env->status()};
52 print map "$_ => $stat{$_}\n", sort keys %stat;
53  
54 eol => 3
55 indent => 0
56 leading => 1
57 spacetab => 0
58 trailing => 1
59  
60 Cleanup can be achieved for all the whitespaces or for just a given
61 type of whitespace, using the following methods.
62  
63 If a B<outfile> is given, the cleaned contents are written to this
64 file. If not, the contents are replaced in-place. B<undef> is
65 returned if there was an error writing the file.
66  
67 # To cleanup the all the whitespaces
68 $ret = $env->cleanup();
69  
70 # To cleanup leading whitespaces only
71 $leadstat = $env->leadclean();
72  
73 # To cleanup trailing whitespaces only
74 $trailstat = $env->trailclean();
75  
76 # To cleanup indentation whitespaces only
77 $indentstat = $env->indentclean();
78  
79 # To cleanup space-followed-by-tabs only
80 $sftstat = $env->spacetabclean();
81  
82 # To cleanup end-of-line whitespaces only
83 $eolstat = $env->eolclean();
84  
85 =cut
86  
87 #
88 # Exported Functions
89 #
90 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my $package = shift;
92 0 my $env = {
93 'infile' => shift,
94 'outfile' => shift,
95 'cleaned' => 0,
96 };
97 0 bless $env, $package;
98 }
99  
100 0 ▶sub detect {
Subroutine detect: not called CC=26 SCAR=65.5
Pod undocumented
101 0 my $env = shift;
102 0 my $ret = 0;
103 0 ▶ my $infile = $env->{cleaned} ? $env->{'outfile'} : $env->{'infile'};
Branch: 1 branch
BranchTrueFalseTotal
$env->{'cleaned'} ? : 0 0 0
104  
105 0 ▶ unless (defined $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $infile) 0 0 0
106 0 $env->{'error'} = "No input file!";
107 0 return undef;
108 }
109 0 ▶ if (-d $infile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $infile) 0 0 0
110 0 $env->{'error'} = "$infile: Is a directory!";
111 0 return undef;
112 }
113 0 ▶ if (ref $infile) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $infile) 0 0 0
114 0 $env->{'error'} = "$infile: Is not a regular file (a reference)!";
115 0 return undef;
116 }
117 0 ▶ unless (open FILE, $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, $infile) 0 0 0
118 0 $env->{'error'} = "$infile: $!";
119 0 return undef;
120 }
121 0 ▶ if (! -T _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T _) 0 0 0
122 0 close FILE;
123 0 $env->{'error'} = "$infile: Not a text file!";
124 0 return undef;
125 }
126 0 $env->{'_IFILE'} = *FILE;
127  
128 0 my $first = 1;
129 0 my $leading = 0;
130 0 my $trailing = 0;
131 0 my $indent = 0;
132 0 my $spacetab = 0;
133 0 my $ateol = 0;
134 0 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
135 0 ▶ if (! /^.*\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^.*\n$/) 0 0 0
136 # warn "$infile: Line too long\n";
137 0 $env->{'error'} = "$infile: Line too long";
138 0 last;
139 }
140  
141 =head1 DESCRIPTION
142  
143 =item Leading space
144  
145 Empty lines at the top of a file.
146  
147 =cut
148 0 ▶ $leading = 1 if $first && /^[ \t]*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $first and /^[ \t]*$/ 0 0 0
Condition0%0/3
$first and /^[ \t]*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$first and /^[ \t]*$/
$first /^[ \t]*$/
Truth table0%0/3
A and B
A= $firstB= /^[ \t]*$/
ABresult
0X0
100
111
149  
150 =item Trailing space
151  
152 Empty lines at the end of a file.
153  
154 =cut
155 0 ▶ $trailing = /^[ \t]*$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
/^[ \t]*$/ ? : 0 0 0
156  
157 =item Indentation space
158  
159 8 or more spaces at the beginning of a line, that should be replaced with
160 TABS.
161  
162 Since this is the most controversial one, here is the rationale:
163 Most terminal drivers and printer drivers have TAB configured or
164 even hardcoded to be 8 spaces. (Some of them allow configuration,
165 but almost always they default to 8.)
166  
167 Changing tab-width to other than 8 and editing will cause your
168 code to look different from within emacs, and say, if you cat it
169 or more it, or even print it.
170  
171 Almost all the popular programming modes let you define an offset
172 (like c-basic-offset or perl-indent-level) to configure the
173 offset, so you should never have to set your tab-width to be other
174 than 8 in all these modes. In fact, with an indent level of say,
175 4, 2 TABS will cause emacs to replace your 8 spaces with one \t
176 (try it). If vi users in your office complain, tell them to use
177 vim, which distinguishes between tabstop and shiftwidth (vi
178 equivalent of our offsets), and also ask them to set smarttab.
179  
180 =cut
181 0 ▶ $indent = 1 if /^\s* {8,}/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s* {8,}/ 0 0 0
182  
183 =item Spaces followed by a TAB.
184  
185 Almost always, we never want that.
186  
187 =cut
188 0 ▶ $spacetab = 1 if / \t/;
Branch: 1 branch
BranchTrueFalseTotal
if / \t/ 0 0 0
189  
190  
191 =item EOL Whitespace
192  
193 Spaces or TABS at the end of a line.
194  
195 =cut
196  
197 0 ▶ $ateol = 1 if /[ \t]$/;
Branch: 1 branch
BranchTrueFalseTotal
if /[ \t]$/ 0 0 0
198 0 $first = 0;
199 }
200 0 close FILE;
201 0 $env->{'_IFILE'} = undef;
202 0 ▶ return undef if defined $env->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $env->{"error"} 0 0 0
203 0 ▶ ++$ret if $leading;
Branch: 1 branch
BranchTrueFalseTotal
if $leading 0 0 0
204 0 ▶ ++$ret if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 0 0
205 0 ▶ ++$ret if $spacetab;
Branch: 1 branch
BranchTrueFalseTotal
if $spacetab 0 0 0
206 0 ▶ ++$ret if $ateol;
Branch: 1 branch
BranchTrueFalseTotal
if $ateol 0 0 0
207 0 ▶ ++$ret if $trailing;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing 0 0 0
208  
209 0 ▶ ++$env->{'status'}->{'leading'} if $leading;
Branch: 1 branch
BranchTrueFalseTotal
if $leading 0 0 0
210 0 ▶ ++$env->{'status'}->{'trailing'} if $trailing;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing 0 0 0
211 0 ▶ ++$env->{'status'}->{'indent'} if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 0 0
212 0 ▶ ++$env->{'status'}->{'spacetab'} if $spacetab;
Branch: 1 branch
BranchTrueFalseTotal
if $spacetab 0 0 0
213 0 ▶ ++$env->{'status'}->{'eol'} if $ateol;
Branch: 1 branch
BranchTrueFalseTotal
if $ateol 0 0 0
214  
215 0 return $ret;
216 }
217  
218 0 ▶sub cleanup {
Subroutine cleanup: not called CC=19 SCAR=57.6
Pod undocumented
219 0 my $env = shift;
220 0 my $infile = $env->{'infile'};
221 0 my $outfile = $env->{'outfile'};
222 0 my $cleanup => $env->{'cleanup'};
223  
224 0 ▶ unless (defined $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $infile) 0 0 0
225 0 $env->{'error'} = "No input file!";
226 0 return undef;
227 }
228 0 ▶ if (-d $infile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $infile) 0 0 0
229 0 $env->{'error'} = "$infile: Is a directory!";
230 0 return undef;
231 }
232 0 ▶ if (ref $infile) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $infile) 0 0 0
233 0 $env->{'error'} = "$infile: Is not a regular file (a reference)!";
234 0 return undef;
235 }
236 0 ▶ unless (open FILE, $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, $infile) 0 0 0
237 0 $env->{'error'} = "$infile: $!";
238 0 return undef;
239 }
240 0 $env->{'_IFILE'} = *FILE;
241 0 ▶ if (defined $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outfile) { } 0 0 0
242 0 ▶ unless (open OUTFILE, ">$outfile") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUTFILE, ">$outfile") 0 0 0
243 0 $env->{'error'} = "$outfile: $!";
244 0 close FILE;
245 0 return $env->{'_IFILE'} = undef;
246 }
247 0 close OUTFILE;
248 } else {
249 0 ▶ unless (-w $infile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $infile) 0 0 0
250 0 $env->{'error'} = "$infile: Not writable!";
251 0 return undef;
252 }
253 0 $outfile = $infile;
254 0 $env->{'outfile'} = $env->{'infile'};
255 }
256  
257 0 my @arr = <FILE>;
258 0 close FILE;
259 0 $env->{'_IFILE'} = undef;
260 #
261 # Leading/Trailing space cleanup
262 #
263 @arr = _leadtrailclean(@arr)
264 0 ▶ if (!defined $cleanup || $cleanup->{'leading'});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cleanup or $cleanup->{"leading"} 0 0 0
Condition0%0/3
not defined $cleanup or $cleanup->{"leading"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cleanup or $cleanup->{"leading"}
not defined $cleanup $cleanup->{"leading"}
Truth table0%0/3
not(A) or B
A= not defined $cleanupB= $cleanup->{"leading"}
ABresult
1X1
011
000
265 @arr = reverse _leadtrailclean(reverse @arr)
266 0 ▶ if (!defined $cleanup || $cleanup->{'trailing'});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cleanup or $cleanup->{"trailing"} 0 0 0
Condition0%0/3
not defined $cleanup or $cleanup->{"trailing"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cleanup or $cleanup->{"trailing"}
not defined $cleanup $cleanup->{"trailing"}
Truth table0%0/3
not(A) or B
A= not defined $cleanupB= $cleanup->{"trailing"}
ABresult
1X1
011
000
267  
268 #
269 # Indentation cleanup
270 #
271 @arr = _indentclean(@arr)
272 0 ▶ if (!defined $cleanup || $cleanup->{'indent'});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cleanup or $cleanup->{"indent"} 0 0 0
Condition0%0/3
not defined $cleanup or $cleanup->{"indent"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cleanup or $cleanup->{"indent"}
not defined $cleanup $cleanup->{"indent"}
Truth table0%0/3
not(A) or B
A= not defined $cleanupB= $cleanup->{"indent"}
ABresult
1X1
011
000
273  
274 #
275 # EOL Space cleanup
276 #
277 @arr = _eolclean(@arr)
278 0 ▶ if (!defined $cleanup || $cleanup->{'eol'});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cleanup or $cleanup->{"eol"} 0 0 0
Condition0%0/3
not defined $cleanup or $cleanup->{"eol"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cleanup or $cleanup->{"eol"}
not defined $cleanup $cleanup->{"eol"}
Truth table0%0/3
not(A) or B
A= not defined $cleanupB= $cleanup->{"eol"}
ABresult
1X1
011
000
279  
280 #
281 # Space-followed-by-TAB cleanup
282 #
283 @arr = _spctabclean(@arr)
284 0 ▶ if (!defined $cleanup || $cleanup->{'spacetab'});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cleanup or $cleanup->{"spacetab"} 0 0 0
Condition0%0/3
not defined $cleanup or $cleanup->{"spacetab"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cleanup or $cleanup->{"spacetab"}
not defined $cleanup $cleanup->{"spacetab"}
Truth table0%0/3
not(A) or B
A= not defined $cleanupB= $cleanup->{"spacetab"}
ABresult
1X1
011
000
285  
286 1 ▶ use File::Spec 0.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
287 0 my ($junk, $tmp);
288 0 ($junk, $junk, $tmp) = File::Spec->splitpath($infile);
289 0 my $tmpdir = File::Spec->tmpdir;
290  
291 0 $tmp = File::Spec->catfile($tmpdir, "$tmp.$$");
292 0 ▶ unless (open FILE, ">$tmp") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, ">$tmp") 0 0 0
293 0 $env->{'error'} = "$tmp: $!. $infile not cleaned";
294 0 return undef;
295 }
296 0 $env->{'_TFILE'} = *FILE;
297 0 print FILE @arr;
298 0 close FILE;
299 0 $env->{'_TFILE'} = undef;
300  
301 1 ▶ use File::Copy qw(move);
Subroutine BEGIN: called CC=1 SCAR=0.0
302 0 move($tmp, $outfile);
303  
304 #
305 # Test the file once again.
306 #
307 0 $env->{'cleaned'} = 1;
308 0 return $env->detect;
309 }
310  
311 0 ▶sub leadclean {
Subroutine leadclean: not called CC=1 SCAR=6.9
Pod undocumented
312 0 my $env = shift;
313 0 $env->{'cleanup'}->{'leading'} = 1;
314 0 return $env->cleanup;
315 }
316  
317 0 ▶sub trailclean {
Subroutine trailclean: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my $env = shift;
319 0 $env->{'cleanup'}->{'trailing'} = 1;
320 0 return $env->cleanup;
321 }
322  
323 0 ▶sub indentclean {
Subroutine indentclean: not called CC=1 SCAR=6.9
Pod undocumented
324 0 my $env = shift;
325 0 $env->{'cleanup'}->{'indent'} = 1;
326 0 return $env->cleanup;
327 }
328  
329 0 ▶sub spacetabclean {
Subroutine spacetabclean: not called CC=1 SCAR=6.9
Pod undocumented
330 0 my $env = shift;
331 0 $env->{'cleanup'}->{'spacetab'} = 1;
332 0 return $env->cleanup;
333 }
334  
335 0 ▶sub eolclean {
Subroutine eolclean: not called CC=1 SCAR=6.9
Pod undocumented
336 0 my $env = shift;
337 0 $env->{'cleanup'}->{'eol'} = 1;
338 0 return $env->cleanup;
339 }
340  
341 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
342 0 my $env = shift;
343 0 $env->{'error'};
344 }
345  
346 0 ▶sub status {
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
347 0 my $env = shift;
348 0 $env->{'status'};
349 }
350  
351 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
352 0 my $env = shift;
353 0 my $ifh = $env->{'_IFILE'};
354 0 my $tfh = $env->{'_TFILE'};
355 # warn "destroying whitespace object for $env->{'infile'}\n";
356 0 ▶ close $ifh if defined $ifh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ifh 0 0 0
357 0 ▶ close $tfh if defined $tfh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tfh 0 0 0
358 }
359  
360 #
361 # Internal functions
362 #
363 0 ▶sub _leadtrailclean {
Subroutine _leadtrailclean: not called CC=4 SCAR=30.0
364 0 my $first = 1;
365 0 my @ret = ();
366 0 foreach (@_) {
367 0 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 0 0 0
368 0 ▶ if (! /^[ \t]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[ \t]*$/) 0 0 0
369 0 $first = 0;
370 0 push @ret, $_;
371 }
372 } else {
373 0 $first = 0;
374 0 push @ret, $_;
375 }
376 }
377 0 return @ret;
378 }
379  
380 0 ▶sub _indentclean {
Subroutine _indentclean: not called CC=3 SCAR=24.8
381 0 my @ret = ();
382 0 foreach (@_) {
383 0 ▶ while (/^\s* {8,}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s* {8,}/) 0 0 0
384 0 $_ =~ s/^(\t*) {8}/$1\t/g;
385 }
386 0 push @ret, $_;
387 }
388 0 return @ret;
389 }
390  
391 0 ▶sub _eolclean {
Subroutine _eolclean: not called CC=2 SCAR=17.9
392 0 my @ret = ();
393 0 foreach (@_) {
394 0 $_ =~ s/[ \t]*$//g;
395 0 push @ret, $_;
396 }
397 0 return @ret;
398 }
399  
400 0 ▶sub _spctabclean {
Subroutine _spctabclean: not called CC=4 SCAR=30.0
401 0 my @ret = ();
402 0 foreach (@_) {
403 0 ▶ while (/ \t/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \t/) 0 0 0
404 0 ▶ s/ \t/_brinkoftabstop($`) ? "\t\t" : "\t"/eg;
Branch: 1 branch
BranchTrueFalseTotal
_brinkoftabstop($`) ? : 0 0 0
405 }
406 0 push @ret, $_;
407 }
408 0 return @ret;
409 }
410  
411 #
412 # This sub ensures that while cleaning space-followed-by-TAB issues,
413 # we don't blindly cleanup at tab boundaries.
414 #
415 # For instance, "1234567 \t" should change to "1234567\t\t" and not to
416 # "1234567\t", which would not look the same as the original.
417 #
418 0 ▶sub _brinkoftabstop {
Subroutine _brinkoftabstop: not called CC=1 SCAR=6.9
419 0 my $s = shift;
420 0 $s =~ s/.*\t//;
421 0 return length($s) % 8 == 7;
422 }
423  
424 1 1;
425  
426 =head1 ACKNOWLEDGMENTS
427  
428 This module is based on the original B<whitespace> program written by
429 Bradley W. White, distributed under the same license as the module
430 itself.
431  
432 =head1 AUTHORS
433  
434 Rajesh Vaidheeswarran E<lt>rv@gnu.orgE<gt>
435  
436 Bradley W. White
437  
438 =head1 LICENSE
439  
440 Copyright (C) 2000-2001 Rajesh Vaidheeswarran
441  
442 All rights reserved.
443  
444 This program is free software; you can redistribute it and/or modify
445 it under the same terms as Perl itself.
446  
447 =cut
↑ Summary