Text::Wrap is usually the best way to go in these matters. Here is a simple regex that has worked for me in the past.
use strict;
use warnings;
use diagnostics;
my $inp = 'This sentence will have more than 120 characters and i want
+ to '.
'truncate this string into two lines containing 60 character
+s each '.
'and ignore characters above 140 in length';
my $linelen = 60;
my $matchlen = $linelen - 1;
# this is what you get with your basic substring parsing;
my $line01 = substr($inp,0,60);
my $line02 = substr($inp,60,60);
my $line03 = substr($inp,120);
print '[',length($line01),'] : %',$line01,'%',"\n";
print '[',length($line02),'] : %',$line02,'%',"\n";
print '[',length($line03),'] : %',$line03,'%',"\n";
print "\n";
# this regex splits on a max of 59 chars plus one "terminating" char;
# in this case this is just another word char (as defined by \w). this
+ is really
# only relevant if the line of non-breaking characters is exactly line
+len in size.
# the very last capture is the rest of the text so, theoretically, you
+ could
# repeat the pattern for as many match sequences as desire. useful if
+the "terminator"
# changes for each field. as a side note, the regex will discard all n
+on-word chars
# at the split location. if needed, you could change the \W+ to any cl
+ass of chars
# such as [\s,-:]+
$inp =~ m/^(.{0,$matchlen}\w??)\W+(.{0,$matchlen}\w??)\W+(.*)/;
$line01 = ($1 || '');
$line02 = ($2 || '');
$line03 = ($3 || '');
print '[',length($line01),'] : %',$line01,'%',"\n";
print '[',length($line02),'] : %',$line02,'%',"\n";
print '[',length($line03),'] : %',$line03,'%',"\n";
print "\n";
# the added advantage of this construct is that it allows you to do so
+mething
# with the last matching character if needed
$inp =~ m/^(.{0,$matchlen})(\w)??\W+(.{0,$matchlen})(\w)??\W+(.*)/;
$line01 = ($1 || '').($2 || '');
$line02 = ($3 || '').($4 || '');
$line03 = ($5 || '');
print '[',length($line01),'] : %',$line01,'%',"\n";
print '[',length($line02),'] : %',$line02,'%',"\n";
print '[',length($line03),'] : %',$line03,'%',"\n";
print "\n";
PJ
use strict; use warnings; use diagnostics;