Hagbone has asked for the wisdom of the Perl Monks concerning the following question:

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".

Replies are listed 'Best First'.
Re: Text::Wrap - having problems with long words
by sauoq (Abbot) on Jan 17, 2003 at 21:36 UTC

    I have tried and have been unable to reproduce your problems. Can you provide more information? In regards to your first question, have you checked to be sure it isn't preserving whitespace which is already there? Do your newlines always come after the last non-space character on your lines?

    -sauoq
    "My two cents aren't worth a dime.";
    
      > ..have you checked to be sure it isn't preserving whitespace which is already there?

      yes.

      >Do your newlines always come after the last non-space character on your lines?

      yes.

      >Can you provide more information?

      Not sure ... I'm calling in a file whose content is at the end of this post ... straightforward stuff .... it's weird the way it's dealing with the blank lines.

      Here's the file content:

      This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details.

      You should have received a copy of the Artistic License with this Kit, in the file named "Artistic". If not, I'll be glad to provide one.

      You should also have received a copy of the GNU General Public License along with this program in the file named "Copying". If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA or visit their web page on the internet at http://www.gnu.org/copyleft/hhhhhhhhhhhhh/hhhhhhhhhhhhhhhhhh/gpl.html.

      For those of you that choose to use the GNU General Public License, my interpretation of the GNU General Public License is that no Perl script falls under the terms of the GPL unless you explicitly put said script under the terms of the GPL yourself. Furthermore, any object code linked with perl does not automatically fall under the terms of the GPL, provided such object code only adds definitions of subroutines and variables, and does not otherwise impair the resulting interpreter from executing any standard Perl script. I consider linking in C subroutines in this manner to be the moral equivalent of defining subroutines in the Perl language itself. You may sell such an object file as proprietary provided that you provide or offer to provide the Perl source, as specified by the GNU General Public License. (This is merely an alternate way of specifying input to the program.) You may also sell a binary produced by the dumping of a running Perl script that belongs to you, provided that you provide or offer to provide the Perl source as specified by the GPL. (The fact that a Perl interpreter and your code are in the same binary file is, in this case, a form of mere aggregation.) This is my interpretation of the GPL. If you still have concerns or difficulties understanding my intent, feel free to contact me. Of course, the Artistic License spells all this out for your protection, so you may prefer to use that.

        OK - here's an update on something I did that seems to work.

        I open the file, and stuff it into an array:
        open (FILE,"<$script_path/$file") || die "NFG1 $script_path/$file!\n"; @CONTENT = <FILE>; close(FILE);
        But instead of:
        $Text::Wrap::columns = '40'; print wrap("","",@CONTENT);
        I used a loop:
        $Text::Wrap::columns = '40'; foreach (@CONTENT) { print wrap("","",$_); }
        and all is working as expected. I'm just wondering if this is a case of treating the symptom, rather then the cause ...

        Thanks for all who contributed ... at least I've got something working now.
        Like sauoq, I have tried but failed to reproduce the results you reported -- either of them -- using the four paragraphs you cited. I am assuming that given this text, @CONTENT should have 7 elements (maybe 8, at most 9, if you have blank lines at the beginning and/or end of the array):
        1: "This program ... details.\n" 2: "\n" 3: "You should have ... provide one.\n" 4: "\n" 5: "You should also ... /gpl.html.\n" 6: "\n" 7: "For those ... to use that.\n"
        (update: added "\n" at the end of each text line, just to be clear about that; also fixed the summary of output below.) Given data like that, and using these lines of code:
        $Text::Wrap::columns = '40'; print wrap( '','',@CONTENT );
        I get the following sequence of paragraphs (summarized):
        This program is distributed in the hope ... ## 5 lines ## ... Artistic License for more details. You should have received a copy of the ... ## 2 lines ## ... glad to provide one. You should also have received a copy of ... ## 6 lines ## ... on the internet at http://www.gnu.org/copyleft/hhhhhhhhhhh hh/hhhhhhhhhhhhhhhhhh/gpl.html. For those of you that choose to use the ... ## 37 lines ## ... protection, so you may prefer to use that.
        So it looks like the URL is being rendered where it should be. Also, when I pipe the output through "od -a", I see that the paragraph boundaries (the blank lines) are rendered as two consecutive "nl" (\n) characters, with no extra spaces. (But frankly, if there were extra spaces, I don't know why this would be a problem.)

        The only thing I would consider fixing is to have the URL be broken after a slash character, rather than in the middle of a "word" token, but that probably doesn't matter.

        My Perl installation is v5.8.0 built for i686-linux, and I'm using the "2001.0929" version of Text::Wrap. What are you using, and how, exactly, are you filling the @CONTENT array?