pastebin - collaborative debugging tool
kpaste.net RSS


spaces 2 tabs, source code
Posted by Anonymous on Thu 20th Sep 2012 05:47
raw | new post

  1. #!/usr/bin/perl
  2. # This script translates white space to tabs,
  3. # if they come in groups of four. It takes one argument: 'text file'.
  4.  
  5. use 5.14.0;
  6. use strict;
  7. use warnings;
  8. use File::Basename qw(basename);
  9. use File::Copy qw(move);
  10.  
  11. my($infile, @content);
  12. my $tab = ' {8}';
  13.  
  14. if (!$ARGV[0] || !-T $ARGV[0]) {
  15.         say "Usage: " . basename($0) . " [ text file ]";
  16.         exit;
  17. } else { $infile = $ARGV[0]; }
  18.  
  19. my $outfile = $infile . '.' . int(rand(10000));
  20. @content = `cat '$infile'`;
  21.  
  22. foreach my $n (0 .. (scalar(@content) - 1)) {
  23.         my($match) = $content[$n] =~ /^( +)/;   # Find lines that start with
  24.         if ($match) {                                                   # spaces.
  25.                 my $write = $match;
  26.                 $write =~ s/($tab)/\t/g; # Translate individual spaces to tabs.
  27.                 $content[$n] =~ s/^$match/$write/;
  28.         }
  29. }
  30.  
  31. open(FILE, '>', $outfile) or die "Can't open '$outfile': $!";
  32. print FILE @content;
  33. close(FILE) or die "Can't close '$outfile': $!";
  34.  
  35. move($outfile, $infile) or die "Can't rename '$outfile': $!";

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at