This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to version 96.051501
[perl5.git] / lib / Text / Wrap.pm
CommitLineData
4633a7c4
LW
1
2package Text::Wrap;
3
4#
5# This is a very simple paragraph formatter. It formats one
6# paragraph at a time by wrapping and indenting text.
7#
8# Usage:
9#
10# use Text::Wrap;
11#
12# print wrap($initial_tab,$subsequent_tab,@text);
13#
14# You can also set the number of columns to wrap before:
15#
16# $Text::Wrap::columns = 135; # <= width of screen
17#
18# use Text::Wrap qw(wrap $columns);
19# $columns = 70;
20#
21#
22# The first line will be printed with $initial_tab prepended. All
23# following lines will have $subsequent_tab prepended.
24#
25# Example:
26#
27# print wrap("\t","","This is a bit of text that ...");
28#
29# David Muir Sharnoff <muir@idiom.com>
30# Version: 9/21/95
31#
32
cb1a09d0
AD
33=head1 NAME
34
35Text::Wrap -- wrap text into a paragraph
36
37=head1 SYNOPSIS
38
39 use Text::Wrap;
40
41 $Text::Wrap::columns = 20; # Default
42 print wrap("\t","",Hello, world, it's a nice day, isn't it?");
43
44=head1 DESCRIPTION
45
46This module is a simple paragraph formatter that wraps text into a paragraph
47and indents each line. The single exported function, wrap(), takes three
48arguments. The first is included before the first output line, and the
49second argument is included before each subsequest output line. The third
50argument is the text to be wrapped.
51
52=head1 AUTHOR
53
54David Muir Sharnoff <muir@idiom.com>
55
56=cut
57
4633a7c4
LW
58require Exporter;
59
60@ISA = (Exporter);
61@EXPORT = qw(wrap);
62@EXPORT_OK = qw($columns);
63
64BEGIN {
65 $Text::Wrap::columns = 76; # <= screen width
66}
67
68use Text::Tabs;
69use strict;
70
71sub wrap
72{
73 my ($ip, $xp, @t) = @_;
74
75 my $r;
76 my $t = expand(join(" ",@t));
77 my $lead = $ip;
78 my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
79 if ($t =~ s/^([^\n]{0,$ll})\s//) {
80 $r .= unexpand($lead . $1 . "\n");
81 $lead = $xp;
82 my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
83 while ($t =~ s/^([^\n]{0,$ll})\s//) {
84 $r .= unexpand($lead . $1 . "\n");
85 }
86 }
87 die "couldn't wrap '$t'"
88 if length($t) > $ll;
89 $r .= $t;
90 return $r;
91}
92
931;