updated to Text::Wrap 98.112801 from CPAN; one published change
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
1 package Text::Wrap;
2
3 require Exporter;
4
5 @ISA = (Exporter);
6 @EXPORT = qw(wrap);
7 @EXPORT_OK = qw($columns $wraplong);
8
9 $VERSION = 98.112801;
10
11 use vars qw($VERSION $columns $debug $break $huge);
12 use strict;
13
14 BEGIN   {
15         $columns = 76;  # <= screen width
16         $debug = 0;
17         $break = '\s';
18         $huge = 'wrap'; # alternatively: 'die'
19 }
20
21 use Text::Tabs qw(expand unexpand);
22
23 sub wrap
24 {
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";
51         }
52         $r .= $remainder;
53
54         print "-----------$r---------\n" if $debug;
55
56         print "Finish up with '$lead', '$t'\n" if $debug;
57
58         $r .= $lead . $t if $t ne "";
59
60         print "-----------$r---------\n" if $debug;;
61         return $r;
62 }
63
64 1;
65 __END__
66
67 =head1 NAME
68
69 Text::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
77         use Text::Wrap qw(wrap $columns $huge);
78
79         $columns = 132;
80         $huge = 'die';
81         $huge = 'wrap';
82
83 =head1 DESCRIPTION
84
85 Text::Wrap::wrap() is a very simple paragraph formatter.  It formats a
86 single paragraph at a time by breaking lines at word boundries.
87 Indentation is controlled for the first line ($initial_tab) and
88 all subsquent lines ($subsequent_tab) independently.  
89
90 Lines are wrapped at $Text::Wrap::columns columns.  
91 $Text::Wrap::columns should be set to the full width of your output device.
92
93 When words that are longer than $columns are encountered, they
94 are broken up.  Previous versions of wrap() die()ed instead.
95 To restore the old (dying) behavior, set $Text::Wrap::huge to
96 'die'.
97
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
105 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
106 many others.
107