<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Text::Wrap;

use warnings::register;
require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(wrap fill);
@EXPORT_OK = qw($columns $break $huge);

$VERSION = 2009.0305;

use vars qw($VERSION $columns $debug $break $huge $unexpand $tabstop
	$separator $separator2);
use strict;

BEGIN	{
	$columns = 76;  # &lt;= screen width
	$debug = 0;
	$break = '\s';
	$huge = 'wrap'; # alternatively: 'die' or 'overflow'
	$unexpand = 1;
	$tabstop = 8;
	$separator = "\n";
	$separator2 = undef;
}

use Text::Tabs qw(expand unexpand);

sub wrap
{
	my ($ip, $xp, @t) = @_;

	local($Text::Tabs::tabstop) = $tabstop;
	my $r = "";
	my $tail = pop(@t);
	my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
	my $lead = $ip;
	my $nll = $columns - length(expand($xp)) - 1;
	if ($nll &lt;= 0 &amp;&amp; $xp ne '') {
		my $nc = length(expand($xp)) + 2;
		warnings::warnif "Increasing \$Text::Wrap::columns from $columns to $nc to accommodate length of subsequent tab";
		$columns = $nc;
		$nll = 1;
	}
	my $ll = $columns - length(expand($ip)) - 1;
	$ll = 0 if $ll &lt; 0;
	my $nl = "";
	my $remainder = "";

	use re 'taint';

	pos($t) = 0;
	while ($t !~ /\G(?:$break)*\Z/gc) {
		if ($t =~ /\G([^\n]{0,$ll})($break|\n+|\z)/xmgc) {
			$r .= $unexpand 
				? unexpand($nl . $lead . $1)
				: $nl . $lead . $1;
			$remainder = $2;
		} elsif ($huge eq 'wrap' &amp;&amp; $t =~ /\G([^\n]{$ll})/gc) {
			$r .= $unexpand 
				? unexpand($nl . $lead . $1)
				: $nl . $lead . $1;
			$remainder = defined($separator2) ? $separator2 : $separator;
		} elsif ($huge eq 'overflow' &amp;&amp; $t =~ /\G([^\n]*?)($break|\n+|\z)/xmgc) {
			$r .= $unexpand 
				? unexpand($nl . $lead . $1)
				: $nl . $lead . $1;
			$remainder = $2;
		} elsif ($huge eq 'die') {
			die "couldn't wrap '$t'";
		} elsif ($columns &lt; 2) {
			warnings::warnif "Increasing \$Text::Wrap::columns from $columns to 2";
			$columns = 2;
			return ($ip, $xp, @t);
		} else {
			die "This shouldn't happen";
		}
			
		$lead = $xp;
		$ll = $nll;
		$nl = defined($separator2)
			? ($remainder eq "\n"
				? "\n"
				: $separator2)
			: $separator;
	}
	$r .= $remainder;

	print "-----------$r---------\n" if $debug;

	print "Finish up with '$lead'\n" if $debug;

	$r .= $lead . substr($t, pos($t), length($t)-pos($t))
		if pos($t) ne length($t);

	print "-----------$r---------\n" if $debug;;

	return $r;
}

sub fill 
{
	my ($ip, $xp, @raw) = @_;
	my @para;
	my $pp;

	for $pp (split(/\n\s+/, join("\n",@raw))) {
		$pp =~ s/\s+/ /g;
		my $x = wrap($ip, $xp, $pp);
		push(@para, $x);
	}

	# if paragraph_indent is the same as line_indent, 
	# separate paragraphs with blank lines

	my $ps = ($ip eq $xp) ? "\n\n" : "\n";
	return join ($ps, @para);
}

1;
__END__

=head1 NAME

Text::Wrap - line wrapping to form simple paragraphs

=head1 SYNOPSIS 

B&lt;Example 1&gt;

	use Text::Wrap;

	$initial_tab = "\t";	# Tab before first line
	$subsequent_tab = "";	# All other lines flush left

	print wrap($initial_tab, $subsequent_tab, @text);
	print fill($initial_tab, $subsequent_tab, @text);

	$lines = wrap($initial_tab, $subsequent_tab, @text);

	@paragraphs = fill($initial_tab, $subsequent_tab, @text);

B&lt;Example 2&gt;

	use Text::Wrap qw(wrap $columns $huge);

	$columns = 132;		# Wrap at 132 characters
	$huge = 'die';
	$huge = 'wrap';
	$huge = 'overflow';

B&lt;Example 3&gt;
	
	use Text::Wrap;

	$Text::Wrap::columns = 72;
	print wrap('', '', @text);

=head1 DESCRIPTION

C&lt;Text::Wrap::wrap()&gt; is a very simple paragraph formatter.  It formats a
single paragraph at a time by breaking lines at word boundaries.
Indentation is controlled for the first line (C&lt;$initial_tab&gt;) and
all subsequent lines (C&lt;$subsequent_tab&gt;) independently.  Please note: 
C&lt;$initial_tab&gt; and C&lt;$subsequent_tab&gt; are the literal strings that will
be used: it is unlikely you would want to pass in a number.

Text::Wrap::fill() is a simple multi-paragraph formatter.  It formats
each paragraph separately and then joins them together when it's done.  It
will destroy any whitespace in the original text.  It breaks text into
paragraphs by looking for whitespace after a newline.  In other respects
it acts like wrap().

Both C&lt;wrap()&gt; and C&lt;fill()&gt; return a single string.

=head1 OVERRIDES

C&lt;Text::Wrap::wrap()&gt; has a number of variables that control its behavior.
Because other modules might be using C&lt;Text::Wrap::wrap()&gt; it is suggested
that you leave these variables alone!  If you can't do that, then 
use C&lt;local($Text::Wrap::VARIABLE) = YOURVALUE&gt; when you change the
values so that the original value is restored.  This C&lt;local()&gt; trick
will not work if you import the variable into your own namespace.

Lines are wrapped at C&lt;$Text::Wrap::columns&gt; columns (default value: 76).
C&lt;$Text::Wrap::columns&gt; should be set to the full width of your output
device.  In fact, every resulting line will have length of no more than
C&lt;$columns - 1&gt;.

It is possible to control which characters terminate words by
modifying C&lt;$Text::Wrap::break&gt;. Set this to a string such as
C&lt;'[\s:]'&gt; (to break before spaces or colons) or a pre-compiled regexp
such as C&lt;qr/[\s']/&gt; (to break before spaces or apostrophes). The
default is simply C&lt;'\s'&gt;; that is, words are terminated by spaces.
(This means, among other things, that trailing punctuation  such as
full stops or commas stay with the word they are "attached" to.)
Setting C&lt;$Text::Wrap::break&gt; to a regular expression that doesn't
eat any characters (perhaps just a forward look-ahead assertion) will
cause warnings.

Beginner note: In example 2, above C&lt;$columns&gt; is imported into
the local namespace, and set locally.  In example 3,
C&lt;$Text::Wrap::columns&gt; is set in its own namespace without importing it.

C&lt;Text::Wrap::wrap()&gt; starts its work by expanding all the tabs in its
input into spaces.  The last thing it does it to turn spaces back
into tabs.  If you do not want tabs in your results, set 
C&lt;$Text::Wrap::unexpand&gt; to a false value.  Likewise if you do not
want to use 8-character tabstops, set C&lt;$Text::Wrap::tabstop&gt; to
the number of characters you do want for your tabstops.

If you want to separate your lines with something other than C&lt;\n&gt;
then set C&lt;$Text::Wrap::separator&gt; to your preference.  This replaces
all newlines with C&lt;$Text::Wrap::separator&gt;.  If you just want to 
preserve existing newlines but add new breaks with something else, set
C&lt;$Text::Wrap::separator2&gt; instead.

When words that are longer than C&lt;$columns&gt; are encountered, they
are broken up.  C&lt;wrap()&gt; adds a C&lt;"\n"&gt; at column C&lt;$columns&gt;.
This behavior can be overridden by setting C&lt;$huge&gt; to
'die' or to 'overflow'.  When set to 'die', large words will cause
C&lt;die()&gt; to be called.  When set to 'overflow', large words will be
left intact.  

Historical notes: 'die' used to be the default value of
C&lt;$huge&gt;.  Now, 'wrap' is the default value.

=head1 EXAMPLES

Code:

  print wrap("\t","",&lt;&lt;END);
  This is a bit of text that forms 
  a normal book-style indented paragraph
  END

Result:

  "	This is a bit of text that forms
  a normal book-style indented paragraph   
  "

Code:

  $Text::Wrap::columns=20;
  $Text::Wrap::separator="|";
  print wrap("","","This is a bit of text that forms a normal book-style paragraph");

Result:

  "This is a bit of|text that forms a|normal book-style|paragraph"

=head1 SEE ALSO

For wrapping multi-byte characters: L&lt;Text::WrapI18N&gt;.
For more detailed controls: L&lt;Text::Format&gt;.

=head1 LICENSE

David Muir Sharnoff &lt;muir@idiom.org&gt; with help from Tim Pierce and
many many others.  Copyright (C) 1996-2009 David Muir Sharnoff.  
This module may be modified, used, copied, and redistributed at
your own risk.  Publicly redistributed versions that are modified 
must use a different name.

</pre></body></html>