6 @EXPORT = qw(wrap fill);
7 @EXPORT_OK = qw($columns $break $huge);
11 use vars qw($VERSION $columns $debug $break $huge $unexpand $tabstop
16 $columns = 76; # <= screen width
19 $huge = 'wrap'; # alternatively: 'die' or 'overflow'
25 use Text::Tabs qw(expand unexpand);
29 my ($ip, $xp, @t) = @_;
31 local($Text::Tabs::tabstop) = $tabstop;
34 my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
36 my $ll = $columns - length(expand($ip)) - 1;
37 my $nll = $columns - length(expand($xp)) - 1;
44 while ($t !~ /\G\s*\Z/gc) {
45 if ($t =~ /\G([^\n]{0,$ll})($break|\z)/xmgc) {
47 ? unexpand($nl . $lead . $1)
50 } elsif ($huge eq 'wrap' && $t =~ /\G([^\n]{$ll})/gc) {
52 ? unexpand($nl . $lead . $1)
54 $remainder = $separator;
55 } elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)($break|\z)/xmgc) {
57 ? unexpand($nl . $lead . $1)
60 } elsif ($huge eq 'die') {
61 die "couldn't wrap '$t'";
63 die "This shouldn't happen";
72 print "-----------$r---------\n" if $debug;
74 print "Finish up with '$lead'\n" if $debug;
76 $r .= $lead . substr($t, pos($t), length($t)-pos($t))
77 if pos($t) ne length($t);
79 print "-----------$r---------\n" if $debug;;
86 my ($ip, $xp, @raw) = @_;
90 for $pp (split(/\n\s+/, join("\n",@raw))) {
92 my $x = wrap($ip, $xp, $pp);
96 # if paragraph_indent is the same as line_indent,
97 # separate paragraphs with blank lines
99 my $ps = ($ip eq $xp) ? "\n\n" : "\n";
100 return join ($ps, @para);
108 Text::Wrap - line wrapping to form simple paragraphs
116 $initial_tab = "\t"; # Tab before first line
117 $subsequent_tab = ""; # All other lines flush left
119 print wrap($initial_tab, $subsequent_tab, @text);
120 print fill($initial_tab, $subsequent_tab, @text);
122 @lines = wrap($initial_tab, $subsequent_tab, @text);
124 @paragraphs = fill($initial_tab, $subsequent_tab, @text);
128 use Text::Wrap qw(wrap $columns $huge);
130 $columns = 132; # Wrap at 132 characters
139 $Text::Wrap::columns = 72;
140 print wrap('', '', @text);
144 C<Text::Wrap::wrap()> is a very simple paragraph formatter. It formats a
145 single paragraph at a time by breaking lines at word boundries.
146 Indentation is controlled for the first line (C<$initial_tab>) and
147 all subsquent lines (C<$subsequent_tab>) independently. Please note:
148 C<$initial_tab> and C<$subsequent_tab> are the literal strings that will
149 be used: it is unlikley you would want to pass in a number.
151 Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
152 each paragraph separately and then joins them together when it's done. It
153 will destory any whitespace in the original text. It breaks text into
154 paragraphs by looking for whitespace after a newline. In other respects
159 C<Text::Wrap::wrap()> has a number of variables that control its behavior.
160 Because other modules might be using C<Text::Wrap::wrap()> it is suggested
161 that you leave these variables alone! If you can't do that, then
162 use C<local($Text::Wrap::VARIABLE) = YOURVALUE> when you change the
163 values so that the original value is restored. This C<local()> trick
164 will not work if you import the variable into your own namespace.
166 Lines are wrapped at C<$Text::Wrap::columns> columns. C<$Text::Wrap::columns>
167 should be set to the full width of your output device. In fact,
168 every resulting line will have length of no more than C<$columns - 1>.
170 It is possible to control which characters terminate words by
171 modifying C<$Text::Wrap::break>. Set this to a string such as
172 C<'[\s:]'> (to break before spaces or colons) or a pre-compiled regexp
173 such as C<qr/[\s']/> (to break before spaces or apostrophes). The
174 default is simply C<'\s'>; that is, words are terminated by spaces.
175 (This means, among other things, that trailing punctuation such as
176 full stops or commas stay with the word they are "attached" to.)
178 Beginner note: In example 2, above C<$columns> is imported into
179 the local namespace, and set locally. In example 3,
180 C<$Text::Wrap::columns> is set in its own namespace without importing it.
182 C<Text::Wrap::wrap()> starts its work by expanding all the tabs in its
183 input into spaces. The last thing it does it to turn spaces back
184 into tabs. If you do not want tabs in your results, set
185 C<$Text::Wrap::unexapand> to a false value. Likewise if you do not
186 want to use 8-character tabstops, set C<$Text::Wrap::tabstop> to
187 the number of characters you do want for your tabstops.
189 If you want to separate your lines with something other than C<\n>
190 then set C<$Text::Wrap::seporator> to your preference.
192 When words that are longer than C<$columns> are encountered, they
193 are broken up. C<wrap()> adds a C<"\n"> at column C<$columns>.
194 This behavior can be overridden by setting C<$huge> to
195 'die' or to 'overflow'. When set to 'die', large words will cause
196 C<die()> to be called. When set to 'overflow', large words will be
199 Historical notes: 'die' used to be the default value of
200 C<$huge>. Now, 'wrap' is the default value.
204 print wrap("\t","","This is a bit of text that forms
205 a normal book-style paragraph");
209 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and