ExtUtils/typemap: better error messages + constant string optimization
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
CommitLineData
4633a7c4 1package Text::Wrap;
2
9a09eeb5 3require Exporter;
4633a7c4 4
0c5a43b5 5@ISA = qw(Exporter);
6@EXPORT = qw(wrap fill);
7@EXPORT_OK = qw($columns $break $huge);
4633a7c4 8
95925ace 9$VERSION = 2006.0711;
b1a524cb 10
37a581db 11use vars qw($VERSION $columns $debug $break $huge $unexpand $tabstop
8dfcc161 12 $separator $separator2);
9a09eeb5 13use strict;
b1a524cb 14
4633a7c4 15BEGIN {
9a09eeb5 16 $columns = 76; # <= screen width
17 $debug = 0;
18 $break = '\s';
3e2ea5df 19 $huge = 'wrap'; # alternatively: 'die' or 'overflow'
37a581db 20 $unexpand = 1;
21 $tabstop = 8;
22 $separator = "\n";
8dfcc161 23 $separator2 = undef;
4633a7c4 24}
25
9a09eeb5 26use Text::Tabs qw(expand unexpand);
27
4633a7c4 28sub wrap
29{
9a09eeb5 30 my ($ip, $xp, @t) = @_;
31
37a581db 32 local($Text::Tabs::tabstop) = $tabstop;
9a09eeb5 33 my $r = "";
3e2ea5df 34 my $tail = pop(@t);
37a581db 35 my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
9a09eeb5 36 my $lead = $ip;
37 my $ll = $columns - length(expand($ip)) - 1;
718842b0 38 $ll = 0 if $ll < 0;
9a09eeb5 39 my $nll = $columns - length(expand($xp)) - 1;
40 my $nl = "";
41 my $remainder = "";
42
37a581db 43 use re 'taint';
44
3e2ea5df 45 pos($t) = 0;
95925ace 46 while ($t !~ /\G(?:$break)*\Z/gc) {
47 if ($t =~ /\G([^\n]{0,$ll})($break|\n+|\z)/xmgc) {
37a581db 48 $r .= $unexpand
49 ? unexpand($nl . $lead . $1)
50 : $nl . $lead . $1;
9a09eeb5 51 $remainder = $2;
3e2ea5df 52 } elsif ($huge eq 'wrap' && $t =~ /\G([^\n]{$ll})/gc) {
37a581db 53 $r .= $unexpand
54 ? unexpand($nl . $lead . $1)
55 : $nl . $lead . $1;
8dfcc161 56 $remainder = defined($separator2) ? $separator2 : $separator;
95925ace 57 } elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)($break|\n+|\z)/xmgc) {
37a581db 58 $r .= $unexpand
59 ? unexpand($nl . $lead . $1)
60 : $nl . $lead . $1;
3e2ea5df 61 $remainder = $2;
9a09eeb5 62 } elsif ($huge eq 'die') {
63 die "couldn't wrap '$t'";
95925ace 64 } elsif ($columns < 2) {
65 warn "Increasing \$Text::Wrap::columns from $columns to 2";
66 $columns = 2;
67 return ($ip, $xp, @t);
9a09eeb5 68 } else {
69 die "This shouldn't happen";
70 }
71
72 $lead = $xp;
73 $ll = $nll;
8dfcc161 74 $nl = defined($separator2)
75 ? ($remainder eq "\n"
76 ? "\n"
77 : $separator2)
78 : $separator;
9b599b2a 79 }
9a09eeb5 80 $r .= $remainder;
b1a524cb 81
9a09eeb5 82 print "-----------$r---------\n" if $debug;
b1a524cb 83
3e2ea5df 84 print "Finish up with '$lead'\n" if $debug;
b1a524cb 85
3e2ea5df 86 $r .= $lead . substr($t, pos($t), length($t)-pos($t))
87 if pos($t) ne length($t);
b1a524cb 88
9a09eeb5 89 print "-----------$r---------\n" if $debug;;
3e2ea5df 90
9a09eeb5 91 return $r;
4633a7c4 92}
93
0c5a43b5 94sub fill
95{
96 my ($ip, $xp, @raw) = @_;
97 my @para;
98 my $pp;
99
100 for $pp (split(/\n\s+/, join("\n",@raw))) {
101 $pp =~ s/\s+/ /g;
102 my $x = wrap($ip, $xp, $pp);
103 push(@para, $x);
104 }
105
106 # if paragraph_indent is the same as line_indent,
107 # separate paragraphs with blank lines
108
3e2ea5df 109 my $ps = ($ip eq $xp) ? "\n\n" : "\n";
110 return join ($ps, @para);
0c5a43b5 111}
112
4633a7c4 1131;
68e56a55 114__END__
b1a524cb 115
116=head1 NAME
117
118Text::Wrap - line wrapping to form simple paragraphs
119
120=head1 SYNOPSIS
121
3e2ea5df 122B<Example 1>
123
b1a524cb 124 use Text::Wrap
125
3e2ea5df 126 $initial_tab = "\t"; # Tab before first line
127 $subsequent_tab = ""; # All other lines flush left
128
b1a524cb 129 print wrap($initial_tab, $subsequent_tab, @text);
0c5a43b5 130 print fill($initial_tab, $subsequent_tab, @text);
b1a524cb 131
7b614c55 132 $lines = wrap($initial_tab, $subsequent_tab, @text);
3e2ea5df 133
134 @paragraphs = fill($initial_tab, $subsequent_tab, @text);
135
136B<Example 2>
137
9a09eeb5 138 use Text::Wrap qw(wrap $columns $huge);
b1a524cb 139
3e2ea5df 140 $columns = 132; # Wrap at 132 characters
9a09eeb5 141 $huge = 'die';
142 $huge = 'wrap';
3e2ea5df 143 $huge = 'overflow';
b1a524cb 144
3e2ea5df 145B<Example 3>
95925ace 146
3e2ea5df 147 use Text::Wrap
b1a524cb 148
3e2ea5df 149 $Text::Wrap::columns = 72;
150 print wrap('', '', @text);
9a09eeb5 151
3e2ea5df 152=head1 DESCRIPTION
9a09eeb5 153
37a581db 154C<Text::Wrap::wrap()> is a very simple paragraph formatter. It formats a
3c4b39be 155single paragraph at a time by breaking lines at word boundaries.
3e2ea5df 156Indentation is controlled for the first line (C<$initial_tab>) and
718842b0 157all subsequent lines (C<$subsequent_tab>) independently. Please note:
3e2ea5df 158C<$initial_tab> and C<$subsequent_tab> are the literal strings that will
3c4b39be 159be used: it is unlikely you would want to pass in a number.
3e2ea5df 160
37a581db 161Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
162each paragraph separately and then joins them together when it's done. It
818675a5 163will destroy any whitespace in the original text. It breaks text into
37a581db 164paragraphs by looking for whitespace after a newline. In other respects
165it acts like wrap().
166
95925ace 167Both C<wrap()> and C<fill()> return a single string.
168
37a581db 169=head1 OVERRIDES
170
171C<Text::Wrap::wrap()> has a number of variables that control its behavior.
172Because other modules might be using C<Text::Wrap::wrap()> it is suggested
173that you leave these variables alone! If you can't do that, then
174use C<local($Text::Wrap::VARIABLE) = YOURVALUE> when you change the
175values so that the original value is restored. This C<local()> trick
176will not work if you import the variable into your own namespace.
177
3e2ea5df 178Lines are wrapped at C<$Text::Wrap::columns> columns. C<$Text::Wrap::columns>
179should be set to the full width of your output device. In fact,
180every resulting line will have length of no more than C<$columns - 1>.
181
37a581db 182It is possible to control which characters terminate words by
183modifying C<$Text::Wrap::break>. Set this to a string such as
184C<'[\s:]'> (to break before spaces or colons) or a pre-compiled regexp
185such as C<qr/[\s']/> (to break before spaces or apostrophes). The
186default is simply C<'\s'>; that is, words are terminated by spaces.
187(This means, among other things, that trailing punctuation such as
188full stops or commas stay with the word they are "attached" to.)
189
3e2ea5df 190Beginner note: In example 2, above C<$columns> is imported into
191the local namespace, and set locally. In example 3,
192C<$Text::Wrap::columns> is set in its own namespace without importing it.
193
37a581db 194C<Text::Wrap::wrap()> starts its work by expanding all the tabs in its
195input into spaces. The last thing it does it to turn spaces back
196into tabs. If you do not want tabs in your results, set
818675a5 197C<$Text::Wrap::unexpand> to a false value. Likewise if you do not
37a581db 198want to use 8-character tabstops, set C<$Text::Wrap::tabstop> to
199the number of characters you do want for your tabstops.
200
201If you want to separate your lines with something other than C<\n>
8dfcc161 202then set C<$Text::Wrap::separator> to your preference. This replaces
203all newlines with C<$Text::Wrap::separator>. If you just to preserve
204existing newlines but add new breaks with something else, set
205C<$Text::Wrap::separator2> instead.
37a581db 206
3e2ea5df 207When words that are longer than C<$columns> are encountered, they
208are broken up. C<wrap()> adds a C<"\n"> at column C<$columns>.
209This behavior can be overridden by setting C<$huge> to
210'die' or to 'overflow'. When set to 'die', large words will cause
211C<die()> to be called. When set to 'overflow', large words will be
212left intact.
9b599b2a 213
37a581db 214Historical notes: 'die' used to be the default value of
3e2ea5df 215C<$huge>. Now, 'wrap' is the default value.
216
95925ace 217=head1 EXAMPLES
218
219Code:
220
221 print wrap("\t","",<<END);
222 This is a bit of text that forms
223 a normal book-style indented paragraph
224 END
225
226Result:
227
228 " This is a bit of text that forms
229 a normal book-style indented paragraph
230 "
231
232Code:
233
234 $Text::Wrap::columns=20;
235 $Text::Wrap::separator="|";
236 print wrap("","","This is a bit of text that forms a normal book-style paragraph");
237
238Result:
b1a524cb 239
95925ace 240 "This is a bit of|text that forms a|normal book-style|paragraph"
b1a524cb 241
8dfcc161 242=head1 LICENSE
b1a524cb 243
4fc6b8d8 244David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
95925ace 245many many others. Copyright (C) 1996-2006 David Muir Sharnoff.
8dfcc161 246This module may be modified, used, copied, and redistributed at
247your own risk. Publicly redistributed modified versions must use
248a different name.
b1a524cb 249