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

I was tinkering around today and wrote a small script to fetch (via Net::FTP) and autoformat some Project Gutenberg etexts with Text::Autoformat. I grabbed a nice small one to test on. Here's a bit of code that is causing me some trouble:
use strict; use Text::Autoformat; open(PGW, "<3ward10.txt") or die $!; while (<PGW>) { my $formatted = autoformat $_, {justify =>'left', left =>2, right =>60, all =>1, }; print $formatted; }

So far, so good.. you'd expect it to take the input file, 3ward10.txt, rewrap the text found to 58 columns (+2), and print it to STDOUT. It doesn't. I looked into Text::Wrap, Text::Correct and others without success.

The other odd thing about this, is that when the original file is left intact, with the ^M characters at the end of each line, the above code "works" (though doesn't wrap or reformat the text), but if I strip out the ^M characters (s,\cM,,g), the above code dies on the third line of the file (a blank line). Removing the line then causes it to barf on the next blank line, and so on (try it yourself, you'll see what I mean).

Can Text::Autoformat not deal with blank lines in a paragraph? That would seem... odd. I also notice I can't seem to stuff the file contents (via the handle, PGW) into an array, and feed that to Text::Autoformat (my @data = <PGW>;) What am I missing here? Why doesn't ::Autoformat actually do as advertised? Some glaring error on my part?

Replies are listed 'Best First'.
Re: Text::Autoformat... doesn't.
by converter (Priest) on Oct 27, 2002 at 08:33 UTC

    The first sub-section of the DESCRIPTION section of the Text::Autoformat man page should clear up any confusion about the type of input the module expects:

    The fundamental task of the autoformat subroutine is to identify and rearrange independent paragraphs in a text. Paragraphs typically consist of a series of lines containing at least one non-whitespace character, followed by one or more lines containing only optional whitespace.

    Given that you haven't altered $/, your input operator is reading only one line at a time. As grantm suggested above, you need to pass the entire input text, not one line at a time, otherwise &autoformat is operating against each input line without the context required to derive paragraph-related information.

Re: Text::Autoformat... doesn't.
by grantm (Parson) on Oct 27, 2002 at 07:15 UTC

    I haven't used Text::AutoFormat, but given that your code only gives it one line at a time, I'm not sure what you expect it to do. Try slurping the whole file into a scalar and then autoformat that.

Re: Text::Autoformat... doesn't.
by Anonymous Monk on Oct 27, 2002 at 07:44 UTC
    wow
    use strict; use warnings ; use Text::Autoformat; die autoformat 1 x 240, {justify =>'left', left =>2, right =>60, all =>1, }; __END__ 1111111111111111111111111111111111111111111111111111111111- 1111111111111111111111111111111111111111111111111111111111- 1111111111111111111111111111111111111111111111111111111111- 1111111111111111111111111111111111111111111111111111111111- 11111111