sub split_len { ## split_len( $chars, $string[, $limit] ) ## - splits $string into chunks of $chars chars ## - limits number of segments returned to $limit, if provided my ($chars, $string) = @_; my ($i, @result); for ($i = 0; ($i+$chars) < length($string); $i+=$chars) { last if (defined $limit && @result >= $limit); push @result, substr($string, $i, $chars); } # deal with any short remainders return @result if (defined $limit && @result >= $limit); if ($i > length($string)-$chars) { push @result, substr($string, $i); } return @result; }