1 2package Text::Tabs; 3 4require Exporter; 5 6@ISA = (Exporter); 7@EXPORT = qw(expand unexpand $tabstop); 8 9use vars qw($VERSION $tabstop $debug); 10$VERSION = 98.112801; 11 12use strict; 13 14BEGIN { 15 $tabstop = 8; 16 $debug = 0; 17} 18 19sub expand 20{ 21 my (@l) = @_; 22 for $_ (@l) { 23 1 while s/(^|\n)([^\t\n]*)(\t+)/ 24 $1. $2 . (" " x 25 ($tabstop * length($3) 26 - (length($2) % $tabstop))) 27 /sex; 28 } 29 return @l if wantarray; 30 return $l[0]; 31} 32 33sub unexpand 34{ 35 my (@l) = @_; 36 my @e; 37 my $x; 38 my $line; 39 my @lines; 40 my $lastbit; 41 for $x (@l) { 42 @lines = split("\n", $x, -1); 43 for $line (@lines) { 44 $line = expand($line); 45 @e = split(/(.{$tabstop})/,$line,-1); 46 $lastbit = pop(@e); 47 $lastbit = '' unless defined $lastbit; 48 $lastbit = "\t" 49 if $lastbit eq " "x$tabstop; 50 for $_ (@e) { 51 if ($debug) { 52 my $x = $_; 53 $x =~ s/\t/^I\t/gs; 54 print "sub on '$x'\n"; 55 } 56 s/ +$/\t/; 57 } 58 $line = join('',@e, $lastbit); 59 } 60 $x = join("\n", @lines); 61 } 62 return @l if wantarray; 63 return $l[0]; 64} 65 661; 67__END__ 68 69 70=head1 NAME 71 72Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1) 73 74=head1 SYNOPSIS 75 76 use Text::Tabs; 77 78 $tabstop = 4; 79 @lines_without_tabs = expand(@lines_with_tabs); 80 @lines_with_tabs = unexpand(@lines_without_tabs); 81 82=head1 DESCRIPTION 83 84Text::Tabs does about what the unix utilities expand(1) and unexpand(1) 85do. Given a line with tabs in it, expand will replace the tabs with 86the appropriate number of spaces. Given a line with or without tabs in 87it, unexpand will add tabs when it can save bytes by doing so. Invisible 88compression with plain ascii! 89 90=head1 BUGS 91 92expand doesn't handle newlines very quickly -- do not feed it an 93entire document in one string. Instead feed it an array of lines. 94 95=head1 AUTHOR 96 97David Muir Sharnoff <muir@idiom.com> 98