#!/usr/bin/perl use warnings; use strict; my $n = shift; my $text = 'A huge string separated by lots and lots of words that I\'d like to break up into n shorter strings of length y'; while ($text) { $text = cut_head( $text, $n ); } exit; sub cut_head { my ($text, $wrap_length) = @_; my $actual_length = 0; WORD: while ($actual_length < $wrap_length and $actual_length < length($text) ) { my $index = get_next_word( $text, $actual_length, $wrap_length ); $actual_length ||= $index; last WORD if $index >= $wrap_length; $actual_length = $index; next WORD; } my $head = substr( $text, 0, $actual_length); my $tail = substr( $text, $actual_length ); print "$head\n"; return $tail; }