Detrail 643 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/perl
  2. # This is a script for removing trailing whitespace from lines in files that
  3. # are listed on the command line.
  4. # This subroutine does the work for one file.
  5. sub detrail {
  6. my($file) = $_[0];
  7. my($changed) = 0;
  8. open(IN, "$file") || die "Can't open $file for input";
  9. @lines = <IN>;
  10. close(IN);
  11. foreach (@lines)
  12. {
  13. if (/\s+\n$/)
  14. {
  15. s/\s+\n$/\n/;
  16. $changed = 1;
  17. }
  18. }
  19. if ($changed)
  20. {
  21. open(OUT, ">$file") || die "Can't open $file for output";
  22. print OUT @lines;
  23. close(OUT);
  24. }
  25. }
  26. # This is the main program
  27. $, = ""; # Output field separator
  28. for ($i = 0; $i < @ARGV; $i++) { &detrail($ARGV[$i]); }
  29. # End