Chapter 15. Lazy Text Formatting

Damian Conway

Don’t you just hate getting an email that’s been for matted for the wrong number of columns? It’s an unprovoked ass ault on your poor visual cortex. And it’s a thoughtless insult, to o. It screams: “Hey, you aren’t even worth the eight keystr okes it would take me to correctly set my editor’s autowrap!” > And, of course, it only gets worse when quoted email is involved. > Even when someone tries to do the right thing, they just end > up frying more of your neurons as you attempt to untangle > the mess that most text formatters make of the standard > quoting conventions. It’s no fun trying to separate the meaning > from the massage.

What the world needs is a text reformatter that looks at the contents—and context—of the ASCII it’s munging, and then Does The Right Thing automagically.

Text::Autoformat

And that’s exactly what the Text::Autoformat module gives you. Specifically, it provides a subroutine named autoformat that wraps text to fixed margins. However, unlike other text wrapping modules (such as Text::Wrap, Text::Correct, or Text::Reflow), autoformat reformats its input by analyzing the text’s structure: identifying and rearranging independent paragraphs by looking for visual gaps, list bullets, changes in quoting, centering, and underlining.

If you’re happy to live with autoformat’s reasonable defaults, then reformatting a single paragraph (taking it from STDIN and printing it to STDOUT) is no more complicated than this:

use Text::Autoformat; ...

Get Games, Diversions & Perl Culture now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.