2

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?

Rob
  • 33
  • 2
  • 2
    You need to get the terminal width - see this questions [How do I retrieve the terminal width in perl](http://stackoverflow.com/questions/1782107/how-do-i-retrieve-the-terminal-width-in-perl) Also see the core module [Text::Wrap](http://perldoc.perl.org/Text/Wrap.html) – ErikR Aug 06 '11 at 13:00

1 Answers1

5

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);
ErikR
  • 51,541
  • 9
  • 73
  • 124