How to wrap text in Perl (fold)

Home > Search > How-to
  by

The Text::Wrap module can be used to wrap text in Perl. In this example, the text will be wrapped at every 20 characters.

use Text::Wrap;
$Text::Wrap::columns = 20;

$example = "A long string of data A long string of data A long string of data A long string of data";

print wrap('', '', $example);

 

Running this script will produce  the following output. Notice that instead of wrapping at 20 characters exactly, whole words are not split.

A long string of 
data A long string 
of data A long 
string of data A 
long string of data

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments