sub wrap { my ($s, $wrap) = @_; my (@lines, $line); while (length $s > $wrap) { ### Take one extra character so we can see if ### we're wrapping inside a word or not $line = substr($s, 0, $wrap+1); $line =~ s/\s+(\S*)$//; push @lines, $line; ### Add any word fragments back on $s = $1 . substr($s, $wrap); $s =~ s/^\s+//; } push @lines, $s; ## Adds the the last of the $s string to @lines return join "\n", @lines; }