in reply to formatting strings
I wrote a snippet for someone a while back. It wraps without truncating words. While it doesn't help you preserve $1 (which you could do by copying $1 into a different variable), I thought you might find it useful.
sub line_wrap { my ($str, $max) = @_; my @lines; foreach my $line (split($\, $str)) { # # Collapse spaces and tabs. # s/\s+/ /g; while (length($line) > $max) { my $max2 = $max - 1; if ($line =~ s/^(.{0,$max2}\S)\s+//s) { # Break at space. push(@lines, $1); } else { # No space. $line =~ s/^(.{$max})//s; push(@lines, $1); } } push(@lines, $line); } return join($\, @lines) . $\; }
Known Bugs: Counts tabs as one char, and only does line breaks at a space.
|
|---|