/usr/share/perl5/Text/Reform.pm is in libtext-reform-perl 1.20-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 | package Text::Reform;
use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); use Carp;
use 5.005;
#use version;
$VERSION = '1.20';
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw( form );
@EXPORT_OK = qw( columns tag break_with break_at break_wrap break_TeX debug );
my @bspecials = qw( [ | ] );
my @lspecials = qw( < ^ > );
my $ljustified = '[<]{2,}[>]{2,}';
my $bjustified = '[[]{2,}[]]{2,}';
my $bsingle = '~+';
my @specials = (@bspecials, @lspecials);
my $fixed_fieldpat = join('|', ($ljustified, $bjustified,
$bsingle,
map { "\\$_\{2,}" } @specials));
my ($lfieldmark, $bfieldmark, $fieldmark, $fieldpat, $decimal);
my $emptyref = '';
sub import
{
#$decimal = localeconv()->{decimal_point} || '.';
$decimal = '.';
my $lnumerical = '[>]+(?:'.quotemeta($decimal).'[<]{1,})';
my $bnumerical = '[]]+(?:'.quotemeta($decimal).'[[]{1,})';
$fieldpat = join('|', ($lnumerical, $bnumerical,$fixed_fieldpat));
$lfieldmark = join '|', ($lnumerical, $ljustified, map { "\\$_\{2}" } @lspecials);
$bfieldmark = join '|', ($bnumerical, $bjustified, $bsingle, map { "\\$_\{2}" } @bspecials);
$fieldmark = join '|', ($lnumerical, $bnumerical,
$bsingle,
$ljustified, $bjustified,
$lfieldmark, $bfieldmark);
Text::Reform->export_to_level(1, @_);
}
sub carpfirst {
use vars '%carped';
my ($msg) = @_;
return if $carped{$msg}++;
carp $msg;
}
###### USEFUL TOOLS ######################################
#===== form =============================================#
sub BAD_CONFIG { 'Configuration hash not allowed between format and data' }
sub break_with
{
my $hyphen = $_[0];
my $hylen = length($hyphen);
my @ret;
sub
{
if ($_[2]<=$hylen)
{
@ret = (substr($_[0],0,1), substr($_[0],1))
}
else
{
@ret = (substr($_[0],0,$_[1]-$hylen),
substr($_[0],$_[1]-$hylen))
}
if ($ret[0] =~ /\A\s*\Z/) { return ("",$_[0]); }
else { return ($ret[0].$hyphen,$ret[1]); }
}
}
sub break_at {
my ($hyphen, $opts_ref) = @_;
my $hylen = length($hyphen);
my $except = $opts_ref->{except};
my @ret;
sub
{
my $max = $_[2]-$hylen;
if ($max <= 0) {
@ret = (substr($_[0],0,1), substr($_[0],1))
}
elsif (defined $except && $_[0] =~ m/\A (.{1,$max}) ($except .*)/xms) {
@ret = ($1,$2);
}
elsif (defined $except && $_[0] =~ m/\A ($except) (.*)/xms) {
@ret = ($1,$2);
}
elsif ($_[0] =~ /\A (.{1,$max}$hyphen) (.*)/xms) {
@ret = ($1,$2);
}
elsif (length($_[0])>$_[2]) {
@ret = (substr($_[0],0,$_[1]-$hylen).$hyphen,
substr($_[0],$_[1]-$hylen))
}
else {
@ret = ("",$_[0]);
}
if ($ret[0] =~ /\A\s*\Z/) { return ("",$_[0]); }
else { return @ret; }
}
}
sub break_wrap
{
return \&break_wrap unless @_;
my ($text, $reqlen, $fldlen) = @_;
if ($reqlen==$fldlen) { $text =~ m/\A(\s*\S*)(.*)/s }
else { ("", $text) }
}
my %hyp;
sub break_TeX
{
my $file = $_[0] || "";
croak "Can't find TeX::Hyphen module"
unless require "TeX/Hyphen.pm";
$hyp{$file} = TeX::Hyphen->new($file||undef)
|| croak "Can't open hyphenation file $file"
unless $hyp{$file};
return sub {
for (reverse $hyp{$file}->hyphenate($_[0])) {
if ($_ < $_[1]) {
return (substr($_[0],0,$_).'-',
substr($_[0],$_) );
}
}
return ("",$_[0]);
}
}
my $debug = 0;
sub _debug { print STDERR @_, "\n" if $debug }
sub debug { $debug = 1; }
sub notempty
{
my $ne = ${$_[0]} =~ /\S/;
_debug("\tnotempty('${$_[0]}') = $ne\n");
return $ne;
}
sub strtod1 {
my $n=shift;
my $real_re='((?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)'.
'(?:(?:[eE])(?:(?:[+-]?)(?:[0123456789]+))|))';
if ($n=~/^\s*$real_re(.*)$/os) {
return ($1,length($2 || ''));
} else {
return (undef,length($n));
}
}
sub replace($$$$) # ($fmt, $len, $argref, $config)
{
my $ref = $_[2];
my $text = '';
my $rem = $_[1];
my $config = $_[3];
my $filled = 0;
if ($config->{fill}) { $$ref =~ s/\A\s*// }
else { $$ref =~ s/\A[ \t]*// }
my $fmtnum = length $_[0];
if ($$ref =~ /\S/ && $fmtnum>2)
{
NUMERICAL:{
my ($ilen,$dlen) = map {length} $_[0] =~ m/([]>]+)\Q$decimal\E([[<]+)/;
my ($num,$unconsumed) = strtod1($$ref);
if ($unconsumed == length $$ref)
{
$$ref =~ s/\s*\S*//;
redo NUMERICAL if $config->{numeric} =~ m/\bSkipNaN\b/i
&& $$ref =~ m/\S/;
$text = '?' x $ilen . $decimal . '?' x $dlen;
$rem = 0;
return $text;
}
my $formatted = sprintf "%$fmtnum.${dlen}f", $num;
$text = (length $formatted > $fmtnum)
? '#' x $ilen . $decimal . '#' x $dlen
: $formatted;
$text =~ s/(\Q$decimal\E\d+?)(0+)$/$1 . " " x length $2/e
unless $config->{numeric} =~ m/\bAllPlaces\b/i
|| $num =~ /\Q$decimal\E\d\d{$dlen,}$/;
if ($unconsumed)
{
if ($unconsumed == length $$ref)
{ $$ref =~ s/\A.[^0-9.+-]*// }
else
{ substr($$ref,0,-$unconsumed) = ""}
}
else { $$ref = "" }
$rem = 0;
}
}
else
{
while ($$ref =~ /\S/)
{
if (!$config->{fill} && $$ref=~s/\A[ \t]*\n//)
{ $filled = 2; last }
last unless $$ref =~ /\A(\s*)(\S+)(.*)\z/s;
my ($ws, $word, $extra) = ($1,$2,$3);
my $nonnl = $ws =~ /[^\n]/;
$ws =~ s/\n/$nonnl? "" : " "/ge if $config->{fill};
my $lead = ($config->{squeeze} ? ($ws ? " " : "") : $ws);
my $match = $lead . $word;
_debug "Extracted [$match]";
last if $text && $match =~ /\n/;
my $len1 = length($match);
if ($len1 <= $rem)
{
_debug "Accepted [$match]";
$text .= $match;
$rem -= $len1;
$$ref = $extra;
}
else
{
_debug "Need to break [$match]";
# was: if ($len1 > $_[1] and $rem-length($lead)>$config->{minbreak})
if ($rem-length($lead)>$config->{minbreak})
{
_debug "Trying to break '$match'";
my ($broken,$left) =
$config->{break}->($match,$rem,$_[1]);
$text .= $broken;
_debug "Broke as: [$broken][$left]";
$$ref = $left.$extra;
$rem -= length $broken;
}
last;
}
}
continue { $filled=1 }
}
if (!$filled && $rem>0 && $$ref=~/\S/ && length $text == 0)
{
$$ref =~ s/^\s*(.{1,$rem})//;
$text = $1;
$rem -= length $text;
}
if ( $text=~/ / && $_[0] eq 'J' && $$ref=~/\S/ && $filled!=2 ) {
# FULLY JUSTIFIED
$text = reverse $text;
$text =~ s/( +)/($rem-->0?" ":"").$1/ge while $rem>0;
$text = reverse $text;
}
elsif ( $_[0] =~ /\>|\]/ ) { # RIGHT JUSTIFIED
substr($text,0,0) =
substr($config->{filler}{left} x $rem, -$rem)
if $rem > 0;
}
elsif ( $_[0] =~ /\^|\|/ ) { # CENTRE JUSTIFIED
if ($rem>0) {
my $halfrem = int($rem/2);
substr($text,0,0) =
substr($config->{filler}{left}x$halfrem, -$halfrem);
$halfrem = $rem-$halfrem;
$text .= substr($config->{filler}{right}x$halfrem, 0, $halfrem);
}
}
else { # LEFT JUSTIFIED
$text .= substr($config->{filler}{right}x$rem, 0, $rem)
if $rem > 0;
}
return $text;
}
my %std_config =
(
header => sub{""},
footer => sub{""},
pagefeed => sub{""},
pagelen => 0,
pagenum => undef,
pagewidth => 72,
break => break_with('-'),
minbreak => 2,
squeeze => 0,
filler => {left=>' ', right=>' '},
interleave => 0,
numeric => "",
_used => 1,
);
sub lcr {
my ($data, $pagewidth, $header) = @_;
$data->{width} ||= $pagewidth;
$data->{left} ||= "";
$data->{centre} ||= $data->{center}||"";
$data->{right} ||= "";
return sub {
my @l = split "\n", (ref $data->{left} eq 'CODE'
? $data->{left}->(@_) : $data->{left}), -1;
my @c = split "\n", (ref $data->{centre} eq 'CODE'
? $data->{centre}->(@_) : $data->{centre}), -1;
my @r = split "\n", (ref $data->{right} eq 'CODE'
? $data->{right}->(@_) : $data->{right}), -1;
my $text = "";
while (@l||@c||@r) {
my $l = @l ? shift(@l) : "";
my $c = @c ? shift(@c) : "";
my $r = @r ? shift(@r) : "";
my $gap = int(($data->{width}-length($c))/2-length($l));
if ($gap < 0) {
$gap = 0;
carpfirst "\nWarning: $header is wider than specified page width ($data->{width} chars)" if $^W;
}
$text .= $l . " " x $gap
. $c . " " x ($data->{width}-length($l)-length($c)-$gap-length($r))
. $r
. "\n";
}
return $text;
}
}
sub fix_config(\%)
{
my ($config) = @_;
if (ref $config->{header} eq 'HASH') {
$config->{header} =
lcr $config->{header}, $config->{pagewidth}, 'header';
}
elsif (ref $config->{header} eq 'CODE') {
my $tmp = $config->{header};
$config->{header} = sub {
my $header = &$tmp;
return (ref $header eq 'HASH')
? lcr($header,$config->{pagewidth},'header')->()
: $header;
}
}
else {
my $tmp = $config->{header};
$config->{header} = sub { $tmp }
}
if (ref $config->{footer} eq 'HASH') {
$config->{footer} =
lcr $config->{footer}, $config->{pagewidth}, 'footer';
}
elsif (ref $config->{footer} eq 'CODE') {
my $tmp = $config->{footer};
$config->{footer} = sub {
my $footer = &$tmp;
return (ref $footer eq 'HASH')
? lcr($footer,$config->{pagewidth},'footer')->()
: $footer;
}
}
else {
my $tmp = $config->{footer};
$config->{footer} = sub { $tmp }
}
unless (ref $config->{pagefeed} eq 'CODE')
{ my $tmp = $config->{pagefeed}; $config->{pagefeed} = sub { $tmp } }
unless (ref $config->{break} eq 'CODE')
{ $config->{break} = break_at($config->{break}) }
if (defined $config->{pagenum} && ref $config->{pagenum} ne 'SCALAR')
{ my $tmp = $config->{pagenum}+0; $config->{pagenum} = \$tmp }
unless (ref $config->{filler} eq 'HASH') {
$config->{filler} = { left => "$config->{filler}",
right => "$config->{filler}" }
}
}
sub FormOpt::DESTROY
{
print STDERR "\nWarning: lexical &form configuration at $std_config{_line} was never used.\n"
if $^W && !$std_config{_used};
%std_config = %{$std_config{_prev}};
}
sub form
{
use vars '%carped';
local %carped;
my $config = {%std_config};
my $startidx = 0;
if (@_ && ref($_[0]) eq 'HASH') # RESETTING CONFIG
{
if (@_ > 1) # TEMPORARY RESET
{
$config = {%$config, %{$_[$startidx++]}};
fix_config(%$config);
$startidx = 1;
}
elsif (defined wantarray) # CONTEXT BEING CAPTURED
{
$_[0]->{_prev} = { %std_config };
$_[0]->{_used} = 0;
$_[0]->{_line} = join " line ", (caller)[1..2];;
%{$_[0]} = %std_config = (%std_config, %{$_[0]});
fix_config(%std_config);
return bless $_[0], 'FormOpt';
}
else # PERMANENT RESET
{
$_[0]->{_used} = 1;
$_[0]->{_line} = join " line ", (caller)[1..2];;
%std_config = (%std_config, %{$_[0]});
fix_config(%std_config);
return;
}
}
$config->{pagenum} = do{\(my $tmp=1)}
unless defined $config->{pagenum};
$std_config{_used}++;
my @ref = map { ref } @_;
my @orig = @_;
my $caller = caller;
no strict;
for (my $nextarg=0; $nextarg<@_; $nextarg++)
{
my $next = $_[$nextarg];
if (!defined $next) {
my $tmp = "";
splice @_, $nextarg, 1, \$tmp;
}
elsif ($ref[$nextarg] eq 'ARRAY') {
splice @_, $nextarg, 1, \join("\n", @$next)
}
elsif ($ref[$nextarg] eq 'HASH' && $next->{cols} ) {
croak "Missing 'from' data for 'cols' option"
unless $next->{from};
croak "Can't mix other options with 'cols' option"
if keys %$next > 2;
my ($cols, $data) = @{$next}{'cols','from'};
croak "Invalid 'cols' option.\nExpected reference to array of column specifiers but found " . (ref($cols)||"'$cols'")
unless ref $cols eq 'ARRAY';
croak "Invalid 'from' data for 'cols' option.\nExpected reference to array of hashes or arrays but found " . (ref($data)||"'$data'")
unless ref $data eq 'ARRAY';
splice @_, $nextarg, 2, columns(@$cols,@$data);
splice @ref, $nextarg, 2, ('ARRAY')x@$cols;
$nextarg--;
}
elsif (!defined eval { local $SIG{__DIE__};
$_[$nextarg] = $next;
_debug "writeable: [$_[$nextarg]]";
1})
{
_debug "unwriteable: [$_[$nextarg]]";
my $arg = $_[$nextarg];
splice @_, $nextarg, 1, \$arg;
}
elsif (!$ref[$nextarg]) {
splice @_, $nextarg, 1, \$_[$nextarg];
}
elsif ($ref[$nextarg] ne 'HASH' and $ref[$nextarg] ne 'SCALAR')
{
splice @_, $nextarg, 1, \"$next";
}
}
my $header = $config->{header}->(${$config->{pagenum}});
$header.="\n" if $header && substr($header,-1,1) ne "\n";
my $footer = $config->{footer}->(${$config->{pagenum}});
$footer.="\n" if $footer && substr($footer,-1,1) ne "\n";
my $prevfooter = $footer;
my $linecount = $header=~tr/\n/\n/ + $footer=~tr/\n/\n/;
my $hfcount = $linecount;
my $text = $header;
my @format_stack;
LINE: while ($startidx < @_ || @format_stack)
{
if (($ref[$startidx]||'') eq 'HASH')
{
$config = {%$config, %{$_[$startidx++]}};
fix_config(%$config);
next;
}
unless (@format_stack) {
@format_stack = $config->{interleave}
? map "$_\n", split /\n/, ${$_[$startidx++]}||""
: ${$_[$startidx++]}||"";
}
my $format = shift @format_stack;
_debug("format: [$format]");
my @parts = split /(\n|(?:\\.)+|$fieldpat)/, $format;
push @parts, "\n" unless @parts && $parts[-1] eq "\n";
my $fieldcount = 0;
my $filled = 0;
my $firstline = 1;
while (!$filled)
{
my $nextarg = $startidx;
my @data;
foreach my $part ( @parts )
{
if ($part =~ /\A(?:\\.)+/)
{
_debug("esc literal: [$part]");
my $tmp = $part;
$tmp =~ s/\\(.)/$1/g;
$text .= $tmp;
}
elsif ($part =~ /($lfieldmark)/)
{
if ($firstline)
{
$fieldcount++;
if ($nextarg > $#_)
{ push @_,\$emptyref; push @ref, '' }
my $type = $1;
$type = 'J' if $part =~ /$ljustified/;
croak BAD_CONFIG if ($ref[$startidx] eq 'HASH');
_debug("once field: [$part]");
_debug("data was: [${$_[$nextarg]}]");
$text .= replace($type,length($part),$_[$nextarg],$config);
_debug("data now: [${$_[$nextarg]}]");
}
else
{
$text .= substr($config->{filler}{left} x length($part), -length($part));
_debug("missing once field: [$part]");
}
$nextarg++;
}
elsif ($part =~ /($fieldmark)/ and substr($part,0,2) ne '~~')
{
$fieldcount++ if $firstline;
if ($nextarg > $#_)
{ push @_,\$emptyref; push @ref, '' }
my $type = $1;
$type = 'J' if $part =~ /$bjustified/;
croak BAD_CONFIG if ($ref[$startidx] eq 'HASH');
_debug("multi field: [$part]");
_debug("data was: [${$_[$nextarg]}]");
$text .= replace($type,length($part),$_[$nextarg],$config);
_debug("data now: [${$_[$nextarg]}]");
push @data, $_[$nextarg];
$nextarg++;
}
else
{
_debug("literal: [$part]");
my $tmp = $part;
$tmp =~ s/\0(\0*)/$1/g;
$text .= $tmp;
if ($part eq "\n")
{
$linecount++;
if ($config->{pagelen} && $linecount>=$config->{pagelen})
{
_debug("\tejecting page: $config->{pagenum}");
carpfirst "\nWarning: could not format page ${$config->{pagenum}} within specified page length"
if $^W && $config->{pagelen} && $linecount > $config->{pagelen};
${$config->{pagenum}}++;
my $pagefeed = $config->{pagefeed}->(${$config->{pagenum}});
$header = $config->{header}->(${$config->{pagenum}});
$header.="\n" if $header && substr($header,-1,1) ne "\n";
$text .= $footer
. $pagefeed
. $header;
$prevfooter = $footer;
$footer = $config->{footer}->(${$config->{pagenum}});
$footer.="\n" if $footer && substr($footer,-1,1) ne "\n";
$linecount = $hfcount =
$header=~tr/\n/\n/ + $footer=~tr/\n/\n/;
$header = $pagefeed
. $header;
}
}
}
_debug("\tnextarg now: $nextarg");
_debug("\tstartidx now: $startidx");
}
$firstline = 0;
$filled = ! grep { notempty $_ } @data;
}
$startidx += $fieldcount;
}
# ADJUST FINAL PAGE HEADER OR FOOTER AS REQUIRED
if ($hfcount && $linecount == $hfcount) # UNNEEDED HEADER
{
$text =~ s/\Q$header\E\Z//;
}
elsif ($linecount && $config->{pagelen}) # MISSING FOOTER
{
$text .= "\n" x ($config->{pagelen}-$linecount)
. $footer;
$prevfooter = $footer;
}
# REPLACE LAST FOOTER
if ($prevfooter) {
my $lastfooter = $config->{footer}->(${$config->{pagenum}},1);
$lastfooter.="\n"
if $lastfooter && substr($lastfooter,-1,1) ne "\n";
my $footerdiff = ($lastfooter =~ tr/\n/\n/)
- ($prevfooter =~ tr/\n/\n/);
# Enough space to squeeze longer final footer in?
my $tail = '^[^\S\n]*\n' x $footerdiff;
if ($footerdiff > 0 && $text =~ /($tail\Q$prevfooter\E)\Z/m) {
$prevfooter = $1;
$footerdiff = 0;
}
# Apparently, not, so create an extra (empty) page for it
if ($footerdiff > 0) {
${$config->{pagenum}}++;
my $lastheader = $config->{header}->(${$config->{pagenum}});
$lastheader.="\n"
if $lastheader && substr($lastheader,-1,1) ne "\n";
$lastfooter = $config->{footer}->(${$config->{pagenum}},1);
$lastfooter.="\n"
if $lastfooter && substr($lastfooter,-1,1) ne "\n";
$text .= $lastheader
. ("\n" x ( $config->{pagelen}
- ($lastheader =~ tr/\n/\n/)
- ($lastfooter =~ tr/\n/\n/)
)
)
. $lastfooter;
}
else {
$lastfooter = ("\n"x-$footerdiff).$lastfooter;
substr($text, -length($prevfooter)) = $lastfooter;
}
}
# RESTORE ARG LIST
for my $i (0..$#orig)
{
if ($ref[$i] eq 'ARRAY')
{ eval { @{$orig[$i]} = map "$_\n", split /\n/, ${$_[$i]} } }
elsif (!$ref[$i])
{ eval { _debug("restoring $i (".$_[$i].") to " .
defined($orig[$i]) ? $orig[$i] : "<undef>");
${$_[$i]} = $orig[$i] } }
}
${$config->{pagenum}}++;
$text =~ s/[ ]+$//gm if $config->{trim};
return $text unless wantarray;
return map "$_\n", split /\n/, $text;
}
#==== columns ========================================#
sub columns {
my @cols;
my (@fullres, @res);
while (@_) {
my $arg = shift @_;
my $type = ref $arg;
if ($type eq 'HASH') {
push @{$res[$_]}, $arg->{$cols[$_]} for 0..$#cols;
}
elsif ($type eq 'ARRAY') {
push @{$res[$_]}, $arg->[$cols[$_]] for 0..$#cols;
}
else {
if (@res) {
push @fullres, @res;
@res = @cols = ();
}
push @cols, $arg;
}
}
return @fullres, @res;
}
#==== tag ============================================#
sub invert($)
{
my $inversion = reverse $_[0];
$inversion =~ tr/{[<(/}]>)/;
return $inversion;
}
sub tag # ($tag, $text; $opt_endtag)
{
my ($tagleader,$tagindent,$ldelim,$tag,$tagargs,$tagtrailer) =
( $_[0] =~ /\A((?:[ \t]*\n)*)([ \t]*)(\W*)(\w+)(.*?)(\s*)\Z/ );
$ldelim = '<' unless $ldelim;
$tagtrailer =~ s/([ \t]*)\Z//;
my $textindent = $1||"";
my $rdelim = invert $ldelim;
my $i;
for ($i = -1; -1-$i < length $rdelim && -1-$i < length $tagargs; $i--)
{
last unless substr($tagargs,$i,1) eq substr($rdelim,$i,1);
}
if ($i < -1)
{
$i++;
$tagargs = substr($tagargs,0,$i);
$rdelim = substr($rdelim,$i);
}
my $endtag = $_[2] || "$ldelim/$tag$rdelim";
return "$tagleader$tagindent$ldelim$tag$tagargs$rdelim$tagtrailer".
join("\n",map { "$tagindent$textindent$_" } split /\n/, $_[1]).
"$tagtrailer$tagindent$endtag$tagleader";
}
1;
__END__
=head1 NAME
Text::Reform - Manual text wrapping and reformatting
=head1 VERSION
This document describes version 1.20 of Text::Reform,
released 2009-09-06.
=head1 SYNOPSIS
use Text::Reform;
print form $template,
$data, $to, $fill, $it, $with;
use Text::Reform qw( tag );
print tag 'B', $enboldened_text;
=head1 DESCRIPTION
=head2 The C<form> sub
The C<form()> subroutine may be exported from the module.
It takes a series of format (or "picture") strings followed by
replacement values, interpolates those values into each picture string,
and returns the result. The effect is similar to the inbuilt perl
C<format> mechanism, although the field specification syntax is
simpler and some of the formatting behaviour is more sophisticated.
A picture string consists of sequences of the following characters:
=over 8
=item <
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 '<'
=item >
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 '>'
=item <<<>>>
Fully-justified field indicator.
Field may be of any width, and brackets need not balance, but there
must be at least 2 '<' and 2 '>'.
=item ^
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 '^'
=item >>>.<<<<
A numerically formatted field with the specified number of digits to
either side of the decimal place. See L<Numerical formatting> below.
=item [
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 '['
=item ]
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 ']'
=item [[[]]]
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 ']'.
=item |
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 '|'
=item ]]].[[[[
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.
=item ~
A one-character wide block field.
=item \
Literal escape of next character (e.g. C<\~> is formatted as '~', not a one
character wide block field).
=item Any other character
That literal character.
=back
Any substitution value which is C<undef> (either explicitly so, or because it
is missing) is replaced by an empty string.
=head2 Controlling line filling.
Note that, unlike the a perl C<format>, C<form> preserves whitespace
(including newlines) unless called with certain options.
The "squeeze" 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.
A true value for the "fill" option causes (only) newlines to be squeezed.
To minimize all whitespace, you need to specify both options. Hence:
$format = "EG> [[[[[[[[[[[[[[[[[[[[[";
$data = "h e\t l lo\nworld\t\t\t\t\t";
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
Whether or not filling or squeezing is in effect, C<form> can also be
directed to trim any extra whitespace from the end of each line it
formats, using the "trim" option. If this option is specified with a
true value, every line returned by C<form> will automatically have the
substitution C<s/[ \t]+$//gm> applied to it.
Hence:
print length form "[[[[[[[[[[", "short";
# 11
print length form {trim=>1}, "[[[[[[[[[[", "short";
# 6
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 C<" ">.
For example:
print form { filler=>'*' },
"Pay bearer: ^^^^^^^^^^^^^^^^^^^",
'$123.45';
prints:
Pay bearer: ******$123.45******
If the filler string is longer than one character, it is truncated
to the appropriate length. So:
print form { filler=>'-->' },
"Pay bearer: ]]]]]]]]]]]]]]]]]]]",
['$1234.50', '$123.45', '$12.34'];
prints:
Pay bearer: ->-->-->-->$1234.50
Pay bearer: -->-->-->-->$123.45
Pay bearer: >-->-->-->-->$12.34
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:
print form { filler=>{left=>'->', right=>'*'} },
"Pay bearer: <<<<<<<<<<<<<<<<<<",
'$123.45',
"Pay bearer: >>>>>>>>>>>>>>>>>>",
'$123.45',
"Pay bearer: ^^^^^^^^^^^^^^^^^^",
'$123.45';
prints:
Pay bearer: $123.45***********
Pay bearer: >->->->->->$123.45
Pay bearer: >->->$123.45******
=head2 Temporary and permanent default options
If C<form> 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:
form { squeeze => 1, trim => 1 };
the options become permanent defaults.
However, when called with only options in non-void context, C<form>
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 C<form>'s defaults within a single subroutine:
sub single {
my $tmp = form { squeeze => 1, trim => 1 };
# do formatting with the obove defaults
} # form's defaults revert to previous values as $tmp object destroyed
=head2 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:
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",
"=====================================================",
"| [[ | [[[ | [[[[[[[[[[ | [[[[[[[[[ | [[[[[[[[[ |
-----------------------------------------------------",
\@values, \@squares, \@roots, \@logs, \@inverses;
The multiline format specifier:
"| [[ | [[[ | [[[[[[[[[[ | [[[[[[[[[ | [[[[[[[[[ |
-----------------------------------------------------",
is treated as a single logical line. So C<form> 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:
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 |
-----------------------------------------------------
This implies that formats and the variables from which they're filled
need to be interleaved. That is, a multi-line specification like this:
print form
"Passed: ##
[[[[[[[[[[[[[[[ # single format specification
Failed: # (needs two sets of data)
[[[[[[[[[[[[[[[", ##
\@passes, \@fails; ## data for previous format
would print:
Passed:
<pass 1>
Failed:
<fail 1>
Passed:
<pass 2>
Failed:
<fail 2>
Passed:
<pass 3>
Failed:
<fail 3>
because the four-line format specifier is treated as a single unit,
to be repeatedly filled until all the data in C<@passes> and C<@fails>
has been consumed.
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 I<not> the desired effect.
Judging by the labels, it is far more likely that the user wanted:
Passed:
<pass 1>
<pass 2>
<pass 3>
Failed:
<fail 4>
<fail 5>
<fail 6>
To achieve that, either explicitly interleave the formats and their data
sources:
print form
"Passed:", ## single format (no data required)
" [[[[[[[[[[[[[[[", ## single format (needs one set of data)
\@passes, ## data for previous format
"Failed:", ## single format (no data required)
" [[[[[[[[[[[[[[[", ## single format (needs one set of data)
\@fails; ## data for previous format
or instruct C<form> to do it for you automagically, by setting the
'interleave' flag true:
print form {interleave=>1}
"Passed: ##
[[[[[[[[[[[[[[[ # single format
Failed: # (needs two sets of data)
[[[[[[[[[[[[[[[", ##
## data to be automagically interleaved
\@passes, \@fails; # as necessary between lines of previous
## format
=head2 How C<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.
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 I<N-1>th character and insert a
'-'), unless a suitable alternative subroutine is specified instead.
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 I<unhyphenated>). For example:
print form '~', 'split';
would print:
s
p
l
i
t
whilst:
print form {minbreak=>1}, '~', 'split';
would print:
s-
p-
l-
i-
t
Alternative breaking subroutines can be specified using the "break" option in a
configuration hash. For example:
form { break => \&my_line_breaker }
$format_str,
@data;
C<form> 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 C<hypenate> sub must return a list of two strings: the initial
(broken) section of the word, and the remainder of the string
respectively).
For example:
sub tilde_break = sub($$$)
{
(substr($_[0],0,$_[1]-1).'~', substr($_[0],$_[1]-1));
}
form { break => \&tilde_break }
$format_str,
@data;
makes '~' the hyphenation character, whilst:
sub wrap_and_slop = sub($$$)
{
my ($text, $reqlen, $fldlen) = @_;
if ($reqlen==$fldlen) { $text =~ m/\A(\s*\S*)(.*)/s }
else { ("", $text) }
}
form { break => \&wrap_and_slop }
$format_str,
@data;
wraps excessively long words to the next line and "slops" them over
the right margin if necessary.
The Text::Reform package provides three functions to simplify the use
of variant hyphenation schemes. The exportable subroutine
C<Text::Reform::break_wrap> generates a reference to a subroutine
implementing the "wrap-and-slop" algorithm shown in the last example,
which could therefore be rewritten:
use Text::Reform qw( form break_wrap );
form { break => break_wrap }
$format_str,
@data;
The subroutine C<Text::Reform::break_with> 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:
use Text::Reform qw( form break_with );
form { break => break_with('~') }
$format_str,
@data;
The subroutine C<Text::Reform::break_at> takes a single string
argument and returns a reference to a sub which hyphenates by
breaking immediately after that string. For example:
use Text::Reform qw( form break_at );
form { break => break_at('-') }
"[[[[[[[[[[[[[[",
"The Newton-Raphson methodology";
# returns:
#
# "The Newton-
# Raphson
# methodology"
Note that this differs from the behaviour of C<break_with>, which
would be:
form { break => break_with('-') }
"[[[[[[[[[[[[[[",
"The Newton-Raphson methodology";
# returns:
#
# "The Newton-R-
# aphson metho-
# dology"
Hence C<break_at> is generally a better choice.
C<break_at> also takes an 'except' option, which tells the resulting
subroutine not to break in the middle of certain strings. For example:
form { break => break_at('-', {except=>qr/Newton-Raphson/}) }
"[[[[[[[[[[[[[[",
"The Newton-Raphson methodology";
# returns:
#
# "The
# Newton-Raphson
# methodology"
This option is particularly useful for preserving URLs.
The subroutine C<Text::Reform::break_TeX>
returns a reference to a sub which hyphenates using
Jan Pazdziora's TeX::Hyphen module. For example:
use Text::Reform qw( form break_wrap );
form { break => break_TeX }
$format_str,
@data;
Note that in the previous examples there is no leading '\&' before
C<break_wrap>, C<break_with>, or C<break_TeX>, since each is being
directly I<called> (and returns a reference to some other suitable
subroutine);
=head2 The C<form> formatting algorithm
The algorithm C<form> uses is:
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
=head2 C<form> examples
As an example of the use of C<form>, the following:
$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\n123.456789";
produces the following output:
1 A big long
----------------
12 piece of|
text to be|
formatted|
exquisite-|
ly|
=
123.0
=
123.4
=
123.456
Note that block fields in a multi-line format string,
cause the entire multi-line format to be repeated as
often as necessary.
Picture strings and replacement values are interleaved in the
traditional C<format> format, but care is needed to ensure that the
correct number of substitution values are provided. Another
example:
$report = form
'Name Rank Serial Number',
'==== ==== =============',
'<<<<<<<<<<<<< ^^^^ <<<<<<<<<<<<<',
$name, $rank, $serial_number,
''
'Age Sex Description',
'=== === ===========',
'^^^ ^^^ [[[[[[[[[[[',
$age, $sex, $description;
=head2 How C<form> consumes strings
Unlike C<format>, within C<form> non-block fields I<do> consume the text
they format, so the following:
$text = "a line of text to be formatted over three lines";
print form "<<<<<<<<<<\n <<<<<<<<\n <<<<<<\n",
$text, $text, $text;
produces:
a line of
text to
be fo-
not:
a line of
a line
a line
To achieve the latter effect, convert the variable arguments
to independent literals (by double-quoted interpolation):
$text = "a line of text to be formatted over three lines";
print form "<<<<<<<<<<\n <<<<<<<<\n <<<<<<\n",
"$text", "$text", "$text";
Although values passed from variable arguments are progressively consumed
I<within> C<form>, the values of the original variables passed to C<form>
are I<not> altered. Hence:
$text = "a line of text to be formatted over three lines";
print form "<<<<<<<<<<\n <<<<<<<<\n <<<<<<\n",
$text, $text, $text;
print $text, "\n";
will print:
a line of
text to
be fo-
a line of text to be formatted over three lines
To cause C<form> to consume the values of the original variables passed to
it, pass them as references. Thus:
$text = "a line of text to be formatted over three lines";
print form "<<<<<<<<<<\n <<<<<<<<\n <<<<<<\n",
\$text, \$text, \$text;
print $text, "\n";
will print:
a line of
text to
be fo-
rmatted over three lines
Note that, for safety, the "non-consuming" behaviour takes precedence,
so if a variable is passed to C<form> both by reference I<and> by value,
its final value will be unchanged.
=head2 Numerical formatting
The ">>>.<<<" and "]]].[[[" field specifiers may be used to format
numeric values about a fixed decimal place marker. For example:
print form '(]]]]].[[)', <<EONUMS;
1
1.0
1.001
1.009
123.456
1234567
one two
EONUMS
would print:
( 1.0 )
( 1.0 )
( 1.00)
( 1.01)
( 123.46)
(#####.##)
(?????.??)
(?????.??)
Fractions are rounded to the specified number of places after the
decimal, but only significant digits are shown. That's why, in the
above example, 1 and 1.0 are formatted as "1.0", whilst 1.001 is
formatted as "1.00".
You can specify that the maximal number of decimal places always be used
by giving the configuration option 'numeric' a value that matches
/\bAllPlaces\b/i. For example:
print form { numeric => AllPlaces },
'(]]]]].[[)', <<'EONUMS';
1
1.0
EONUMS
would print:
( 1.00)
( 1.00)
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.
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 /\bSkipNaN\b/i in which case, any invalid numeric data is simply
ignored. For example:
print form { numeric => 'SkipNaN' }
'(]]]]].[[)',
<<EONUMS;
1
two three
4
EONUMS
would print:
( 1.0 )
( 4.0 )
=head2 Filling block fields with lists of values
If an argument corresponding to a field is an array reference, then C<form>
automatically joins the elements of the array into a single string, separating
each element with a newline character. As a result, a call like this:
@values = qw( 1 10 100 1000 );
print form "(]]]].[[)", \@values;
will print out
( 1.00)
( 10.00)
( 100.00)
(1000.00)
as might be expected.
Note however that arrays must be passed by reference (so that C<form>
knows that the entire array holds data for a single field). If the previous
example had not passed @values by reference:
@values = qw( 1 10 100 1000 );
print form "(]]]].[[)", @values;
the output would have been:
( 1.00)
10
100
1000
This is because @values would have been interpolated into C<form>'s
argument list, so only $value[0] would have been used as the data for
the initial format string. The remaining elements of @value would have
been treated as separate format strings, and printed out "verbatim".
Note too that, because arrays must be passed using a reference, their
original contents are consumed by C<form>, just like the contents of
scalars passed by reference.
To avoid having an array consumed by C<form>, pass it as an anonymous
array:
print form "(]]]].[[)", [@values];
=head2 Headers, footers, and pages
The C<form> subroutine can also insert headers, footers, and page-feeds
as it formats. These features are controlled by the "header", "footer",
"pagefeed", "pagelen", and "pagenum" options.
The "pagenum" option takes a scalar value or a reference to a scalar
variable and starts page numbering at that value. If a reference to a
scalar variable is specified, the value of that variable is updated as
the formatting proceeds, so that the final page number is available in
it after formatting. This can be useful for multi-part reports.
The "pagelen" option specifies the total number of lines in a page (including
headers, footers, and page-feeds).
The "pagewidth" option specifies the total number of columns in a page.
If the "header" option is specified with a string value, that string is
used as the header of every page generated. If it is specified as a reference
to a subroutine, that subroutine is called at the start of every page and
its return value used as the header string. When called, the subroutine is
passed the current page number.
Likewise, if the "footer" option is specified with a string value, that
string is used as the footer of every page generated. If it is specified
as a reference to a subroutine, that subroutine is called at the I<start>
of every page and its return value used as the footer string. When called,
the footer subroutine is passed the current page number.
Both the header and footer options can also be specified as hash references.
In this case the hash entries for keys "left", "centre" (or "center"), and
"right" specify what is to appear on the left, centre, and right of the
header/footer. The entry for the key "width" specifies how wide the
footer is to be. If the "width" key is omitted, the "pagewidth" configuration
option (which defaults to 72 characters) is used.
The "left", "centre", and "right" values may be literal
strings, or subroutines (just as a normal header/footer specification may
be.) See the second example, below.
Another alternative for header and footer options is to specify them as a
subroutine that returns a hash reference. The subroutine is called for each
page, then the resulting hash is treated like the hashes described in the
preceding paragraph. See the third example, below.
The "pagefeed" option acts in exactly the same way, to produce a
pagefeed which is appended after the footer. But note that the pagefeed
is not counted as part of the page length.
All three of these page components are recomputed at the start of each
new page, before the page contents are formatted (recomputing the header
and footer first makes it possible to determine how many lines of data to
format so as to adhere to the specified page length).
When the call to C<form> is complete and the data has been fully formatted,
the footer subroutine is called one last time, with an extra argument of 1.
The string returned by this final call is used as the final footer.
So for example, a 60-line per page report, starting at page 7,
with appropriate headers and footers might be set up like so:
$page = 7;
form { header => sub { "Page $_[0]\n\n" },
footer => sub { my ($pagenum, $lastpage) = @_;
return "" if $lastpage;
return "-"x50 . "\n"
.form ">"x50, "...".($pagenum+1);
},
pagefeed => "\n\n",
pagelen => 60
pagenum => \$page,
},
$template,
@data;
Note the recursive use of C<form> within the "footer" option!
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:
form { header => { right => "Running head" },
footer => { centre => sub { "Page $_[0]" } },
pagelen => 60
},
$template,
@data;
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):
form { header => { right => "Running head" },
footer => sub { return {centre => "Page $_[0]"} },
pagelen => 60
},
$template,
@data;
=head2 The C<cols> option
Sometimes data to be used in a C<form> 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:
@name = qw(Tom Dick Harry);
@score = qw( 88 54 99);
@time = qw( 15 13 18);
print form
'-------------------------------',
'Name Score Time',
'-------------------------------',
'[[[[[[[[[[[[[[ ||||| ||||',
\@name, \@score, \@time;
if the data is aggregrated by rows:
@data = (
{ name=>'Tom', score=>88, time=>15 },
{ name=>'Dick', score=>54, time=>13 },
{ name=>'Harry', score=>99, time=>18 },
);
you need to do some fancy mapping before it can be fed to C<form>:
print form
'-------------------------------',
'Name Score Time',
'-------------------------------',
'[[[[[[[[[[[[[[ ||||| ||||',
[map $$_{name}, @data],
[map $$_{score}, @data],
[map $$_{time} , @data];
Or you could just use the C<'cols'> option:
use Text::Reform qw(form columns);
print form
'-------------------------------',
'Name Score Time',
'-------------------------------',
'[[[[[[[[[[[[[[ ||||| ||||',
{ cols => [qw(name score time)],
from => \@data
};
This option takes an array of strings that specifies the keys of the
hash entries to be extracted into columns. The C<'from'> 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 C<form>'s argument list a reference to an array containing
the entries for that key, extracted from each of the hash references
supplied by C<'from'>. So, for example, the option:
{ cols => [qw(name score time)],
from => \@data
}
is replaced by three array references, the first containing the C<'name'>
entries for each hash inside C<@data>, the second containing the
C<'score'> entries for each hash inside C<@data>, and the third
containing the C<'time'> entries for each hash inside C<@data>.
If, instead, you have a list of arrays containing the data:
@data = (
# Time Name Score
[ 15, 'Tom', 88 ],
[ 13, 'Dick', 54 ],
[ 18, 'Harry', 99 ],
);
the C<'cols'> option can extract the appropriate columns for that too. You
just specify the required indices, rather than keys:
print form
'-----------------------------',
'Name Score Time',
'-----------------------------',
'[[[[[[[[[[[[[[ ||||| ||||',
{ cols => [1,2,0],
from => \@data
}
Note that the indices can be in any order, and the resulting arrays are
returned in the same order.
If you need to merge columns extracted from two hierarchical
data structures, just concatenate the data structures first,
like so:
print form
'---------------------------------------',
'Name Score Time Ranking
'---------------------------------------',
'[[[[[[[[[[[[[[ ||||| |||| |||||||',
{ cols => [1,2,0],
from => [@data, @olddata],
}
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:
print form
'-----------------------------',
'Name Score Time'
'-----------------------------',
'[[[[[[[[[[[[[[ ||||| ||||',
{ cols=>[1,2,0], from=>\@data },
'[[[[[[[[[[[[[[ ||||| ||||',
{ cols=>[3,8,1], from=>\@olddata },
'[[[[[[[[[[[[[[ ||||| ||||',
{ cols=>[qw(name score time)], from=>\@otherdata };
=head2 The C<tag> sub
The C<tag> 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 "/I<tag>" variant),
unless an explicit end-tag is provided as the third argument.
The tag specifier consists of the following components (in order):
=over 4
=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
=item An optional tag indent
Zero or more whitespace characters. Both the tag and the end-tag are indented
by this whitespace.
=item An optional left (opening) tag delimiter
Zero or more non-"word" characters (not alphanumeric or '_').
If the opening delimiter is omitted, the character '<' is used.
=item A tag
One or more "word" characters (alphanumeric or '_').
=item Optional tag arguments
Any number of any characters
=item An optional right (closing) tag delimiter
Zero or more non-"word" characters which balance some sequential portion
of the opening tag delimiter. For example, if the opening delimiter
is "<-(" then any of the following are acceptible closing delimiters:
")->", "->", or ">".
If the closing delimiter is omitted, the "inverse" of the opening delimiter
is used (for example, ")->"),
=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.
=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).
=back
For example:
$text = "three lines\nof tagged\ntext";
print tag "A HREF=#nextsection", $text;
prints:
<A HREF=#nextsection>three lines
of tagged
text</A>
whereas:
print tag "[-:GRIN>>>\n", $text;
prints:
[-:GRIN>>>:-]
three lines
of tagged
text
[-:/GRIN>>>:-]
and:
print tag "\n\n <BOLD>\n\n ", $text, "<END BOLD>";
prints:
S< >
<BOLD>
three lines
of tagged
text
<END BOLD>
S< >
(with the indicated spacing fore and aft).
=head1 AUTHOR
Damian Conway (damian@conway.org)
=head1 BUGS
The module uses C<POSIX::strtod>, which may be broken under certain versions
of Windows. Applying the WINDOWS_PATCH patch to Reform.pm will replace the
POSIX function with a copycat subroutine.
There are undoubtedly serious bugs lurking somewhere in code this funky :-)
Bug reports and other feedback are most welcome.
=head1 LICENCE AND COPYRIGHT
Copyright (c) 1997-2007, Damian Conway C<< <DCONWAY@CPAN.org> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE 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 "AS IS" 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.
IN 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 (INCLUDING 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), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
|