This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
updated to Text::Wrap 98.112801 from CPAN; one published change
[perl5.git] / lib / Text / Wrap.pm
CommitLineData
4633a7c4
LW
1package Text::Wrap;
2
9a09eeb5 3require Exporter;
4633a7c4 4
9a09eeb5 5@ISA = (Exporter);
4633a7c4 6@EXPORT = qw(wrap);
9a09eeb5 7@EXPORT_OK = qw($columns $wraplong);
4633a7c4 8
9a09eeb5 9$VERSION = 98.112801;
b1a524cb 10
9a09eeb5
DMS
11use vars qw($VERSION $columns $debug $break $huge);
12use strict;
b1a524cb 13
4633a7c4 14BEGIN {
9a09eeb5
DMS
15 $columns = 76; # <= screen width
16 $debug = 0;
17 $break = '\s';
18 $huge = 'wrap'; # alternatively: 'die'
4633a7c4
LW
19}
20
9a09eeb5
DMS
21use Text::Tabs qw(expand unexpand);
22
4633a7c4
LW
23sub wrap
24{
9a09eeb5
DMS
25 my ($ip, $xp, @t) = @_;
26
27 my $r = "";
28 my $t = expand(join(" ",@t));
29 my $lead = $ip;
30 my $ll = $columns - length(expand($ip)) - 1;
31 my $nll = $columns - length(expand($xp)) - 1;
32 my $nl = "";
33 my $remainder = "";
34
35 while ($t !~ /^\s*$/) {
36 if ($t =~ s/^([^\n]{0,$ll})($break|\Z(?!\n))//xm) {
37 $r .= unexpand($nl . $lead . $1);
38 $remainder = $2;
39 } elsif ($huge eq 'wrap' && $t =~ s/^([^\n]{$ll})//) {
40 $r .= unexpand($nl . $lead . $1);
41 $remainder = "\n";
42 } elsif ($huge eq 'die') {
43 die "couldn't wrap '$t'";
44 } else {
45 die "This shouldn't happen";
46 }
47
48 $lead = $xp;
49 $ll = $nll;
50 $nl = "\n";
9b599b2a 51 }
9a09eeb5 52 $r .= $remainder;
b1a524cb 53
9a09eeb5 54 print "-----------$r---------\n" if $debug;
b1a524cb 55
9a09eeb5 56 print "Finish up with '$lead', '$t'\n" if $debug;
b1a524cb 57
9a09eeb5 58 $r .= $lead . $t if $t ne "";
b1a524cb 59
9a09eeb5
DMS
60 print "-----------$r---------\n" if $debug;;
61 return $r;
4633a7c4
LW
62}
63
641;
68e56a55 65__END__
b1a524cb
PP
66
67=head1 NAME
68
69Text::Wrap - line wrapping to form simple paragraphs
70
71=head1 SYNOPSIS
72
73 use Text::Wrap
74
75 print wrap($initial_tab, $subsequent_tab, @text);
76
9a09eeb5 77 use Text::Wrap qw(wrap $columns $huge);
b1a524cb
PP
78
79 $columns = 132;
9a09eeb5
DMS
80 $huge = 'die';
81 $huge = 'wrap';
b1a524cb
PP
82
83=head1 DESCRIPTION
84
4fc6b8d8 85Text::Wrap::wrap() is a very simple paragraph formatter. It formats a
b1a524cb
PP
86single paragraph at a time by breaking lines at word boundries.
87Indentation is controlled for the first line ($initial_tab) and
9a09eeb5
DMS
88all subsquent lines ($subsequent_tab) independently.
89
90Lines are wrapped at $Text::Wrap::columns columns.
91$Text::Wrap::columns should be set to the full width of your output device.
92
93When words that are longer than $columns are encountered, they
94are broken up. Previous versions of wrap() die()ed instead.
95To restore the old (dying) behavior, set $Text::Wrap::huge to
96'die'.
9b599b2a 97
b1a524cb
PP
98=head1 EXAMPLE
99
100 print wrap("\t","","This is a bit of text that forms
101 a normal book-style paragraph");
102
103=head1 AUTHOR
104
4fc6b8d8 105David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
9a09eeb5 106many others.
b1a524cb 107