O'Reilly logo

Games, Diversions & Perl Culture by Jon Orwant

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required