Rob
Rob

Reputation: 33

Printing in a readable way (Perl)

Suppose I wish to print quite a long phrase in Perl. Like most, I run Perl codes in cmd (and later on expect to make an executable out of it).

The problem is that because each user has its own size of command line, sometimes the code cuts a word in the middle, and finishes writing it in the next line. For example:

This is a te
xt

I don't want such a thing; I prefer to see "This is a\ntext". But, as said, some users have different sizes of command lines, so I can't edit it manually. Is there something that could be done to print all words in whole?

Upvotes: 2

Views: 167

Answers (1)

ErikR
ErikR

Reputation: 52039

Use the core module Text::Wrap with the module Term::ReadKey to get the current terminal width:

use Term::ReadKey;
use Text::Wrap;

my ($width, $height, $wpixels, $hpixels) = GetTerminalSize();
$Text::Wrap::columns = $width;
print wrap('', '', $text);

Upvotes: 5

Related Questions