Text::Wrap

in Perl Tips, Miscellaneous
by William Ward on August 2, 2002 8:31 am

Try the Text::Wrap module if you need to reformat text to fit in an 80 character window, or whatever other size you might need. This is a module that comes with Perl, so you don’t have to worry about installing it or whether it will be installed on a machine you will be running your programs on.

It defines two subroutines: wrap() and fill(). The difference between them is that fill() inserts spaces between words so that all the columns line up on both left and right sides. Both will split lines so that no line is greater than 80 characters.

To load the module, just put this at the top of your program or module:

  use Text::Wrap;

You can specify prefix text to appear before the first and before all subsequent lines. So if you want paragraphs where the first line is indented five spaces, and the rest is left-aligned, use:

  print wrap("     ", "", @lines);

Or, if you want the first line at five spaces and the remaining lines indented even more (the first tabstop, typically at eight spaces):

  print wrap("     ", "t", @lines);

But typically both of these strings are empty so that all lines are lined up on the left side:

  print wrap("", "", @lines);

To control the maximum line size (typically 80), do this:

  $Text::Wrap::columns = 132;

If a word longer than $Text::Wrap::columns is encountered, it will be split (but on older versions of Text::Wrap, it will cause an error). This can be controlled on newer versions of Text::Wrap using the variable $huge:

  $Text::Wrap::huge = "die";    # Cause an error
  $Text::Wrap::huge = "wrap";   # Split the word

When setting these variables you may wish to import them to save typing. Here’s how to do that:

  use Text::Wrap qw(wrap $columns $huge);
  $columns = 132;
  $huge = "die";

10 Comments »

  1. i have a while loop on my file.pl perl source code. When i enter a huge number like 1000, the perl source code while function doesn’t wrap the numbers to reach zero on my browswer. Ne code that’ll make it wrap.

    Comment by John — June 29, 2006 @ 5:45 pm

  2. Does your program use the Text::Wrap module? I’m not sure what your code is trying to do. Perhaps if you post an example of the code I might be able to say more.

    Comment by William Ward — June 30, 2006 @ 1:37 pm

  3. well I have a question about word wraping.
    I’ve created an easy to use text guide with perl.
    I’ve devided it into a program that reads the guide and a program that is the guide.
    when I print the part the user is asking for I want it to print it like so:

    “title.
    -step1……
    …….
    -step2……
    …..
    -step3….
    …..

    I’ve used this line of code to do so:
    print wrap(”",” “,”$text”);

    the problem is that is comes out like this:

    “title.
    -step1…

    -step2…

    -step3…
    …”

    my problem is that it puts a space every time there is a new line instead of only doing so when wraping a word.

    I really hope you understood my problem and that you have a sulotion for me.

    thank you for your time =]

    Comment by Yoav Hayun — September 13, 2007 @ 6:22 am

  4. oh… it doesn’t put in spaces.

    (S) = space

    What is want:
    “title.
    -step1…
    (S)…
    -step2…
    (S)…
    -step3…
    (S)…”

    what I get:

    “title. (S)
    (S)-step1…
    (S)…
    (S)-step2…
    (S)…
    (S)-step3…
    (S)…”

    Comment by Yoav Hayun — September 13, 2007 @ 6:25 am

  5. Can you post a bigger piece of your code? I’m not quite sure what you are asking.

    Two things that might be relevant:

    1. Text::Wrap expects each argument to be one line of text. So you might have to split your string up into individual strings, with each string being one line, and then pass those to wrap.

    2. The print function does not insert spaces in between the lines of output. If you put an array inside single quotes, it does. So if that’s what you want, put the text into an array and print “@array” instead. Or use join().

    Comment by William Ward — September 13, 2007 @ 11:26 am

  6. hummm I can’t seem to be able to upload a new comment…

    Comment by Yoav Hayun — September 14, 2007 @ 2:04 pm

  7. ok I guess it was to long. here it is:

    ok I’ll try to explain myself better.
    I have a text in a var. lets say Text:
    my $Text = qq{title
    -step1…
    -step2…
    -step3…};

    now I want to print it. so this is my line of code:

    print wrap(”",” “,”$Text”);

    the problem is that not only there is a space every new line (and not only when a word is being wrapped) but also the first word of the new line is at the same column as the ‘-’ at the beginnig of each step.

    you also must know that there’s is an exe file that reads the guide and a txt file that is the guide (containing all the information) so I cannot use the print function for every step.

    I’m also adding pics:
    what I want:
    http://img209.imageshack.us/img209/8049/wanthz5.png

    what I get:
    http://img297.imageshack.us/img297/5079/getfg8.png

    thank you for your help.

    Comment by Yoav Hayun — September 14, 2007 @ 2:05 pm

  8. it’s been a while and you didn’t answer my question… wanted to know why..
    anyways… hope to hear from you.

    Comment by yoavhayun — September 19, 2007 @ 5:45 am

  9. Sorry I just got back from vacation and have been very busy. I looked at your pics but haven’t had time to figure out the cause of your issue.

    Comment by William Ward — September 19, 2007 @ 2:19 pm

  10. I have the following code that does not work against a large text file (notepad)

    #! / usr/bin/perl -w
    use Text::Wrap qw(wrap $columns $huge);
    $columns = 132;
    $huge = "die";
    #
    #
    $name = $ARGV[0];
    $name1 = $ARGV[1];
    #
    #
    open (INFILE, "$name1");
    while (my $row = ) {
    print OUTFILE wrap('','',$row) ;
    }
    close (INFILE);
    close (OUTFILE);

    The input file is 4MB and contains variable lenght lines of what would seem to be meaningless text. It is actually an EDI file where each record is seperated by a “~” tilde. I need the file reformated to remove any prexisting carriage returns, and the print the output file as lines of 80 characters maximm.

    Can some one help me make adjustments to my code, and understand what is going wrong? This code simply prints each line that breaks at the “~”

    Thanks

    Comment by Mike — October 1, 2007 @ 12:05 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment