.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Text::Reform 3pm" .TH Text::Reform 3pm "2022-11-19" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Text::Reform \- Manual text wrapping and reformatting .SH "VERSION" .IX Header "VERSION" This document describes version 1.20 of Text::Reform, released 2009\-09\-06. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Text::Reform; \& \& print form $template, \& $data, $to, $fill, $it, $with; \& \& \& use Text::Reform qw( tag ); \& \& print tag \*(AqB\*(Aq, $enboldened_text; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" .ie n .SS "The ""form"" sub" .el .SS "The \f(CWform\fP sub" .IX Subsection "The form sub" The \f(CW\*(C`form()\*(C'\fR subroutine may be exported from the module. It takes a series of format (or \*(L"picture\*(R") strings followed by replacement values, interpolates those values into each picture string, and returns the result. The effect is similar to the inbuilt perl \&\f(CW\*(C`format\*(C'\fR mechanism, although the field specification syntax is simpler and some of the formatting behaviour is more sophisticated. .PP A picture string consists of sequences of the following characters: .IP "<" 8 Left-justified field indicator. A series of two or more sequential <'s specify a left-justified field to be filled by a subsequent value. A single < is formatted as the literal character '<' .IP ">" 8 Right-justified field indicator. A series of two or more sequential >'s specify a right-justified field to be filled by a subsequent value. A single > is formatted as the literal character '>' .IP "<<<>>>" 8 Fully-justified field indicator. Field may be of any width, and brackets need not balance, but there must be at least 2 '<' and 2 '>'. .IP "^" 8 Centre-justified field indicator. A series of two or more sequential ^'s specify a centred field to be filled by a subsequent value. A single ^ is formatted as the literal character '^' .IP ">>>.<<<<" 8 A numerically formatted field with the specified number of digits to either side of the decimal place. See \*(L"Numerical formatting\*(R" below. .IP "[" 8 Left-justified block field indicator. Just like a < field, except it repeats as required on subsequent lines. See below. A single [ is formatted as the literal character '[' .IP "]" 8 Right-justified block field indicator. Just like a > field, except it repeats as required on subsequent lines. See below. A single ] is formatted as the literal character ']' .IP "[[[]]]" 8 Fully-justified block field indicator. Just like a <<<>>> field, except it repeats as required on subsequent lines. See below. Field may be of any width, and brackets need not balance, but there must be at least 2 '[' and 2 ']'. .IP "|" 8 Centre-justified block field indicator. Just like a ^ field, except it repeats as required on subsequent lines. See below. A single | is formatted as the literal character '|' .IP "]]].[[[[" 8 A numerically formatted block field with the specified number of digits to either side of the decimal place. Just like a >>>.<<<< field, except it repeats as required on subsequent lines. See below. .IP "~" 8 A one-character wide block field. .IP "\e" 8 .IX Item "" Literal escape of next character (e.g. \f(CW\*(C`\e~\*(C'\fR is formatted as '~', not a one character wide block field). .IP "Any other character" 8 .IX Item "Any other character" That literal character. .PP Any substitution value which is \f(CW\*(C`undef\*(C'\fR (either explicitly so, or because it is missing) is replaced by an empty string. .SS "Controlling line filling." .IX Subsection "Controlling line filling." Note that, unlike the a perl \f(CW\*(C`format\*(C'\fR, \f(CW\*(C`form\*(C'\fR preserves whitespace (including newlines) unless called with certain options. .PP The \*(L"squeeze\*(R" option (when specified with a true value) causes any sequence of spaces and/or tabs (but not newlines) in an interpolated string to be replaced with a single space. .PP A true value for the \*(L"fill\*(R" option causes (only) newlines to be squeezed. .PP To minimize all whitespace, you need to specify both options. Hence: .PP .Vb 2 \& $format = "EG> [[[[[[[[[[[[[[[[[[[[["; \& $data = "h e\et l lo\enworld\et\et\et\et\et"; \& \& print form $format, $data; # all whitespace preserved: \& # \& # EG> h e l lo \& # EG> world \& \& \& print form {squeeze=>1}, # only newlines preserved: \& $format, $data; # \& # EG> h e l lo \& # EG> world \& \& \& print form {fill=>1}, # only spaces/tabs preserved: \& $format, $data; # \& # EG> h e l lo world \& \& \& print form {squeeze=>1, fill=>1}, # no whitespace preserved: \& $format, $data; # \& # EG> h e l lo world .Ve .PP Whether or not filling or squeezing is in effect, \f(CW\*(C`form\*(C'\fR can also be directed to trim any extra whitespace from the end of each line it formats, using the \*(L"trim\*(R" option. If this option is specified with a true value, every line returned by \f(CW\*(C`form\*(C'\fR will automatically have the substitution \f(CW\*(C`s/[ \et]+$//gm\*(C'\fR applied to it. .PP Hence: .PP .Vb 2 \& print length form "[[[[[[[[[[", "short"; \& # 11 \& \& print length form {trim=>1}, "[[[[[[[[[[", "short"; \& # 6 .Ve .PP It is also possible to control the character used to fill lines that are too short, using the 'filler' option. If this option is specified the value of the 'filler' flag is used as the fill string, rather than the default \f(CW" "\fR. .PP For example: .PP .Vb 3 \& print form { filler=>\*(Aq*\*(Aq }, \& "Pay bearer: ^^^^^^^^^^^^^^^^^^^", \& \*(Aq$123.45\*(Aq; .Ve .PP prints: .PP .Vb 1 \& Pay bearer: ******$123.45****** .Ve .PP If the filler string is longer than one character, it is truncated to the appropriate length. So: .PP .Vb 3 \& print form { filler=>\*(Aq\-\->\*(Aq }, \& "Pay bearer: ]]]]]]]]]]]]]]]]]]]", \& [\*(Aq$1234.50\*(Aq, \*(Aq$123.45\*(Aq, \*(Aq$12.34\*(Aq]; .Ve .PP prints: .PP .Vb 3 \& Pay bearer: \->\-\->\-\->\-\->$1234.50 \& Pay bearer: \-\->\-\->\-\->\-\->$123.45 \& Pay bearer: >\-\->\-\->\-\->\-\->$12.34 .Ve .PP If the value of the 'filler' option is a hash, then it's 'left' and \&'right' entries specify separate filler strings for each side of an interpolated value. So: .PP .Vb 7 \& print form { filler=>{left=>\*(Aq\->\*(Aq, right=>\*(Aq*\*(Aq} }, \& "Pay bearer: <<<<<<<<<<<<<<<<<<", \& \*(Aq$123.45\*(Aq, \& "Pay bearer: >>>>>>>>>>>>>>>>>>", \& \*(Aq$123.45\*(Aq, \& "Pay bearer: ^^^^^^^^^^^^^^^^^^", \& \*(Aq$123.45\*(Aq; .Ve .PP prints: .PP .Vb 3 \& Pay bearer: $123.45*********** \& Pay bearer: >\->\->\->\->\->$123.45 \& Pay bearer: >\->\->$123.45****** .Ve .SS "Temporary and permanent default options" .IX Subsection "Temporary and permanent default options" If \f(CW\*(C`form\*(C'\fR is called with options, but no template string or data, it resets it's defaults to the options specified. If called in a void context: .PP .Vb 1 \& form { squeeze => 1, trim => 1 }; .Ve .PP the options become permanent defaults. .PP However, when called with only options in non-void context, \f(CW\*(C`form\*(C'\fR resets its defaults to those options and returns an object. The reset default values persist only until that returned object is destroyed. Hence to temporarily reset \f(CW\*(C`form\*(C'\fR's defaults within a single subroutine: .PP .Vb 2 \& sub single { \& my $tmp = form { squeeze => 1, trim => 1 }; \& \& # do formatting with the obove defaults \& \& } # form\*(Aqs defaults revert to previous values as $tmp object destroyed .Ve .SS "Multi-line format specifiers and interleaving" .IX Subsection "Multi-line format specifiers and interleaving" By default, if a format specifier contains two or more lines (i.e. one or more newline characters), the entire format specifier is repeatedly filled as a unit, until all block fields have consumed their corresponding arguments. For example, to build a simple look-up table: .PP .Vb 1 \& my @values = (1..12); \& \& my @squares = map { sprintf "%.6g", $_**2 } @values; \& my @roots = map { sprintf "%.6g", sqrt($_) } @values; \& my @logs = map { sprintf "%.6g", log($_) } @values; \& my @inverses = map { sprintf "%.6g", 1/$_ } @values; \& \& print form \& " N N**2 sqrt(N) log(N) 1/N", \& "=====================================================", \& "| [[ | [[[ | [[[[[[[[[[ | [[[[[[[[[ | [[[[[[[[[ | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-", \& \e@values, \e@squares, \e@roots, \e@logs, \e@inverses; .Ve .PP The multiline format specifier: .PP .Vb 2 \& "| [[ | [[[ | [[[[[[[[[[ | [[[[[[[[[ | [[[[[[[[[ | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-", .Ve .PP is treated as a single logical line. So \f(CW\*(C`form\*(C'\fR alternately fills the first physical line (interpolating one value from each of the arrays) and the second physical line (which puts a line of dashes between each row of the table) producing: .PP .Vb 10 \& N N**2 sqrt(N) log(N) 1/N \& ===================================================== \& | 1 | 1 | 1 | 0 | 1 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 2 | 4 | 1.41421 | 0.693147 | 0.5 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 3 | 9 | 1.73205 | 1.09861 | 0.333333 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 4 | 16 | 2 | 1.38629 | 0.25 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 5 | 25 | 2.23607 | 1.60944 | 0.2 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 6 | 36 | 2.44949 | 1.79176 | 0.166667 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 7 | 49 | 2.64575 | 1.94591 | 0.142857 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 8 | 64 | 2.82843 | 2.07944 | 0.125 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 9 | 81 | 3 | 2.19722 | 0.111111 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 10 | 100 | 3.16228 | 2.30259 | 0.1 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 11 | 121 | 3.31662 | 2.3979 | 0.0909091 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 12 | 144 | 3.4641 | 2.48491 | 0.0833333 | \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP This implies that formats and the variables from which they're filled need to be interleaved. That is, a multi-line specification like this: .PP .Vb 5 \& print form \& "Passed: ## \& [[[[[[[[[[[[[[[ # single format specification \& Failed: # (needs two sets of data) \& [[[[[[[[[[[[[[[", ## \& \& \e@passes, \e@fails; ## data for previous format .Ve .PP would print: .PP .Vb 12 \& Passed: \& \& Failed: \& \& Passed: \& \& Failed: \& \& Passed: \& \& Failed: \& .Ve .PP because the four-line format specifier is treated as a single unit, to be repeatedly filled until all the data in \f(CW@passes\fR and \f(CW@fails\fR has been consumed. .PP Unlike the table example, where this unit filling correctly put a line of dashes between lines of data, in this case the alternation of passes and fails is probably \fInot\fR the desired effect. .PP Judging by the labels, it is far more likely that the user wanted: .PP .Vb 8 \& Passed: \& \& \& \& Failed: \& \& \& .Ve .PP To achieve that, either explicitly interleave the formats and their data sources: .PP .Vb 7 \& print form \& "Passed:", ## single format (no data required) \& " [[[[[[[[[[[[[[[", ## single format (needs one set of data) \& \e@passes, ## data for previous format \& "Failed:", ## single format (no data required) \& " [[[[[[[[[[[[[[[", ## single format (needs one set of data) \& \e@fails; ## data for previous format .Ve .PP or instruct \f(CW\*(C`form\*(C'\fR to do it for you automagically, by setting the \&'interleave' flag true: .PP .Vb 5 \& print form {interleave=>1} \& "Passed: ## \& [[[[[[[[[[[[[[[ # single format \& Failed: # (needs two sets of data) \& [[[[[[[[[[[[[[[", ## \& \& ## data to be automagically interleaved \& \e@passes, \e@fails; # as necessary between lines of previous \& ## format .Ve .ie n .SS "How ""form"" hyphenates" .el .SS "How \f(CWform\fP hyphenates" .IX Subsection "How form hyphenates" Any line with a block field repeats on subsequent lines until all block fields on that line have consumed all their data. Non-block fields on these lines are replaced by the appropriate number of spaces. .PP Words are wrapped whole, unless they will not fit into the field at all, in which case they are broken and (by default) hyphenated. Simple hyphenation is used (i.e. break at the \fIN\-1\fRth character and insert a \&'\-'), unless a suitable alternative subroutine is specified instead. .PP Words will not be broken if the break would leave less than 2 characters on the current line. This minimum can be varied by setting the 'minbreak' option to a numeric value indicating the minimum total broken characters (including hyphens) required on the current line. Note that, for very narrow fields, words will still be broken (but \fIunhyphenated\fR). For example: .PP .Vb 1 \& print form \*(Aq~\*(Aq, \*(Aqsplit\*(Aq; .Ve .PP would print: .PP .Vb 5 \& s \& p \& l \& i \& t .Ve .PP whilst: .PP .Vb 1 \& print form {minbreak=>1}, \*(Aq~\*(Aq, \*(Aqsplit\*(Aq; .Ve .PP would print: .PP .Vb 5 \& s\- \& p\- \& l\- \& i\- \& t .Ve .PP Alternative breaking subroutines can be specified using the \*(L"break\*(R" option in a configuration hash. For example: .PP .Vb 3 \& form { break => \e&my_line_breaker } \& $format_str, \& @data; .Ve .PP \&\f(CW\*(C`form\*(C'\fR expects any user-defined line-breaking subroutine to take three arguments (the string to be broken, the maximum permissible length of the initial section, and the total width of the field being filled). The \f(CW\*(C`hypenate\*(C'\fR sub must return a list of two strings: the initial (broken) section of the word, and the remainder of the string respectively). .PP For example: .PP .Vb 4 \& sub tilde_break = sub($$$) \& { \& (substr($_[0],0,$_[1]\-1).\*(Aq~\*(Aq, substr($_[0],$_[1]\-1)); \& } \& \& form { break => \e&tilde_break } \& $format_str, \& @data; .Ve .PP makes '~' the hyphenation character, whilst: .PP .Vb 6 \& sub wrap_and_slop = sub($$$) \& { \& my ($text, $reqlen, $fldlen) = @_; \& if ($reqlen==$fldlen) { $text =~ m/\eA(\es*\eS*)(.*)/s } \& else { ("", $text) } \& } \& \& form { break => \e&wrap_and_slop } \& $format_str, \& @data; .Ve .PP wraps excessively long words to the next line and \*(L"slops\*(R" them over the right margin if necessary. .PP The Text::Reform package provides three functions to simplify the use of variant hyphenation schemes. The exportable subroutine \&\f(CW\*(C`Text::Reform::break_wrap\*(C'\fR generates a reference to a subroutine implementing the \*(L"wrap-and-slop\*(R" algorithm shown in the last example, which could therefore be rewritten: .PP .Vb 1 \& use Text::Reform qw( form break_wrap ); \& \& form { break => break_wrap } \& $format_str, \& @data; .Ve .PP The subroutine \f(CW\*(C`Text::Reform::break_with\*(C'\fR takes a single string argument and returns a reference to a sub which hyphenates by cutting off the text at the right margin and appending the string argument. Hence the first of the two examples could be rewritten: .PP .Vb 1 \& use Text::Reform qw( form break_with ); \& \& form { break => break_with(\*(Aq~\*(Aq) } \& $format_str, \& @data; .Ve .PP The subroutine \f(CW\*(C`Text::Reform::break_at\*(C'\fR takes a single string argument and returns a reference to a sub which hyphenates by breaking immediately after that string. For example: .PP .Vb 1 \& use Text::Reform qw( form break_at ); \& \& form { break => break_at(\*(Aq\-\*(Aq) } \& "[[[[[[[[[[[[[[", \& "The Newton\-Raphson methodology"; \& \& # returns: \& # \& # "The Newton\- \& # Raphson \& # methodology" .Ve .PP Note that this differs from the behaviour of \f(CW\*(C`break_with\*(C'\fR, which would be: .PP .Vb 3 \& form { break => break_with(\*(Aq\-\*(Aq) } \& "[[[[[[[[[[[[[[", \& "The Newton\-Raphson methodology"; \& \& # returns: \& # \& # "The Newton\-R\- \& # aphson metho\- \& # dology" .Ve .PP Hence \f(CW\*(C`break_at\*(C'\fR is generally a better choice. .PP \&\f(CW\*(C`break_at\*(C'\fR also takes an 'except' option, which tells the resulting subroutine not to break in the middle of certain strings. For example: .PP .Vb 3 \& form { break => break_at(\*(Aq\-\*(Aq, {except=>qr/Newton\-Raphson/}) } \& "[[[[[[[[[[[[[[", \& "The Newton\-Raphson methodology"; \& \& # returns: \& # \& # "The \& # Newton\-Raphson \& # methodology" .Ve .PP This option is particularly useful for preserving URLs. .PP The subroutine \f(CW\*(C`Text::Reform::break_TeX\*(C'\fR returns a reference to a sub which hyphenates using Jan Pazdziora's TeX::Hyphen module. For example: .PP .Vb 1 \& use Text::Reform qw( form break_wrap ); \& \& form { break => break_TeX } \& $format_str, \& @data; .Ve .PP Note that in the previous examples there is no leading '\e&' before \&\f(CW\*(C`break_wrap\*(C'\fR, \f(CW\*(C`break_with\*(C'\fR, or \f(CW\*(C`break_TeX\*(C'\fR, since each is being directly \fIcalled\fR (and returns a reference to some other suitable subroutine); .ie n .SS "The ""form"" formatting algorithm" .el .SS "The \f(CWform\fP formatting algorithm" .IX Subsection "The form formatting algorithm" The algorithm \f(CW\*(C`form\*(C'\fR uses is: .PP .Vb 5 \& 1. If interleaving is specified, split the first string in the \& argument list into individual format lines and add a \& terminating newline (unless one is already present). \& Otherwise, treat the entire string as a single "line" (like \& /s does in regexes) \& \& 2. For each format line... \& \& 2.1. determine the number of fields and shift \& that many values off the argument list and \& into the filling list. If insufficient \& arguments are available, generate as many \& empty strings as are required. \& \& 2.2. generate a text line by filling each field \& in the format line with the initial contents \& of the corresponding arg in the filling list \& (and remove those initial contents from the arg). \& \& 2.3. replace any <,>, or ^ fields by an equivalent \& number of spaces. Splice out the corresponding \& args from the filling list. \& \& 2.4. Repeat from step 2.2 until all args in the \& filling list are empty. \& \& 3. concatenate the text lines generated in step 2 \& \& 4. repeat from step 1 until the argument list is empty .Ve .ie n .SS """form"" examples" .el .SS "\f(CWform\fP examples" .IX Subsection "form examples" As an example of the use of \f(CW\*(C`form\*(C'\fR, the following: .PP .Vb 2 \& $count = 1; \& $text = "A big long piece of text to be formatted exquisitely"; \& \& print form q \& q{ |||| <<<<<<<<<< }, \& $count, $text, \& q{ \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- }, \& q{ ^^^^ ]]]]]]]]]]| }, \& $count+11, $text, \& q{ = \& ]]].[[[ }, \& "123 123.4\en123.456789"; .Ve .PP produces the following output: .PP .Vb 10 \& 1 A big long \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 12 piece of| \& text to be| \& formatted| \& exquisite\-| \& ly| \& = \& 123.0 \& = \& 123.4 \& = \& 123.456 .Ve .PP Note that block fields in a multi-line format string, cause the entire multi-line format to be repeated as often as necessary. .PP Picture strings and replacement values are interleaved in the traditional \f(CW\*(C`format\*(C'\fR format, but care is needed to ensure that the correct number of substitution values are provided. Another example: .PP .Vb 10 \& $report = form \& \*(AqName Rank Serial Number\*(Aq, \& \*(Aq==== ==== =============\*(Aq, \& \*(Aq<<<<<<<<<<<<< ^^^^ <<<<<<<<<<<<<\*(Aq, \& $name, $rank, $serial_number, \& \*(Aq\*(Aq \& \*(AqAge Sex Description\*(Aq, \& \*(Aq=== === ===========\*(Aq, \& \*(Aq^^^ ^^^ [[[[[[[[[[[\*(Aq, \& $age, $sex, $description; .Ve .ie n .SS "How ""form"" consumes strings" .el .SS "How \f(CWform\fP consumes strings" .IX Subsection "How form consumes strings" Unlike \f(CW\*(C`format\*(C'\fR, within \f(CW\*(C`form\*(C'\fR non-block fields \fIdo\fR consume the text they format, so the following: .PP .Vb 3 \& $text = "a line of text to be formatted over three lines"; \& print form "<<<<<<<<<<\en <<<<<<<<\en <<<<<<\en", \& $text, $text, $text; .Ve .PP produces: .PP .Vb 3 \& a line of \& text to \& be fo\- .Ve .PP not: .PP .Vb 3 \& a line of \& a line \& a line .Ve .PP To achieve the latter effect, convert the variable arguments to independent literals (by double-quoted interpolation): .PP .Vb 3 \& $text = "a line of text to be formatted over three lines"; \& print form "<<<<<<<<<<\en <<<<<<<<\en <<<<<<\en", \& "$text", "$text", "$text"; .Ve .PP Although values passed from variable arguments are progressively consumed \&\fIwithin\fR \f(CW\*(C`form\*(C'\fR, the values of the original variables passed to \f(CW\*(C`form\*(C'\fR are \fInot\fR altered. Hence: .PP .Vb 4 \& $text = "a line of text to be formatted over three lines"; \& print form "<<<<<<<<<<\en <<<<<<<<\en <<<<<<\en", \& $text, $text, $text; \& print $text, "\en"; .Ve .PP will print: .PP .Vb 4 \& a line of \& text to \& be fo\- \& a line of text to be formatted over three lines .Ve .PP To cause \f(CW\*(C`form\*(C'\fR to consume the values of the original variables passed to it, pass them as references. Thus: .PP .Vb 4 \& $text = "a line of text to be formatted over three lines"; \& print form "<<<<<<<<<<\en <<<<<<<<\en <<<<<<\en", \& \e$text, \e$text, \e$text; \& print $text, "\en"; .Ve .PP will print: .PP .Vb 4 \& a line of \& text to \& be fo\- \& rmatted over three lines .Ve .PP Note that, for safety, the \*(L"non-consuming\*(R" behaviour takes precedence, so if a variable is passed to \f(CW\*(C`form\*(C'\fR both by reference \fIand\fR by value, its final value will be unchanged. .SS "Numerical formatting" .IX Subsection "Numerical formatting" The \*(L">>>.<<<\*(R" and \*(L"]]].[[[\*(R" field specifiers may be used to format numeric values about a fixed decimal place marker. For example: .PP .Vb 9 \& print form \*(Aq(]]]]].[[)\*(Aq, < AllPlaces }, \& \*(Aq(]]]]].[[)\*(Aq, <<\*(AqEONUMS\*(Aq; \& 1 \& 1.0 \& EONUMS .Ve .PP would print: .PP .Vb 2 \& ( 1.00) \& ( 1.00) .Ve .PP Note that although decimal digits are rounded to fit the specified width, the integral part of a number is never modified. If there are not enough places before the decimal place to represent the number, the entire number is replaced with hashes. .PP If a non-numeric sequence is passed as data for a numeric field, it is formatted as a series of question marks. This querulous behaviour can be changed by giving the configuration option 'numeric' a value that matches /\ebSkipNaN\eb/i in which case, any invalid numeric data is simply ignored. For example: .PP .Vb 7 \& print form { numeric => \*(AqSkipNaN\*(Aq } \& \*(Aq(]]]]].[[)\*(Aq, \& < sub { "Page $_[0]\en\en" }, \& footer => sub { my ($pagenum, $lastpage) = @_; \& return "" if $lastpage; \& return "\-"x50 . "\en" \& .form ">"x50, "...".($pagenum+1); \& }, \& pagefeed => "\en\en", \& pagelen => 60 \& pagenum => \e$page, \& }, \& $template, \& @data; .Ve .PP Note the recursive use of \f(CW\*(C`form\*(C'\fR within the \*(L"footer\*(R" option! .PP Alternatively, to set up headers and footers such that the running head is right justified in the header and the page number is centred in the footer: .PP .Vb 6 \& form { header => { right => "Running head" }, \& footer => { centre => sub { "Page $_[0]" } }, \& pagelen => 60 \& }, \& $template, \& @data; .Ve .PP The footer in the previous example could also have been specified the other way around, as a subroutine that returns a hash (rather than a hash containing a subroutine): .PP .Vb 6 \& form { header => { right => "Running head" }, \& footer => sub { return {centre => "Page $_[0]"} }, \& pagelen => 60 \& }, \& $template, \& @data; .Ve .ie n .SS "The ""cols"" option" .el .SS "The \f(CWcols\fP option" .IX Subsection "The cols option" Sometimes data to be used in a \f(CW\*(C`form\*(C'\fR call needs to be extracted from a nested data structure. For example, whilst it's easy to print a table if you already have the data in columns: .PP .Vb 3 \& @name = qw(Tom Dick Harry); \& @score = qw( 88 54 99); \& @time = qw( 15 13 18); \& \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time\*(Aq, \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& \e@name, \e@score, \e@time; .Ve .PP if the data is aggregrated by rows: .PP .Vb 5 \& @data = ( \& { name=>\*(AqTom\*(Aq, score=>88, time=>15 }, \& { name=>\*(AqDick\*(Aq, score=>54, time=>13 }, \& { name=>\*(AqHarry\*(Aq, score=>99, time=>18 }, \& ); .Ve .PP you need to do some fancy mapping before it can be fed to \f(CW\*(C`form\*(C'\fR: .PP .Vb 8 \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time\*(Aq, \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& [map $$_{name}, @data], \& [map $$_{score}, @data], \& [map $$_{time} , @data]; .Ve .PP Or you could just use the \f(CW\*(Aqcols\*(Aq\fR option: .PP .Vb 1 \& use Text::Reform qw(form columns); \& \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time\*(Aq, \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& { cols => [qw(name score time)], \& from => \e@data \& }; .Ve .PP This option takes an array of strings that specifies the keys of the hash entries to be extracted into columns. The \f(CW\*(Aqfrom\*(Aq\fR entry (which must be present) also takes an array, which is expected to contain a list of references to hashes. For each key specified, this option inserts into \f(CW\*(C`form\*(C'\fR's argument list a reference to an array containing the entries for that key, extracted from each of the hash references supplied by \f(CW\*(Aqfrom\*(Aq\fR. So, for example, the option: .PP .Vb 3 \& { cols => [qw(name score time)], \& from => \e@data \& } .Ve .PP is replaced by three array references, the first containing the \f(CW\*(Aqname\*(Aq\fR entries for each hash inside \f(CW@data\fR, the second containing the \&\f(CW\*(Aqscore\*(Aq\fR entries for each hash inside \f(CW@data\fR, and the third containing the \f(CW\*(Aqtime\*(Aq\fR entries for each hash inside \f(CW@data\fR. .PP If, instead, you have a list of arrays containing the data: .PP .Vb 6 \& @data = ( \& # Time Name Score \& [ 15, \*(AqTom\*(Aq, 88 ], \& [ 13, \*(AqDick\*(Aq, 54 ], \& [ 18, \*(AqHarry\*(Aq, 99 ], \& ); .Ve .PP the \f(CW\*(Aqcols\*(Aq\fR option can extract the appropriate columns for that too. You just specify the required indices, rather than keys: .PP .Vb 8 \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time\*(Aq, \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& { cols => [1,2,0], \& from => \e@data \& } .Ve .PP Note that the indices can be in any order, and the resulting arrays are returned in the same order. .PP If you need to merge columns extracted from two hierarchical data structures, just concatenate the data structures first, like so: .PP .Vb 8 \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time Ranking \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| |||| |||||||\*(Aq, \& { cols => [1,2,0], \& from => [@data, @olddata], \& } .Ve .PP Of course, this only works if the columns are in the same positions in both data sets (and both datasets are stored in arrays) or if the columns have the same keys (and both datasets are in hashes). If not, you would need to format each dataset separately, like so: .PP .Vb 10 \& print form \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(AqName Score Time\*(Aq \& \*(Aq\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& { cols=>[1,2,0], from=>\e@data }, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& { cols=>[3,8,1], from=>\e@olddata }, \& \*(Aq[[[[[[[[[[[[[[ ||||| ||||\*(Aq, \& { cols=>[qw(name score time)], from=>\e@otherdata }; .Ve .ie n .SS "The ""tag"" sub" .el .SS "The \f(CWtag\fP sub" .IX Subsection "The tag sub" The \f(CW\*(C`tag\*(C'\fR subroutine may be exported from the module. It takes two arguments: a tag specifier and a text to be entagged. The tag specifier indicates the indenting of the tag, and of the text. The sub generates an end-tag (using the usual "/\fItag\fR" variant), unless an explicit end-tag is provided as the third argument. .PP The tag specifier consists of the following components (in order): .IP "An optional vertical spacer (zero or more whitespace-separated newlines)" 4 .IX Item "An optional vertical spacer (zero or more whitespace-separated newlines)" One or more whitespace characters up to a final mandatory newline. This vertical space is inserted before the tag and after the end-tag .IP "An optional tag indent" 4 .IX Item "An optional tag indent" Zero or more whitespace characters. Both the tag and the end-tag are indented by this whitespace. .IP "An optional left (opening) tag delimiter" 4 .IX Item "An optional left (opening) tag delimiter" Zero or more non\-\*(L"word\*(R" characters (not alphanumeric or '_'). If the opening delimiter is omitted, the character '<' is used. .IP "A tag" 4 .IX Item "A tag" One or more \*(L"word\*(R" characters (alphanumeric or '_'). .IP "Optional tag arguments" 4 .IX Item "Optional tag arguments" Any number of any characters .IP "An optional right (closing) tag delimiter" 4 .IX Item "An optional right (closing) tag delimiter" Zero or more non\-\*(L"word\*(R" characters which balance some sequential portion of the opening tag delimiter. For example, if the opening delimiter is \*(L"<\-(\*(R" then any of the following are acceptible closing delimiters: \&\*(L")\->\*(R", \*(L"\->\*(R", or \*(L">\*(R". If the closing delimiter is omitted, the \*(L"inverse\*(R" of the opening delimiter is used (for example, \*(L")\->\*(R"), .IP "An optional vertical spacer (zero or more newlines)" 4 .IX Item "An optional vertical spacer (zero or more newlines)" One or more whitespace characters up to a mandatory newline. This vertical space is inserted before and after the complete text. .IP "An optional text indent" 4 .IX Item "An optional text indent" Zero or more space of tab characters. Each line of text is indented by this whitespace (in addition to the tag indent). .PP For example: .PP .Vb 1 \& $text = "three lines\enof tagged\entext"; \& \& print tag "A HREF=#nextsection", $text; .Ve .PP prints: .PP .Vb 3 \& three lines \& of tagged \& text .Ve .PP whereas: .PP .Vb 1 \& print tag "[\-:GRIN>>>\en", $text; .Ve .PP prints: .PP .Vb 5 \& [\-:GRIN>>>:\-] \& three lines \& of tagged \& text \& [\-:/GRIN>>>:\-] .Ve .PP and: .PP .Vb 1 \& print tag "\en\en \en\en ", $text, ""; .Ve .PP prints: .PP \&\ .PP .Vb 1 \& \& \& three lines \& of tagged \& text \& \& .Ve .PP \&\ .PP (with the indicated spacing fore and aft). .SH "AUTHOR" .IX Header "AUTHOR" Damian Conway (damian@conway.org) .SH "BUGS" .IX Header "BUGS" The module uses \f(CW\*(C`POSIX::strtod\*(C'\fR, which may be broken under certain versions of Windows. Applying the \s-1WINDOWS_PATCH\s0 patch to Reform.pm will replace the \&\s-1POSIX\s0 function with a copycat subroutine. .PP There are undoubtedly serious bugs lurking somewhere in code this funky :\-) Bug reports and other feedback are most welcome. .SH "LICENCE AND COPYRIGHT" .IX Header "LICENCE AND COPYRIGHT" Copyright (c) 1997\-2007, Damian Conway \f(CW\*(C`\*(C'\fR. All rights reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \*(L"AS IS\*(R" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.\s0 .PP \&\s-1IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE\s0 (\s-1INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0