I've got a script that uses Text::Wrap, and have encountered a problem (actually, two problems), and was wondering if other have experienced anything similar.

First, I've opened a text file, and shoved it into an array:
@CONTENT

If I use the code:
$Text::Wrap::columns = '40'; print wrap("","",@CONTENT);
It wraps (on word boundaries), BUT when it encounters a lone newline (a blank line), it's adding a space at the end of the line preceding the lone newline, and adding a space at the beginning of the line following the lone newline.

When it encounters a newline at the end of a line, it's adding a space to the beginning of the following line.

If I revise the code above to:
$Text::Wrap::columns = '40'; push(@YO, wrap("","",@CONTENT)); foreach (@YO) { $_ =~ s/(\s)?\n\s/\n/g; print; }
The spaces are eliminated, but that sure is inefficient (and ugly). It seems something's going on with line feeds that I don't understand.

The second problem (and more important) has to do with words that exceed the $Text::Wrap::columns value.

If my content file has a long URL (let's say 55 characters, and the wrap is set at 40 characters), Text::Wrap splits the URL over two lines (which is OK), but moves the first portion of the split, the second portion of the split, AND all the remaining paragraph to the END of the output.

Is there something obvious that I'm missing? I'm just cutting my teeth using modules, and the Perl Module Reference describes Text::Wrap as "a very simple paragraph formatter".


In reply to Text::Wrap - having problems with long words by Hagbone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.