Hi Anonymous,
The /s modifier means:
Treat the string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
So if I understand your question correctly, simply removing that modifier should do what you want:
my $len = 20;
# 345678901234567890
my $text = <<'ENDTXT';
One
Two Three Four Five Six Seven
Eight Nine Ten
Eleven Twelve Thirteen Fourteen
ENDTXT
my @lines = $text =~ /(.{1,$len}\W)/gm;
# remove leftover whitespace at ends of lines
s/\s+$// for @lines;
print "<$_>\n" for @lines;
__END__
<One>
<Two Three Four Five>
<Six Seven>
<Eight Nine Ten>
<Eleven Twelve>
<Thirteen Fourteen>
Note that there's also the core module Text::Wrap that you could take a look at. Update 2: A an example of Text::Wrap that does the same thing as is similar to the above. Uncomment the tr/// operation to reflow the entire text:
use Text::Wrap;
$Text::Wrap::columns = 20;
#$text=~tr/\n/ /;
print wrap('', '', $text);
Update 1: The following modification to the regex eliminates the need for the s/\s+$// for @lines; above (works because \s includes newline). Update 3: Actually, the following doesn't behave the same way the original regex does. It's hard to make an alternative suggestion without knowing what your intentions are here: do you definitely want to include one more non-word character at the end of the matched string, even whitespace, or did you perhaps mean a word boundary \b? If you could provide some sample input and expected output for different cases, and/or explain more about how you want the splitting to occur, that would help in making an alternate suggestion.
my @lines = $text =~ /(.{1,$len})\s+/gm;
Hope this helps, -- Hauke D |