If you have a line of text you wish to break into two lines of nearly the same length, this regex is for you.
$str = "of course, japhy thought of this one"; $half = int(length($str)/2); $str =~ s {( ?)([^ ]*)(?<=^.{$half})([^ ]*)( ?)} { length($2) < length($3) ? "\n$2$3$4" : "$1$2$3\n" }e; print $str; __END__ of course, japhy thought of this one
This determines the midpoint of your string, and then looks for a space (if there is one) followed by non-spaces, stopping at the middle of the string, and then looks for non-spaces followed by a space (if there is one). Then, based on the lengths of the non-space sections, it determines where to insert the newline. Centering this content, if wanted, is then child's play.