Book Home Programming PerlSearch this book

32.43. Text::Wrap

use Text::Wrap;  # Imports wrap().

@lines = (<<"EO_G&S" =~ /\S.*\S/g);

    This particularly rapid,
    unintelligible
    patter isn't generally
    heard,
    and if
    it is, it
    doesn't matter.

EO_G&S

$Text::Wrap::columns = 50;
print wrap(" " x 8, " " x 3, @lines), "\n";
This prints:
           This particularly rapid, unintelligible
      patter isn't generally heard, and if it is, it
      doesn't matter.
The Text::Wrap module implements a simple paragraph formatter. Its wrap function formats a single paragraph at a time by breaking lines at word boundaries. The first argument is the prefix prepended to the first line returned. The second argument is the prefix string used for all lines save the first. All remaining arguments are joined together using a newline as the separator and returned as one reformatted paragraph string. You'll have to figure out your terminal's width yourself, or at least specify what you want in $Text::Wrap::columns. Although one could use the TIOCGWINSZioctl call to figure out the number of columns, it would be easier for those not used to C programming to install the CPAN module Term::ReadKey and use that module's GetTerminalSize routine.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.