#! c:/perl -w
use strict;
use diagnostics;
##############################################
# vars
use vars qw($aphor $max $min $new @sect $sect $splBRK);
$max=65;
$min=55;
$new="";
@sect="";
$sect[0]="x";
$splBRK="~~";
# uppercase "Z" before each double-tilde solely for ease of checking CLI output.
$aphor="Now is the time for all good men to come toZ ~~the aid of their country as the quickZ ~~red fox jumps over the lazy brown dog's back and the knife runs away with the spoon since this nonsense goes on for waaay faaaaahrrr too long.";
################################################
# convert ~~ s ####
if (length($aphor) > ($max+6) || $aphor =~ /~~/ ) { # plus six is an ARBITRARY allowed_overrun
$aphor =~ s/$splBRK/\n\t/g;
}
else {
$new = "\n\t" . $aphor;
print "$new\n";
exit();
}
&split2;
exit();
##################################################
# sub SPLIT2 #####
sub split2 {
@sect= $aphor =~
m/\G # match with anchor
(?:([\w\x20]{,$min}\n)) # NONcapture (ie, grouping only) NMT $min(letters or spaces)
/gmsx;
# THEN
@sect=$aphor =~
m/\G
(.{$min,$max} # anything of length (between $min and $max) followed by
(?:[\b\x20]) # a NONcapture group of one word boundary (In case no space after ~~) or space
)/gsx; # global, include \n in ., extended patterns
print "\t$_\n" foreach @sect;
if ($') { print "\t$'\n"; }
}
####
Now is the time for all good men to come toZ
the aid of their
country as the quickZ
red fox jumps over the lazy brown dog's
back and the knife runs away with the spoon since this nonsense
goes on for waaay faaaaahrrr too long.
####
Now is the time for all good men to come toZ < \n in file so short line OK
the aid of their country as the quickZ < \n in file so short line OK
red fox jumps over the lazy brown dog's back and the knife runs < 65 chars, including tab & trailing space: OK
away with the spoon since this nonsense goes on for waaay < 59 chars: OK
faaaaahrrr too long. < Last line: short is OK