6 @EXPORT = qw(wrap fill);
7 @EXPORT_OK = qw($columns $break $huge);
9 $VERSION = 2000.06292219; #GMT
11 use vars qw($VERSION $columns $debug $break $huge);
15 $columns = 76; # <= screen width
18 $huge = 'wrap'; # alternatively: 'die'
21 use Text::Tabs qw(expand unexpand);
25 my ($ip, $xp, @t) = @_;
29 my $t = _linearize(@t);
32 my $ll = $columns - length(expand($ip)) - 1;
33 my $nll = $columns - length(expand($xp)) - 1;
37 while ($t !~ /^\s*$/) {
38 if ($t =~ s/^([^\n]{0,$ll})($break|\Z(?!\n))//x) {
39 $r .= unexpand($nl . $lead . $1);
41 } elsif ($huge eq 'wrap' && $t =~ s/^([^\n]{$ll})//) {
42 $r .= unexpand($nl . $lead . $1);
44 } elsif ($huge eq 'die') {
45 die "couldn't wrap '$t'";
47 die "This shouldn't happen";
56 print "-----------$r---------\n" if $debug;
58 print "Finish up with '$lead', '$t'\n" if $debug;
60 $r .= $lead . $t if $t ne "";
62 print "-----------$r---------\n" if $debug;;
67 my @lines = expand(@_);
69 # Join the lines together, adding in extra whitespace only where needed
70 # to keep words seperated.
71 my $text = join "", map { /\s+\Z/ ? $_ : $_.' ' } @lines[0..$#lines-1];
80 my ($ip, $xp, @raw) = @_;
84 for $pp (split(/\n\s+/, join("\n",@raw))) {
86 my $x = wrap($ip, $xp, $pp);
90 # if paragraph_indent is the same as line_indent,
91 # separate paragraphs with blank lines
93 return join ($ip eq $xp ? "\n\n" : "\n", @para);
101 Text::Wrap - line wrapping to form simple paragraphs
107 print wrap($initial_tab, $subsequent_tab, @text);
108 print fill($initial_tab, $subsequent_tab, @text);
110 use Text::Wrap qw(wrap $columns $huge);
118 Text::Wrap::wrap() is a very simple paragraph formatter. It formats a
119 single paragraph at a time by breaking lines at word boundaries.
120 Indentation is controlled for the first line ($initial_tab) and
121 all subsequent lines ($subsequent_tab) independently.
123 Lines are wrapped at $Text::Wrap::columns columns.
124 $Text::Wrap::columns should be set to the full width of your output device.
126 When words that are longer than $columns are encountered, they
127 are broken up. Previous versions of wrap() die()ed instead.
128 To restore the old (dying) behavior, set $Text::Wrap::huge to
131 Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
132 each paragraph separately and then joins them together when it's done. It
133 will destroy any whitespace in the original text. It breaks text into
134 paragraphs by looking for whitespace after a newline. In other respects
139 print wrap("\t","","This is a bit of text that forms
140 a normal book-style paragraph");
144 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and