Perl Code for cktb.pl
This is the perl program code for cktb.pl. It looks for blanks or tabs at the end of lines and prints a report of what it finds. This has been updated to use lexical file handles and to print its output using print and printf rather than Perl formats.
#!/usr/bin/env perl # Look for and count the blanks or tabs at the end of lines # --Kent Nassen, 5/26/99, 6/11/99, 6/28/00, 9/20/25 # 6/28/00: only print longest string of whitespace message if the file # had trailing whitespace. Added run_time to header. # 9/20/25: Removed Perl formats. Use print and printf instead. This means # pagination is done manually and page 1 is handled separately because # there are no lines read at that point. Use lexical filehandles and # remove the use of the FileHandle module. Use our() for variables now. #use warnings; #use strict; use Getopt::Std; our ($file, $filename, $input, $line, $len, $whitespace, $match, $matchcount, $ProgName, $version, $pagesize, $longest, $longline, $opt_l, $run_time, $trail, $pagenum, $linecount); $pagesize=54; #$version="v1.1, 6/11/99"; $version="v1.2, 9/20/25"; # p;rint titles on all pages $run_time=localtime(time); ($ProgName = $0) =~ s%.*/%%; # Unix #($ProgName = lc $0) =~ s%.*\\%%; # DOS die "\n*** $ProgName: need a filename to test. \n\ $ProgName: Find lines with trailing white space (tabs or spaces) in files,\ by Kent Nassen, $version\n\ Usage: $ProgName [-l] filename [filename...]\ where -l gives long output, instead of a summary.\n\n" unless defined($file=$ARGV[0]); getopts('l'); sub print_banner { my ($pagenum) = @_; print "\n Page $pagenum\n\n"; print " $ProgName: Find lines with trailing spaces or tabs,", " by Kent Nassen, $version\n", " Scanning for trailing white space in the file: $file\n", " Report generated: $run_time\n\n"; print "\n Trailing WS\n"; print " Line # Length\n"; print " ------- -----------\n"; } # Check a file for trailing blanks/white space my $fh; foreach $file (@ARGV) { $pagenum=1; # Print Page 1 page number, titles, and table headers for opt_l case. if ($pagenum==1) { print " \nPage 1\n\n"; } print " $ProgName: Find lines with trailing spaces or tabs,", " by Kent Nassen, $version\n", " Scanning for trailing white space in the file: $file\n", " Report generated: $run_time\n\n"; if ( $opt_l ) { print " Trailing WS\n"; print " Line # Length\n"; print " ------- -----------\n"; } process($file, $fh, \$pagenum); } sub process { $match=""; $- = 0; $pagenum=2; my ($file, $fh, $pagenum_ref) = @_; $pagenum = defined $pagenum_ref ? $$pagenum_ref : 1; $filename=$file; $line=$matchcount=$longest=$longline=$linecount=0; $input++; unless (open $fh, $filename) { print STDERR "\n $ProgName: *** Can't open $filename: $!\n\n"; return; } while (<$fh>) { chomp; $line++; if (/([\t\s]+)$/) { $$pagenum_ref = $pagenum if defined $pagenum_ref; $match=$_; # keep full line on a match $trail=$1; # the white space; $linecount++; $matchcount++; $len=length($trail); if ($len>$longest) { $longest=$len; $longline=$line; } if ($opt_l) { if ($linecount % 45 == 0) { print_banner($pagenum); $pagenum++; $$pagenum_ref = $pagenum if defined $pagenum_ref && ref($pagenum_ref) eq 'SCALAR'; } # opt_l case: print the table data printf " %5d %7d\n",$line,length($trail); } } } # end of while() if (!$matchcount) { print "\n *** No lines found with trailing white space"; $longest=0; $longline=$line; } print "\n $line total lines in the file '$file'.\n"; if ($matchcount) { print " Longest string of trailing whitespace was $longest", " characters at line $longline.\n\n", "------------------------------------------------------------------------\n"; } $longest=$longline=0; close $fh; }
Back to the cktb Perl Page
Last-modified: Sat Sep 20 23:34:14 EDT 2025