More Code
# ...
while(@words) {
my $w = shift @words;
my $lw = length $w;
if($cols_left > $lw) {
$para .= ' ' if $cols_left < $self->columns;
$para .= $w;
$cols_left -= $lw;
}
elsif($lw <= $self->columns) {
$para .= "\n$w";
$cols_left = $self->columns - $lw;
}
else {
my($part, $rest) = _split_at($w, $self->columns);
$para .= "\n$part\n";
$cols_left = $self->columns;
unshift @words, $rest if length $rest;
}
}
# ...
sub _split_at {
my($word, $cols) = @_;
my $part = substr $word, 0, $cols - 1, '';
return "$part-", $word;
}