sub splitit { my ($left, @words, $right, $doit) = split /\s+/, shift; $right = pop @words; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . ' ' . shift @rest, $right, @rest); } else { return $doit->($left, pop(@rest) . ' ' . $right, @rest); } } return $left, $right; }; $doit->( $left, $right, @words); } while () { s/\s+$//; print join(" | ", splitit($_)), "\n" unless /^$/;; } __DATA__ Now is the time for all good men to come to the aid of their party. The quick red fox jumped over the lazy brown dog. Time flies like an arrow, fruit flies like a banana. If you tell me, I'll forget. If you show me, I'll remember. If you involve me, I'm calling the cops. #### #!/usr/bin/env perl # # split a sentence into two roughly equal parts # use strict; use warnings; sub splitit { my $sentence = shift; my @words = split /(\s+)/, $sentence; my $doit; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . shift @rest, $right, @rest); } else { return $doit->($left, pop(@rest) . $right, @rest); } } return $left, $right; }; return $doit->( '', '', @words); } #### sub splitit { my @words = split /(\s+)/, shift; my ($left, $right) = ('', ''); while (@words) { if (length($left) < length($right)) { $left .= shift @words; } else { $right = pop(@words) . $right; } } return $left, $right; }