Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Answer: How can I split a line close to a certain length, but on a word boundary?

( #8730=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How can I split a line on word boundaries closest to a certain length? contributed by poznick

I ended up using:
$rest = $messagebody;
@text=();
while($rest ne '') {
    $rest =~ /(.{1,140}\W)/ms;
    push @text, $1;
    $rest = $';
}
Text::Wrap doesn't provide the desired behavior, unless I were to do a tr/// to translate all the newlines into some strange character, do the wrap, and then tr the strange characters back to newlines. This way seemed a little cleaner.

Comment on Answer: How can I split a line close to a certain length, but on a word boundary?
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-07-10 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls