This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
doc tweak (from Michael G Schwern <schwern@pobox.com>)
[perl5.git] / lib / Text / Tabs.pm
CommitLineData
cb1a09d0 1
75f92628 2package Text::Tabs;
a0d0e21e
LW
3
4require Exporter;
5
6@ISA = (Exporter);
7@EXPORT = qw(expand unexpand $tabstop);
8
8bc713e8 9use vars qw($VERSION $tabstop $debug);
e8739726 10$VERSION = 98.112801;
8bc713e8
PP
11
12use strict;
13
14BEGIN {
15 $tabstop = 8;
16 $debug = 0;
17}
a0d0e21e
LW
18
19sub expand
20{
e8739726 21 my (@l) = @_;
4633a7c4 22 for $_ (@l) {
8bc713e8
PP
23 1 while s/(^|\n)([^\t\n]*)(\t+)/
24 $1. $2 . (" " x
25 ($tabstop * length($3)
26 - (length($2) % $tabstop)))
27 /sex;
a0d0e21e 28 }
4633a7c4 29 return @l if wantarray;
4fc6b8d8 30 return $l[0];
a0d0e21e
LW
31}
32
33sub unexpand
34{
e8739726 35 my (@l) = @_;
a0d0e21e 36 my @e;
8bc713e8
PP
37 my $x;
38 my $line;
39 my @lines;
40 my $lastbit;
4633a7c4 41 for $x (@l) {
8bc713e8
PP
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);
a0d0e21e 59 }
8bc713e8 60 $x = join("\n", @lines);
a0d0e21e 61 }
4633a7c4 62 return @l if wantarray;
4fc6b8d8 63 return $l[0];
a0d0e21e
LW
64}
65
661;
8bc713e8
PP
67__END__
68
69
70=head1 NAME
71
4fc6b8d8 72Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
8bc713e8
PP
73
74=head1 SYNOPSIS
75
b9b44659 76 use Text::Tabs;
8bc713e8 77
b9b44659
GS
78 $tabstop = 4;
79 @lines_without_tabs = expand(@lines_with_tabs);
80 @lines_with_tabs = unexpand(@lines_without_tabs);
8bc713e8
PP
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
4fc6b8d8 97David Muir Sharnoff <muir@idiom.com>