1Text::Wrap(3pm) Perl Programmers Reference Guide Text::Wrap(3pm)
2
3
4
6 Text::Wrap - line wrapping to form simple paragraphs
7
9 Example 1
10
11 use Text::Wrap
12
13 $initial_tab = "\t"; # Tab before first line
14 $subsequent_tab = ""; # All other lines flush left
15
16 print wrap($initial_tab, $subsequent_tab, @text);
17 print fill($initial_tab, $subsequent_tab, @text);
18
19 $lines = wrap($initial_tab, $subsequent_tab, @text);
20
21 @paragraphs = fill($initial_tab, $subsequent_tab, @text);
22
23 Example 2
24
25 use Text::Wrap qw(wrap $columns $huge);
26
27 $columns = 132; # Wrap at 132 characters
28 $huge = 'die';
29 $huge = 'wrap';
30 $huge = 'overflow';
31
32 Example 3
33
34 use Text::Wrap
35
36 $Text::Wrap::columns = 72;
37 print wrap('', '', @text);
38
40 "Text::Wrap::wrap()" is a very simple paragraph formatter. It formats
41 a single paragraph at a time by breaking lines at word boundries.
42 Indentation is controlled for the first line ($initial_tab) and all
43 subsequent lines ($subsequent_tab) independently. Please note: $ini‐
44 tial_tab and $subsequent_tab are the literal strings that will be used:
45 it is unlikley you would want to pass in a number.
46
47 Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
48 each paragraph separately and then joins them together when it's done.
49 It will destroy any whitespace in the original text. It breaks text
50 into paragraphs by looking for whitespace after a newline. In other
51 respects it acts like wrap().
52
54 "Text::Wrap::wrap()" has a number of variables that control its behav‐
55 ior. Because other modules might be using "Text::Wrap::wrap()" it is
56 suggested that you leave these variables alone! If you can't do that,
57 then use "local($Text::Wrap::VARIABLE) = YOURVALUE" when you change the
58 values so that the original value is restored. This "local()" trick
59 will not work if you import the variable into your own namespace.
60
61 Lines are wrapped at $Text::Wrap::columns columns. $Text::Wrap::col‐
62 umns should be set to the full width of your output device. In fact,
63 every resulting line will have length of no more than "$columns - 1".
64
65 It is possible to control which characters terminate words by modifying
66 $Text::Wrap::break. Set this to a string such as '[\s:]' (to break
67 before spaces or colons) or a pre-compiled regexp such as "qr/[\s']/"
68 (to break before spaces or apostrophes). The default is simply '\s';
69 that is, words are terminated by spaces. (This means, among other
70 things, that trailing punctuation such as full stops or commas stay
71 with the word they are "attached" to.)
72
73 Beginner note: In example 2, above $columns is imported into the local
74 namespace, and set locally. In example 3, $Text::Wrap::columns is set
75 in its own namespace without importing it.
76
77 "Text::Wrap::wrap()" starts its work by expanding all the tabs in its
78 input into spaces. The last thing it does it to turn spaces back into
79 tabs. If you do not want tabs in your results, set $Text::Wrap::unex‐
80 pand to a false value. Likewise if you do not want to use 8-character
81 tabstops, set $Text::Wrap::tabstop to the number of characters you do
82 want for your tabstops.
83
84 If you want to separate your lines with something other than "\n" then
85 set $Text::Wrap::separator to your preference. This replaces all new‐
86 lines with $Text::Wrap::separator. If you just to preserve existing
87 newlines but add new breaks with something else, set $Text::Wrap::sepa‐
88 rator2 instead.
89
90 When words that are longer than $columns are encountered, they are bro‐
91 ken up. "wrap()" adds a "\n" at column $columns. This behavior can be
92 overridden by setting $huge to 'die' or to 'overflow'. When set to
93 'die', large words will cause "die()" to be called. When set to 'over‐
94 flow', large words will be left intact.
95
96 Historical notes: 'die' used to be the default value of $huge. Now,
97 'wrap' is the default value.
98
100 print wrap("\t","","This is a bit of text that forms
101 a normal book-style paragraph");
102
104 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and many
105 many others. Copyright (C) 1996-2002 David Muir Sharnoff. This module
106 may be modified, used, copied, and redistributed at your own risk.
107 Publicly redistributed modified versions must use a different name.
108
109
110
111perl v5.8.8 2001-09-21 Text::Wrap(3pm)