Mail::DKIM::TextWrap - text wrapping module written for use with DKIM
my $output = ""; my $tw = Mail::DKIM::TextWrap->new( Margin => 10, Output => \$output, ); $tw->add("Mary had a little lamb, whose fleece was white as snow.\n"); $tw->finish; print $output;
This is a general-purpose text-wrapping module that I wrote because I had some specific needs with Mail::DKIM that none of the contemporary text-wrapping modules offered.
Specifically, it offers the ability to change wrapping options in the middle of a paragraph. For instance, with a DKIM signature:
DKIM-Signature: a=rsa; c=simple; h=first:second:third:fourth; b=Xr2mo2wmb1LZBwmEJElIPezal7wQQkRQ8WZtxpofkNmXTjXf8y2f0
the line-breaks can be inserted next to any of the colons of the h= tag, or any character of the b= tag. The way I implemented this was to serialize the signature one element at a time, changing the text-wrapping options at the start and end of each tag.
Text wrapping options can be specified when calling new(), or by simply changing the property as needed. For example, to change the number of characters allowed per line:
$tw->{Margin} = 20;
/\s/
.
undef
.
/\s/
.
my $tw = Mail::DKIM::TextWrap->new( Output => \$output, %wrapping_options, );
The text-wrapping object encapsulates the current options and the current state of the text stream. In addition to specifying text wrapping options as described in the section above, the following options are recognized:
$tw->add("Mary had a little lamb.\n");
You can add() all the text at once, or add() the text in parts by calling add() multiple times. If you are doing the latter, be aware that the text can be "wrapped" between add() calls, even if it does not match the "Break" pattern.
For example, given a margin of 12, this will not wrap
$tw->add("Abcdefghijklmnopqrstuvwxyz");
but this will
$tw->add("Abcdefgh"); $tw->add("ijklmnop"); $tw->add("qrstuvwxyz");
$tw->finish;
Call this when finished adding text, so that any remaining text in TextWrap's buffers will be output.