Perl (Scripting) - Wrap text (Text::Wrap)

by
Jeremy Canfield |
Updated: March 09 2020
| Perl (Scripting) articles
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
Did you find this article helpful?
If so, consider buying me a coffee over at