1Text::Wrap(3) User Contributed Perl Documentation Text::Wrap(3)
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 boundaries.
42 Indentation is controlled for the first line ($initial_tab) and all
43 subsequent lines ($subsequent_tab) independently. Please note:
44 $initial_tab and $subsequent_tab are the literal strings that will be
45 used: it is unlikely 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
53 "wrap()" compresses trailing whitespace into one newline, and "fill()"
54 deletes all trailing whitespace.
55
56 Both "wrap()" and "fill()" return a single string.
57
58 Unlike the old Unix fmt(1) utility, this module correctly accounts for
59 any Unicode combining characters (such as diacriticals) that may occur
60 in each line for both expansion and unexpansion. These are overstrike
61 characters that do not increment the logical position. Make sure you
62 have the appropriate Unicode settings enabled.
63
65 "Text::Wrap::wrap()" has a number of variables that control its
66 behavior. Because other modules might be using "Text::Wrap::wrap()" it
67 is suggested that you leave these variables alone! If you can't do
68 that, then use "local($Text::Wrap::VARIABLE) = YOURVALUE" when you
69 change the values so that the original value is restored. This
70 "local()" trick will not work if you import the variable into your own
71 namespace.
72
73 Lines are wrapped at $Text::Wrap::columns columns (default value: 76).
74 $Text::Wrap::columns should be set to the full width of your output
75 device. In fact, every resulting line will have length of no more than
76 "$columns - 1".
77
78 It is possible to control which characters terminate words by modifying
79 $Text::Wrap::break. Set this to a string such as '[\s:]' (to break
80 before spaces or colons) or a pre-compiled regexp such as "qr/[\s']/"
81 (to break before spaces or apostrophes). The default is simply '\s';
82 that is, words are terminated by spaces. (This means, among other
83 things, that trailing punctuation such as full stops or commas stay
84 with the word they are "attached" to.) Setting $Text::Wrap::break to a
85 regular expression that doesn't eat any characters (perhaps just a
86 forward look-ahead assertion) will cause warnings.
87
88 Beginner note: In example 2, above $columns is imported into the local
89 namespace, and set locally. In example 3, $Text::Wrap::columns is set
90 in its own namespace without importing it.
91
92 "Text::Wrap::wrap()" starts its work by expanding all the tabs in its
93 input into spaces. The last thing it does it to turn spaces back into
94 tabs. If you do not want tabs in your results, set
95 $Text::Wrap::unexpand to a false value. Likewise if you do not want to
96 use 8-character tabstops, set $Text::Wrap::tabstop to the number of
97 characters you do want for your tabstops.
98
99 If you want to separate your lines with something other than "\n" then
100 set $Text::Wrap::separator to your preference. This replaces all
101 newlines with $Text::Wrap::separator. If you just want to preserve
102 existing newlines but add new breaks with something else, set
103 $Text::Wrap::separator2 instead.
104
105 When words that are longer than $columns are encountered, they are
106 broken up. "wrap()" adds a "\n" at column $columns. This behavior can
107 be overridden by setting $huge to 'die' or to 'overflow'. When set to
108 'die', large words will cause "die()" to be called. When set to
109 'overflow', large words will be left intact.
110
111 Historical notes: 'die' used to be the default value of $huge. Now,
112 'wrap' is the default value.
113
115 Code:
116
117 print wrap("\t","",<<END);
118 This is a bit of text that forms
119 a normal book-style indented paragraph
120 END
121
122 Result:
123
124 " This is a bit of text that forms
125 a normal book-style indented paragraph
126 "
127
128 Code:
129
130 $Text::Wrap::columns=20;
131 $Text::Wrap::separator="|";
132 print wrap("","","This is a bit of text that forms a normal book-style paragraph");
133
134 Result:
135
136 "This is a bit of|text that forms a|normal book-style|paragraph"
137
139 This module comes in two flavors: one for modern perls (5.10 and above)
140 and one for ancient obsolete perls. The version for modern perls has
141 support for Unicode. The version for old perls does not. You can tell
142 which version you have installed by looking at $Text::Wrap::SUBVERSION:
143 it is "old" for obsolete perls and "modern" for current perls.
144
145 This man page is for the version for modern perls and so that's
146 probably what you've got.
147
149 For correct handling of East Asian half- and full-width characters, see
150 Text::WrapI18N. For more detailed controls: Text::Format.
151
153 David Muir Sharnoff <cpan@dave.sharnoff.org> with help from Tim Pierce
154 and many many others.
155
157 Copyright (C) 1996-2009 David Muir Sharnoff. Copyright (C) 2012-2013
158 Google, Inc. This module may be modified, used, copied, and
159 redistributed at your own risk. Although allowed by the preceding
160 license, please do not publicly redistribute modified versions of this
161 code with the name "Text::Wrap" unless it passes the unmodified
162 Text::Wrap test suite.
163
164
165
166perl v5.32.1 2021-01-27 Text::Wrap(3)