/usr/share/perl5/Data/TreeDumper.pm is in libdata-treedumper-perl 0.40-1.
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 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 | package Data::TreeDumper ;
use 5.006 ;
use strict ;
use warnings ;
use Carp ;
use Check::ISA ;
require Exporter ;
our @ISA = qw(Exporter) ;
our %EXPORT_TAGS = ('all' => [ qw() ]) ;
our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'} } ) ;
our @EXPORT = qw(DumpTree PrintTree DumpTrees CreateChainingFilter);
our $VERSION = '0.40' ;
my $WIN32_CONSOLE ;
BEGIN
{
if($^O ne 'MSWin32')
{
eval "use Term::Size;" ;
die $@ if $@ ;
}
else
{
eval "use Win32::Console;" ;
die $@ if $@ ;
$WIN32_CONSOLE= new Win32::Console;
}
}
use Text::Wrap ;
use Class::ISA ;
use Sort::Naturally ;
#-------------------------------------------------------------------------------
# setup values
#-------------------------------------------------------------------------------
our %setup =
(
FILTER => undef
, FILTER_ARGUMENT => undef
, LEVEL_FILTERS => undef
, TYPE_FILTERS => undef
, USE_ASCII => 1
, MAX_DEPTH => -1
, INDENTATION => ''
, NO_OUTPUT => 0
, START_LEVEL => 1
, VIRTUAL_WIDTH => 120
, DISPLAY_ROOT_ADDRESS => 0
, DISPLAY_ADDRESS => 1
, DISPLAY_PATH => 0
, DISPLAY_OBJECT_TYPE => 1
, DISPLAY_INHERITANCE => 0
, DISPLAY_TIE => 0
, DISPLAY_AUTOLOAD => 0
, DISPLAY_PERL_SIZE => 0
, DISPLAY_PERL_ADDRESS => 0
, NUMBER_LEVELS => 0
, COLOR_LEVELS => undef
, GLYPHS => ['| ', '|- ', '`- ', ' ']
, QUOTE_HASH_KEYS => 0
, QUOTE_VALUES => 0
, REPLACEMENT_LIST => [ ["\n" => '[\n]'], ["\r" => '[\r]'], ["\t" => '[\t]'] ]
, DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH => 0
, DISPLAY_CALLER_LOCATION=> 0
, __DATA_PATH => ''
, __PATH_ELEMENTS => []
, __TYPE_SEPARATORS => {
'' => ['<SCALAR:', '>']
, 'REF' => ['<', '>']
, 'CODE' => ['<CODE:', '>']
, 'HASH' => ['{\'', '\'}']
, 'ARRAY' => ['[', ']']
, 'SCALAR' => ['<SCALAR_REF:', '>']
}
) ;
#----------------------------------------------------------------------
# package variables à la Data::Dumper (as is the silly naming scheme)
#----------------------------------------------------------------------
our $Filter = $setup{FILTER} ;
our $Filterarguments = $setup{FILTER_ARGUMENT} ;
our $Levelfilters = $setup{LEVEL_FILTERS} ;
our $Typefilters = $setup{TYPE_FILTERS} ;
our $Useascii = $setup{USE_ASCII} ;
our $Maxdepth = $setup{MAX_DEPTH} ;
our $Indentation = $setup{INDENTATION} ;
our $Nooutput = $setup{NO_OUTPUT} ;
our $Startlevel = $setup{START_LEVEL} ;
our $Virtualwidth = $setup{VIRTUAL_WIDTH} ;
our $Displayrootaddress = $setup{DISPLAY_ROOT_ADDRESS} ;
our $Displayaddress = $setup{DISPLAY_ADDRESS} ;
our $Displaypath = $setup{DISPLAY_PATH} ;
our $Displayobjecttype = $setup{DISPLAY_OBJECT_TYPE} ;
our $Displayinheritance = $setup{DISPLAY_INHERITANCE} ;
our $Displaytie = $setup{DISPLAY_TIE} ;
our $Displayautoload = $setup{DISPLAY_AUTOLOAD} ;
our $Displayperlsize = $setup{DISPLAY_PERL_SIZE} ;
our $Displayperladdress = $setup{DISPLAY_PERL_ADDRESS} ;
our $Numberlevels = $setup{NUMBER_LEVELS} ;
our $Colorlevels = $setup{COLOR_LEVELS} ;
our $Glyphs = [@{$setup{GLYPHS}}] ; # we don't want it to be shared
our $Quotehashkeys = $setup{QUOTE_HASH} ;
our $Quotevalues = $setup{QUOTE_VALUES} ;
our $ReplacementList = [@{$setup{REPLACEMENT_LIST}}] ; # we don't want it to be shared
our $Displaynumberofelementsovermaxdepth = $setup{DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH} ;
our $Displaycallerlocation= $setup{DISPLAY_CALLER_LOCATION} ;
#~ our $Deparse = 0 ; # not implemented
sub GetPackageSetup
{
return
(
FILTER => $Data::TreeDumper::Filter
, FILTER_ARGUMENT => $Data::TreeDumper::Filterarguments
, LEVEL_FILTERS => $Data::TreeDumper::Levelfilters
, TYPE_FILTERS => $Data::TreeDumper::Typefilters
, USE_ASCII => $Data::TreeDumper::Useascii
, MAX_DEPTH => $Data::TreeDumper::Maxdepth
, INDENTATION => $Data::TreeDumper::Indentation
, NO_OUTPUT => $Data::TreeDumper::Nooutput
, START_LEVEL => $Data::TreeDumper::Startlevel
, VIRTUAL_WIDTH => $Data::TreeDumper::Virtualwidth
, DISPLAY_ROOT_ADDRESS => $Data::TreeDumper::Displayrootaddress
, DISPLAY_ADDRESS => $Data::TreeDumper::Displayaddress
, DISPLAY_PATH => $Data::TreeDumper::Displaypath
, DISPLAY_OBJECT_TYPE => $Data::TreeDumper::Displayobjecttype
, DISPLAY_INHERITANCE => $Data::TreeDumper::Displayinheritance
, DISPLAY_TIE => $Data::TreeDumper::Displaytie
, DISPLAY_AUTOLOAD => $Data::TreeDumper::Displayautoload
, DISPLAY_PERL_SIZE => $Data::TreeDumper::Displayperlsize
, DISPLAY_PERL_ADDRESS => $Data::TreeDumper::Displayperladdress
, NUMBER_LEVELS => $Data::TreeDumper::Numberlevels
, COLOR_LEVELS => $Data::TreeDumper::Colorlevels
, GLYPHS => $Data::TreeDumper::Glyphs
, QUOTE_HASH_KEYS => $Data::TreeDumper::Quotehashkeys
, REPLACEMENT_LIST => $Data::TreeDumper::ReplacementList
, DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH => $Displaynumberofelementsovermaxdepth
, DISPLAY_CALLER_LOCATION=> $Displaycallerlocation
, __DATA_PATH => ''
, __PATH_ELEMENTS => []
, __TYPE_SEPARATORS => $setup{__TYPE_SEPARATORS}
) ;
}
#-------------------------------------------------------------------------------
# API
#-------------------------------------------------------------------------------
sub PrintTree
{
my ($package, $file_name, $line) = caller() ;
print DumpTree(@_, DUMPER_NAME => "PrintTree at '$file_name:$line'") ;
}
sub DumpTree
{
my $structure_to_dump = shift ;
my $title = shift ;
my %overrides = @_ ;
$title = defined $title ? $title : '' ;
my ($package, $file_name, $line) = caller() ;
my $location = '' ;
if($Displaycallerlocation)
{
$location = defined $overrides{DUMPER_NAME} ? $overrides{DUMPER_NAME} : "DumpTree at '$file_name:$line'" ;
}
unless(defined $structure_to_dump)
{
return("$title (undefined variable) $location\n") ;
}
if('' eq ref $structure_to_dump)
{
return("$title $structure_to_dump (scalar variable) $location\n");
}
if($Displaycallerlocation)
{
print "$location\n" ;
}
my %local_setup ;
if(exists $overrides{NO_PACKAGE_SETUP} && $overrides{NO_PACKAGE_SETUP})
{
%local_setup = (%setup, %overrides) ;
}
else
{
%local_setup = (GetPackageSetup(), %overrides) ;
}
unless (exists $local_setup{TYPE_FILTERS}{Regexp})
{
# regexp objecjts (created with qr) are dumped by the below sub
$local_setup{TYPE_FILTERS}{Regexp} =
sub
{
my ($regexp) = @_ ;
return ('HASH', {REGEXP=> "$regexp"}, 'REGEXP') ;
} ;
}
return(TreeDumper($structure_to_dump, {TITLE => $title, %local_setup})) ;
}
#-------------------------------------------------------------------------------
sub DumpTrees
{
my @trees = grep {'ARRAY' eq ref $_} @_ ;
my %global_overrides = grep {'ARRAY' ne ref $_} @_ ;
my $dump = '' ;
for my $tree (@trees)
{
my ($structure_to_dump, $title, %overrides) = @{$tree} ;
$title = defined $title ? $title : '' ;
if(defined $structure_to_dump)
{
$dump .= DumpTree($structure_to_dump, $title, %global_overrides, %overrides) ;
}
else
{
my ($package, $file_name, $line) = caller() ;
$dump .= "DumpTrees can't dump 'undef' with title: '$title' @ '$file_name:$line'.\n" ;
}
}
return($dump) ;
}
#-------------------------------------------------------------------------------
# The dumper
#-------------------------------------------------------------------------------
sub TreeDumper
{
my $tree = shift ;
my $setup = shift ;
my $level = shift || 0 ;
my $levels_left = shift || [] ;
my $tree_type = ref $tree ;
confess "TreeDumper can only display objects passed by reference!\n" if('' eq $tree_type) ;
my $already_displayed_nodes = shift || {$tree => GetReferenceType($tree) . 'O', NEXT_INDEX => 1} ;
return('') if ($setup->{MAX_DEPTH} == $level) ;
#--------------------------
# perl data size
#--------------------------
if($level == 0)
{
eval 'use Devel::Size qw(size total_size) ;' ;
if($@)
{
# shoud we warn ???
delete $setup->{DISPLAY_PERL_SIZE} ;
}
}
local $Devel::Size::warn = 0 if($level == 0) ;
#--------------------------
# filters
#--------------------------
my ($filter_sub, $filter_argument) = GetFilter($setup, $level, ref $tree) ;
my ($replacement_tree, @nodes_to_display) ;
if(defined $filter_sub)
{
($tree_type, $replacement_tree, @nodes_to_display)
= $filter_sub->($tree, $level, $setup->{__DATA_PATH}, undef, $setup, $filter_argument) ;
$tree = $replacement_tree if(defined $replacement_tree) ;
}
else
{
($tree_type, undef, @nodes_to_display) = DefaultNodesToDisplay($tree) ;
}
return('') unless defined $tree_type ; #easiest way to prune in a filter is to return undef as type
# filters can change the name of the nodes by passing an array ref
my @node_names ;
for my $node (@nodes_to_display)
{
if(ref $node eq 'ARRAY')
{
push @node_names, $node->[1] ;
$node = $node->[0] ; # Modify $nodes_to_display
}
else
{
push @node_names, $node ;
}
}
#--------------------------
# dump
#--------------------------
my $output = '' ;
$output .= RenderRoot($tree, $setup) if($level == 0) ;
my ($opening_bracket, $closing_bracket) = GetBrackets($setup, $tree_type) ;
for (my $node_index = 0 ; $node_index < @nodes_to_display ; $node_index++)
{
my $nodes_left = (@nodes_to_display - 1) - $node_index ;
$levels_left->[$level] = $nodes_left ;
my @separator_data = GetSeparator
(
$level
, $nodes_left
, $levels_left
, $setup->{START_LEVEL}
, $setup->{GLYPHS}
, $setup->{COLOR_LEVELS}
) ;
my ($element, $element_name, $element_address, $element_id)
= GetElement($tree, $tree_type, \@nodes_to_display, \@node_names, $node_index, $setup);
my $is_terminal_node = IsTerminalNode
(
$element
, $element_name
, $level
, $setup
) ;
if(! $is_terminal_node && exists $already_displayed_nodes->{$element_address})
{
$is_terminal_node = 1 ;
}
my $element_name_rendering =
defined $tree
? RenderElementName
(
\@separator_data
, $element, $element_name, $element_address, $element_id
, $level
, $levels_left
, $already_displayed_nodes
, $setup
)
: '' ;
unless($is_terminal_node)
{
local $setup->{__DATA_PATH} = "$setup->{__DATA_PATH}$opening_bracket$element_name$closing_bracket" ;
push @{$setup->{__PATH_ELEMENTS}}, [$tree_type, $element_name, $tree] ;
my $sub_tree_dump = TreeDumper($element, $setup, $level + 1, $levels_left, $already_displayed_nodes) ;
$output .= $element_name_rendering .$sub_tree_dump ;
pop @{$setup->{__PATH_ELEMENTS}} ;
}
else
{
$output .= $element_name_rendering ;
}
}
RenderEnd(\$output, $setup) if($level == 0) ;
return($output) ;
}
#-------------------------------------------------------------------------------
sub GetFilter
{
my ($setup, $level, $type) = @_ ;
my $filter_sub = $setup->{FILTER} ;
# specific level filter has higher priority
my $level_filters = $setup->{LEVEL_FILTERS} ;
$filter_sub = $level_filters->{$level} if(defined $level_filters && exists $level_filters->{$level}) ;
my $type_filters = $setup->{TYPE_FILTERS} ;
$filter_sub = $type_filters->{$type} if(defined $type_filters && exists $type_filters->{$type}) ;
unless ('CODE' eq ref $filter_sub || ! defined $filter_sub)
{
my ($package, $file_name, $line) = caller(2) ;
die "DumpTree: FILTER must be sub reference at '$file_name:$line'" ;
}
return($filter_sub, $setup->{FILTER_ARGUMENT}) ;
}
#-------------------------------------------------------------------------------
sub GetElement
{
my ($tree, $tree_type, $nodes_to_display, $node_names, $node_index, $setup) = @_ ;
my ($element, $element_name, $element_address, $element_id) ;
for($tree)
{
# TODO, move this out of the loop with static table of functions
($tree_type eq 'HASH' || obj($tree, 'HASH')) and do
{
$element = $tree->{$nodes_to_display->[$node_index]} ;
$element_address = "$element" if defined $element ;
if($setup->{QUOTE_HASH_KEYS})
{
$element_name = "'$node_names->[$node_index]'" ;
}
else
{
$element_name = $node_names->[$node_index] ;
}
$element_id = \($tree->{$nodes_to_display->[$node_index]}) ;
last
} ;
($tree_type eq 'ARRAY' || obj($tree, 'ARRAY')) and do
{
#~ # debug while writting Diff module
#~ unless(defined $nodes_to_display->[$node_index])
#~ {
#~ use Data::Dumper ;
#~ print Dumper $nodes_to_display ;
#~ my ($package, $file_name, $line) = caller() ;
#~ print "Called from $file_name, $line\n" ;
#~ print "$tree->\[$nodes_to_display->\[$node_index\]\]\n" ;
#~ }
$element = $tree->[$nodes_to_display->[$node_index]] ;
$element_address = "$element" if defined $element ;
$element_name = $node_names->[$node_index] ;
$element_id = \($tree->[$nodes_to_display->[$node_index]]) ;
last ;
} ;
($tree_type eq 'REF' || obj($tree, 'REF')) and do
{
$element = $$tree ;
$element_address = "$element" if defined $element ;
my $sub_type = '?' ;
for($element)
{
my $element_type = ref $element;
($element_type eq '' || obj($element, 'HASH')) and do
{
$sub_type = 'scalar' ;
last ;
} ;
($element_type eq 'HASH' || obj($element, 'HASH')) and do
{
$sub_type = 'HASH' ;
last ;
} ;
($element_type eq 'ARRAY' || obj($element, 'ARRAY')) and do
{
$sub_type = 'ARRAY' ;
last ;
} ;
($element_type eq 'REF' || obj($element, 'REF')) and do
{
$sub_type = 'REF' ;
last ;
} ;
($element_type eq 'CODE' || obj($element, 'CODE')) and do
{
$sub_type = 'CODE' ;
last ;
} ;
($element_type eq 'SCALAR' || obj($element, 'SCALAR')) and do
{
$sub_type = 'SCALAR REF' ;
last ;
} ;
}
$element_name = "$tree to $sub_type" ;
$element_id = $tree ;
last ;
} ;
($tree_type eq 'CODE' || obj($tree, 'CODE')) and do
{
$element = $tree ;
$element_address = "$element" if defined $element ;
$element_name = $tree ;
$element_id = $tree ;
last ;
} ;
($tree_type eq 'SCALAR' || obj($tree, 'SCALAR')) and do
#~ ('SCALAR' eq $_ or 'GLOB' eq $_) and do
{
$element = $$tree ;
$element_address = "$element" if defined $element ;
$element_name = '?' ;
$element_id = $tree ;
last ;
} ;
}
return ($element, $element_name, $element_address, $element_id) ;
}
#----------------------------------------------------------------------
sub RenderElementName
{
my
(
$separator_data
, $element, $element_name, $element_address, $element_id
, $level
, $levels_left
, $already_displayed_nodes
, $setup
) = @_ ;
my @rendering_elements = GetElementInfo
(
$element
, $element_name
, $element_address
, $element_id
, $level
, $already_displayed_nodes
, $setup
) ;
my $output = RenderNode
(
$element
, $element_name
, $level
, @$separator_data
, @rendering_elements
, $setup
) ;
return($output) ;
}
#-------------------------------------------------------------------------------
sub GetBrackets
{
my ($setup, $tree_type) = @_ ;
my ($opening_bracket, $closing_bracket) ;
if(exists $setup->{__TYPE_SEPARATORS}{$tree_type})
{
($opening_bracket, $closing_bracket) = @{$setup->{__TYPE_SEPARATORS}{$tree_type}} ;
}
else
{
($opening_bracket, $closing_bracket) = ('<Unknown type!', '>') ;
}
return($opening_bracket, $closing_bracket) ;
}
#-------------------------------------------------------------------------------
sub RenderEnd
{
my ($output_ref, $setup) = @_ ;
return('') if $setup->{NO_OUTPUT} ;
if(defined $setup->{RENDERER}{END})
{
$$output_ref .= $setup->{RENDERER}{END}($setup) ;
}
else
{
unless ($setup->{USE_ASCII})
{
# convert to ANSI
$$output_ref =~ s/\| /\033(0\170 \033(B/g ;
$$output_ref =~ s/\|- /\033(0\164\161 \033(B/g ;
$$output_ref =~ s/\`- /\033(0\155\161 \033(B/g ;
}
}
}
#-------------------------------------------------------------------------------
sub RenderRoot
{
my ($tree, $setup) = @_ ;
my $output = '' ;
if(defined $setup->{RENDERER} && '' eq ref $setup->{RENDERER})
{
eval <<EOE ;
use Data::TreeDumper::Renderer::$setup->{RENDERER} ;
\$setup->{RENDERER} = Data::TreeDumper::Renderer::$setup->{RENDERER}::GetRenderer() ;
EOE
die "Data::TreeDumper couldn't load renderer '$setup->{RENDERER}':\n$@" if $@ ;
}
if(defined $setup->{RENDERER}{NAME})
{
eval <<EOE ;
use Data::TreeDumper::Renderer::$setup->{RENDERER}{NAME} ;
\$setup->{RENDERER} = {%{\$setup->{RENDERER}}, %{Data::TreeDumper::Renderer::$setup->{RENDERER}{NAME}::GetRenderer()}} ;
EOE
die "Data::TreeDumper couldn't load renderer '$setup->{RENDERER}{NAME}':\n$@" if $@ ;
}
unless($setup->{NO_OUTPUT})
{
my $root_tie_and_class = GetElementTieAndClass($setup, $tree) ;
if(defined $setup->{RENDERER}{BEGIN})
{
my $root_address = '' ;
$root_address = GetReferenceType($tree) . 'O' if($setup->{DISPLAY_ROOT_ADDRESS}) ;
my $perl_address = '' ;
$perl_address = $tree if($setup->{DISPLAY_PERL_ADDRESS}) ;
my $perl_size = '' ;
$perl_size = total_size($tree) if($setup->{DISPLAY_PERL_SIZE}) ;
$output .= $setup->{RENDERER}{BEGIN}($setup->{TITLE} . $root_tie_and_class, $root_address, $tree, $perl_size, $perl_address, $setup) ;
}
else
{
$output .= $setup->{INDENTATION} ;
$output .= defined $setup->{TITLE} ? $setup->{TITLE} : '' ;
$output .= $root_tie_and_class ;
$output .= ' [' . GetReferenceType($tree) . "0]" if($setup->{DISPLAY_ROOT_ADDRESS}) ;
$output .= " $tree" if($setup->{DISPLAY_PERL_ADDRESS}) ;
$output .= " <" . total_size($tree) . ">" if($setup->{DISPLAY_PERL_SIZE}) ;
$output .= "\n" ;
}
}
return($output) ;
}
#-------------------------------------------------------------------------------
sub RenderNode
{
my
(
$element
, $element_name
, $level
, $previous_level_separator
, $separator
, $subsequent_separator
, $separator_size
, $is_terminal_node
, $perl_size
, $perl_address
, $tag
, $element_value
, $default_element_rendering
, $dtd_address
, $address_field
, $address_link
, $setup
) = @_ ;
my $output = '' ;
return('') if $setup->{NO_OUTPUT} ;
if(defined $setup->{RENDERER}{NODE})
{
#~ #TODO: some elements are not available in this function, pass them from caller
$output .= $setup->{RENDERER}{NODE}
(
$element
, $level
, $is_terminal_node
, $previous_level_separator
, $separator
, $element_name
, $element_value
, $dtd_address
, $address_link
, $perl_size
, $perl_address
, $setup
) ;
}
else
{
#--------------------------
# wrapping
#--------------------------
my $level_text = GetLevelText($element, $level, $setup) ;
my $tree_header = $setup->{INDENTATION} . $level_text . $previous_level_separator . $separator ;
my $tree_subsequent_header = $setup->{INDENTATION} . $level_text . $previous_level_separator . $subsequent_separator ;
my $element_description = $element_name . $default_element_rendering ;
$perl_size = " <$perl_size> " unless $perl_size eq '' ;
$element_description .= " $address_field$perl_size$perl_address\n" ;
if($setup->{NO_WRAP})
{
$output .= $tree_header ;
$output .= $element_description ;
}
else
{
my ($columns, $rows) = ('', '') ;
if(defined $setup->{WRAP_WIDTH})
{
$columns = $setup->{WRAP_WIDTH} ;
}
else
{
if(defined $^O)
{
if($^O ne 'MSWin32')
{
eval "(\$columns, \$rows) = Term::Size::chars *STDOUT{IO} ;" ;
}
else
{
($columns, $rows) = $WIN32_CONSOLE->Size();
}
}
if($columns eq '')
{
$columns = $setup->{VIRTUAL_WIDTH} ;
}
}
local $Text::Wrap::columns = $columns ;
local $Text::Wrap::unexpand = 0 ;
if(length($tree_header) + length($element_description) > $columns && ! $setup->{NO_WRAP})
{
$output .= wrap
(
$tree_header
, $tree_subsequent_header
, $element_description
) ;
}
else
{
$output .= $tree_header ;
$output .= $element_description ;
}
}
}
return($output) ;
}
#-------------------------------------------------------------------------------
sub GetElementInfo
{
my
(
$element
, $element_name
, $element_address
, $element_id
, $level
, $already_displayed_nodes
, $setup
) = @_ ;
my $perl_size = '' ;
$perl_size = total_size($element) if($setup->{DISPLAY_PERL_SIZE}) ;
my $perl_address = "" ;
my $tag = '' ;
my $element_value = '' ;
my $is_terminal_node = 0 ;
my $default_element_rendering = '' ;
for(ref $element)
{
'' eq $_ and do
{
$is_terminal_node++ ;
$tag = 'S' ;
$element_address = $already_displayed_nodes->{NEXT_INDEX} ;
my $value = defined $element ? $element : 'undef' ;
$element_value = "$value" ;
my $replacement_list = $setup->{REPLACEMENT_LIST} ;
if(defined $replacement_list)
{
for my $replacement (@$replacement_list)
{
my $find = $replacement->[0] ;
my $replace = $replacement->[1] ;
$element_value =~ s/$find/$replace/g ;
}
}
if($setup->{QUOTE_VALUES} && defined $element)
{
$default_element_rendering = " = '$element_value'" ;
}
else
{
$default_element_rendering = " = $element_value" ;
}
$perl_address = "$element_id" if($setup->{DISPLAY_PERL_ADDRESS}) ;
# $setup->{DISPLAY_TIE} doesn't make sense as scalars are copied
last ;
} ;
'HASH' eq $_ and do
{
$is_terminal_node = IsTerminalNode
(
$element
, $element_name
, $level
, $setup
) ;
$tag = 'H' ;
$perl_address = "$element" if($setup->{DISPLAY_PERL_ADDRESS}) ;
if(! %{$element} && ! $setup->{NO_NO_ELEMENTS})
{
$default_element_rendering = $element_value = ' (no elements)' ;
}
if
(
%{$element}
&&
(
(($setup->{MAX_DEPTH} == $level + 1) && $setup->{DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH})
|| $setup->{DISPLAY_NUMBER_OF_ELEMENTS}
)
)
{
my $number_of_elements = keys %{$element} ;
my $plural = $number_of_elements > 1 ? 's' : '' ;
my $elements = ' (' . $number_of_elements . ' element' . $plural . ')' ;
$default_element_rendering .= $elements ;
$element_value .= $elements ;
}
if($setup->{DISPLAY_TIE} && (my $tie = tied %$element))
{
$tie =~ s/=.*$// ;
my $tie = " (tied to '$tie')" ;
$default_element_rendering .= $tie ;
$element_value .= $tie ;
}
last ;
} ;
'ARRAY' eq $_ and do
{
$is_terminal_node = IsTerminalNode
(
$element
, $element_name
, $level
, $setup
) ;
$tag = 'A' ;
$perl_address = "$element" if($setup->{DISPLAY_PERL_ADDRESS}) ;
if(! @{$element} && ! $setup->{NO_NO_ELEMENTS})
{
$default_element_rendering = $element_value .= ' (no elements)' ;
}
if
(
@{$element}
&&
(
(($setup->{MAX_DEPTH} == $level + 1) && $setup->{DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH})
|| $setup->{DISPLAY_NUMBER_OF_ELEMENTS}
)
)
{
my $plural = scalar(@{$element}) ? 's' : '' ;
my $elements = ' (' . @{$element} . ' element' . $plural . ')' ;
$default_element_rendering .= $elements ;
$element_value .= $elements ;
}
if($setup->{DISPLAY_TIE} && (my $tie = tied @$element))
{
$tie =~ s/=.*$// ;
my $tie = " (tied to '$tie')" ;
$default_element_rendering .= $tie ;
$element_value .= $tie ;
}
last ;
} ;
'CODE' eq $_ and do
{
$is_terminal_node++ ;
$tag = 'C' ;
#~ use Data::Dump::Streamer;
#~ $element_value = "----- " . Dump($element)->Out() ;
$element_value = "$element" ;
$default_element_rendering= " = $element_value" ;
$perl_address = "$element_id" if($setup->{DISPLAY_PERL_ADDRESS}) ;
last ;
} ;
'SCALAR' eq $_ and do
{
$is_terminal_node = 0 ;
$tag = 'RS' ;
$element_address = $element_id ;
$perl_address = "$element_id" if($setup->{DISPLAY_PERL_ADDRESS}) ;
last ;
} ;
'GLOB' eq $_ and do
{
$is_terminal_node++ ;
$tag = 'G' ;
$perl_address = "$element" if($setup->{DISPLAY_PERL_ADDRESS}) ;
last ;
} ;
'REF' eq $_ and do
{
$is_terminal_node = 0 ;
$tag = 'R' ;
$perl_address = $element if($setup->{DISPLAY_PERL_ADDRESS}) ;
last ;
} ;
# DEFAULT, an object.
$tag = 'O' ;
my $object_elements = '' ;
if( obj($element, 'HASH') )
{
$tag = 'OH' ;
if
(
%{$element}
&&
(
(($setup->{MAX_DEPTH} == $level + 1) && $setup->{DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH})
|| $setup->{DISPLAY_NUMBER_OF_ELEMENTS}
)
)
{
my $number_of_elements = keys %{$element} ;
my $plural = $number_of_elements > 1 ? 's' : '' ;
$object_elements = ' (' . $number_of_elements . ' element' . $plural . ')' ;
}
}
elsif(obj($element, 'ARRAY'))
{
$tag = 'OA' ;
if
(
@{$element}
&&
(
(($setup->{MAX_DEPTH} == $level + 1) && $setup->{DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH})
|| $setup->{DISPLAY_NUMBER_OF_ELEMENTS}
)
)
{
my $plural = scalar(@{$element}) ? 's' : '' ;
$object_elements = ' (' . @{$element} . ' element' . $plural . ')' ;
}
}
elsif(obj($element, 'GLOB'))
{
$tag = 'OG' ;
}
elsif(obj($element, 'SCALAR'))
{
$tag = 'OS' ;
}
$perl_address = "$element" if($setup->{DISPLAY_PERL_ADDRESS}) ;
($is_terminal_node, my $element_value)
= IsTerminalNode
(
$element
, $element_name
, $level
, $setup
) ;
if($setup->{DISPLAY_OBJECT_TYPE})
{
$element_value .= GetElementTieAndClass($setup, $element) ;
$default_element_rendering = " = $element_value" ;
}
$default_element_rendering .= $object_elements ;
}
# address
my $dtd_address = $tag . $already_displayed_nodes->{NEXT_INDEX} ;
my $address_field = '' ;
my $address_link ;
if(exists $already_displayed_nodes->{$element_address})
{
$already_displayed_nodes->{NEXT_INDEX}++ ;
$address_field = " [$dtd_address -> $already_displayed_nodes->{$element_address}]" if $setup->{DISPLAY_ADDRESS} ;
$address_link = $already_displayed_nodes->{$element_address} ;
$is_terminal_node = 1 ;
}
else
{
$already_displayed_nodes->{$element_address} = $dtd_address ;
$already_displayed_nodes->{$element_address} .= " /$setup->{__DATA_PATH}" if $setup->{DISPLAY_PATH};
$already_displayed_nodes->{NEXT_INDEX}++ ;
$address_field = " [$dtd_address]" if $setup->{DISPLAY_ADDRESS} ;
}
return
(
$is_terminal_node
, $perl_size
, $perl_address
, $tag
, $element_value
, $default_element_rendering
, $dtd_address
, $address_field
, $address_link
) ;
}
#----------------------------------------------------------------------
sub IsTerminalNode
{
my
(
$element
, $element_name
, $level
, $setup
) = @_ ;
my $is_terminal_node = 0 ;
my $element_value = '' ;
my ($filter_sub, $filter_argument) = GetFilter($setup, $level, ref $element) ;
for(ref $element)
{
'' eq $_ and do
{
$is_terminal_node = 1 ;
last ;
} ;
'HASH' eq $_ and do
{
# node is terminal if it has no children
$is_terminal_node++ unless %$element ;
# node might be terminal if filter says it has no children
if(!$is_terminal_node && defined $setup->{RENDERER}{NODE})
{
if(defined $filter_sub)
{
my @children_nodes_to_display ;
local $setup->{__DATA_PATH} = "$setup->{__DATA_PATH}\{$element_name\}" ;
(undef, undef, @children_nodes_to_display)
= $filter_sub->($element, $level + 1, $setup->{__DATA_PATH}, undef, $setup, $filter_argument) ;
$is_terminal_node++ unless @children_nodes_to_display ;
}
}
last ;
} ;
'ARRAY' eq $_ and do
{
# node is terminal if it has no children
$is_terminal_node++ unless(@$element) ;
# node might be terminal if filter says it has no children
if(!$is_terminal_node && defined $setup->{RENDERER}{NODE})
{
if(defined $filter_sub)
{
my @children_nodes_to_display ;
local $setup->{__DATA_PATH} = "$setup->{__DATA_PATH}\[$element_name\]" ;
(undef, undef, @children_nodes_to_display)
= $filter_sub->($element, $level + 1, $setup->{__DATA_PATH}, undef, $setup, $filter_argument) ;
$is_terminal_node++ unless @children_nodes_to_display ;
}
}
last ;
} ;
'CODE' eq $_ and do
{
$is_terminal_node = 1 ;
last ;
} ;
'SCALAR' eq $_ and do
{
$is_terminal_node = 0 ;
last ;
} ;
'GLOB' eq $_ and do
{
$is_terminal_node = 1 ;
last ;
} ;
'REF' eq $_ and do
{
$is_terminal_node = 0 ;
last ;
} ;
# DEFAULT, an object.
#check if the object is empty and display that state if NO_NO_ELEMENT isn't set
for($element)
{
obj($_, 'HASH') and do
{
unless(%$element)
{
$is_terminal_node++ ;
unless($setup->{NO_NO_ELEMENTS})
{
$element_value = "(Hash, empty) $element_value" ;
}
}
last ;
} ;
obj($_, 'ARRAY/') and do
{
unless(@$element)
{
$is_terminal_node++ ;
unless($setup->{NO_NO_ELEMENTS})
{
$element_value = "(Array, empty) $element_value" ;
}
}
last ;
} ;
}
}
return($is_terminal_node, $element_value) if wantarray ;
return($is_terminal_node) ;
}
#----------------------------------------------------------------------
sub GetElementTieAndClass
{
my ($setup, $element) = @_ ;
my $element_type = '' ;
if($setup->{DISPLAY_TIE})
{
if(obj($element, 'HASH') && (my $tie_hash = tied %$element))
{
$tie_hash =~ s/=.*$// ;
$element_type .= " (tied to '$tie_hash' [H])"
}
elsif(obj($element, 'ARRAY') && (my $tie_array = tied @$element))
{
$tie_array =~ s/=.*$// ;
$element_type .= " (tied to '$tie_array' [A])"
}
elsif(obj($element, 'SCALAR') && (my $tie_scalar = tied $$element))
{
$tie_scalar =~ s/=.*$// ;
$element_type .= " (tied to '$tie_scalar' [RS])"
}
elsif(obj($element, 'GLOB') && (my $tie_glob = tied *$element))
{
$tie_glob =~ s/=.*$// ;
$element_type .= " (tied to '$tie_glob' [G])"
}
}
for(ref $element)
{
'' eq $_ || 'HASH' eq $_ || 'ARRAY' eq $_ || 'CODE' eq $_ || 'SCALAR' eq $_ || 'GLOB' eq $_ || 'REF' eq $_ and do
{
last ;
} ;
# an object.
if($setup->{DISPLAY_OBJECT_TYPE})
{
my $class = ref($element) ;
my $has_autoload = $class->can("AUTOLOAD") ? '[AL]' : '' ;
$element_type .= " blessed in '$has_autoload$class'" ;
if($setup->{DISPLAY_INHERITANCE})
{
for my $base_class (Class::ISA::super_path(ref($element)))
{
if($setup->{DISPLAY_AUTOLOAD})
{
no warnings ;
eval "\$has_autoload = *${base_class}::AUTOLOAD{CODE} ;" ;
if($has_autoload)
{
$element_type .= " <- [AL]$base_class " ;
}
else
{
$element_type .= " <- $base_class " ;
}
}
else
{
$element_type .= " <- $base_class " ;
}
}
}
}
}
return($element_type) ;
}
#----------------------------------------------------------------------
# filters
#----------------------------------------------------------------------
sub DefaultNodesToDisplay
{
my ($tree, undef, undef, $keys) = @_ ;
return('', undef) if '' eq ref $tree ;
my $tree_type = ref $tree ;
if('HASH' eq $tree_type)
{
return('HASH', undef, @$keys) if(defined $keys) ;
return('HASH', undef, nsort keys %$tree) ;
}
if('ARRAY' eq $tree_type)
{
return('ARRAY', undef, @$keys) if(defined $keys) ;
return('ARRAY', undef, (0 .. @$tree - 1)) ;
}
return('SCALAR', undef, (0)) if('SCALAR' eq $tree_type) ;
return('REF', undef, (0)) if('REF' eq $tree_type) ;
return('CODE', undef, (0)) if('CODE' eq $tree_type) ;
my @nodes_to_display ;
undef $tree_type ;
for($tree)
{
obj($_, 'HASH') and do
{
@nodes_to_display = nsort keys %$tree ;
$tree_type = 'HASH' ;
last ;
} ;
obj($_, 'ARRAY') and do
{
@nodes_to_display = (0 .. @$tree - 1) ;
$tree_type = 'ARRAY' ;
last ;
} ;
obj($_, 'GLOB') and do
{
@nodes_to_display = (0) ;
$tree_type = 'REF' ;
last ;
} ;
obj($_, 'SCALAR') and do
{
@nodes_to_display = (0) ;
$tree_type = 'REF' ;
last ;
} ;
warn "TreeDumper: Unsupported underlying type for $tree.\n" ;
}
return($tree_type, undef, @nodes_to_display) ;
}
#-------------------------------------------------------------------------------
sub CreateChainingFilter
{
my @filters = @_ ;
return sub
{
my ($tree, $level, $path, $keys) = @_ ;
my ($tree_type, $replacement_tree);
for my $filter (@filters)
{
($tree_type, $replacement_tree, @$keys) = $filter->($tree, $level, $path, $keys) ;
$tree = $replacement_tree if (defined $replacement_tree) ;
}
return ($tree_type, $replacement_tree, @$keys) ;
}
} ;
#-------------------------------------------------------------------------------
# rendering support
#-------------------------------------------------------------------------------
{ # make %types private
my %types =
(
'' => 'SCALAR! not a reference!'
, 'REF' => 'R'
, 'CODE' => 'C'
, 'HASH' => 'H'
, 'ARRAY' => 'A'
, 'SCALAR' => 'RS'
) ;
sub GetReferenceType
{
my $element = shift ;
my $reference = ref $element ;
if(exists $types{$reference})
{
return($types{$reference}) ;
}
else
{
my $tag = 'O?' ;
if($element =~ /=HASH/ )
{
$tag = 'OH' ;
}
elsif($element =~ /=ARRAY/)
{
$tag = 'OA' ;
}
elsif($element =~ /=GLOB/)
{
$tag = 'OG' ;
}
elsif($element =~ /=SCALAR/)
{
$tag = 'OS' ;
}
return($tag) ;
}
}
} # make %types private
#-------------------------------------------------------------------------------
sub GetLevelText
{
my ($element, $level, $setup) = @_ ;
my $level_text = '' ;
if($setup->{NUMBER_LEVELS})
{
if('CODE' eq ref $setup->{NUMBER_LEVELS})
{
$level_text = $setup->{NUMBER_LEVELS}->($element, $level, $setup) ;
}
else
{
my $color_levels = $setup->{COLOR_LEVELS} ;
my ($color_start, $color_end) = ('', '') ;
if($color_levels)
{
if('ARRAY' eq ref $color_levels)
{
my $color_index = $level % @{$color_levels->[0]} ;
($color_start, $color_end) = ($color_levels->[0][$color_index] , $color_levels->[1]) ;
}
else
{
# assume code
($color_start, $color_end) = $color_levels->($level) ;
}
}
$level_text = sprintf("$color_start%$setup->{NUMBER_LEVELS}d$color_end ", ($level + 1)) ;
}
}
return($level_text) ;
}
#----------------------------------------------------------------------
sub GetSeparator
{
my
(
$level
, $is_last_in_level
, $levels_left
, $start_level
, $glyphs
, $colors # array or code ref
) = @_ ;
my $separator_size = 0 ;
my $previous_level_separator = '' ;
my ($color_start, $color_end) = ('', '') ;
for my $current_level ((1 - $start_level) .. ($level - 1))
{
$separator_size += 3 ;
if($colors)
{
if('ARRAY' eq ref $colors)
{
my $color_index = $current_level % @{$colors->[0]} ;
($color_start, $color_end) = ($colors->[0][$color_index] , $colors->[1]) ;
}
else
{
if('CODE' eq ref $colors)
{
($color_start, $color_end) = $colors->($current_level) ;
}
#else
# ignore other types
}
}
if(! defined $levels_left->[$current_level] || $levels_left->[$current_level] == 0)
{
#~ $previous_level_separator .= "$color_start $color_end" ;
$previous_level_separator .= "$color_start$glyphs->[3]$color_end" ;
}
else
{
#~ $previous_level_separator .= "$color_start| $color_end" ;
$previous_level_separator .= "$color_start$glyphs->[0]$color_end" ;
}
}
my $separator = '' ;
my $subsequent_separator = '' ;
$separator_size += 3 ;
if($level > 0 || $start_level)
{
if($colors)
{
if('ARRAY' eq ref $colors)
{
my $color_index = $level % @{$colors->[0]} ;
($color_start, $color_end) = ($colors->[0][$color_index] , $colors->[1]) ;
}
else
{
# assume code
($color_start, $color_end) = $colors->($level) ;
}
}
if($is_last_in_level == 0)
{
#~ $separator = "$color_start`- $color_end" ;
#~ $subsequent_separator = "$color_start $color_end" ;
$separator = "$color_start$glyphs->[2]$color_end" ;
$subsequent_separator = "$color_start$glyphs->[3]$color_end" ;
}
else
{
#~ $separator = "$color_start|- $color_end" ;
#~ $subsequent_separator = "$color_start| $color_end" ;
$separator = "$color_start$glyphs->[1]$color_end" ;
$subsequent_separator = "$color_start$glyphs->[0]$color_end" ;
}
}
return
(
$previous_level_separator
, $separator
, $subsequent_separator
, $separator_size
) ;
}
#-------------------------------------------------------------------------------
1 ;
__END__
=head1 NAME
Data::TreeDumper - Improved replacement for Data::Dumper. Powerful filtering capability.
=head1 SYNOPSIS
use Data::TreeDumper ;
my $sub = sub {} ;
my $s =
{
A =>
{
a =>
{
}
, bbbbbb => $sub
, c123 => $sub
, d => \$sub
}
, C =>
{
b =>
{
a =>
{
a =>
{
}
, b => sub
{
}
, c => 42
}
}
}
, ARRAY => [qw(elment_1 element_2 element_3)]
} ;
#-------------------------------------------------------------------
# package setup data
#-------------------------------------------------------------------
$Data::TreeDumper::Useascii = 0 ;
$Data::TreeDumper::Maxdepth = 2 ;
print DumpTree($s, 'title') ;
print DumpTree($s, 'title', MAX_DEPTH => 1) ;
print DumpTrees
(
[$s, "title", MAX_DEPTH => 1]
, [$s2, "other_title", DISPLAY_ADDRESS => 0]
, USE_ASCII => 1
, MAX_DEPTH => 5
) ;
=head1 Output
title:
|- A [H1]
| |- a [H2]
| |- bbbbbb = CODE(0x8139fa0) [C3]
| |- c123 [C4 -> C3]
| `- d [R5]
| `- REF(0x8139fb8) [R5 -> C3]
|- ARRAY [A6]
| |- 0 [S7] = elment_1
| |- 1 [S8] = element_2
| `- 2 [S9] = element_3
`- C [H10]
`- b [H11]
`- a [H12]
|- a [H13]
|- b = CODE(0x81ab130) [C14]
`- c [S15] = 42
=head1 DESCRIPTION
Data::Dumper and other modules do a great job of dumping data
structures. Their output, however, often takes more brain power to
understand than the data itself. When dumping large amounts of data,
the output can be overwhelming and it can be difficult to see the
relationship between each piece of the dumped data.
Data::TreeDumper also dumps data in a tree-like fashion but I<hopefully>
in a format more easily understood.
=head2 Label
Each node in the tree has a label. The label contains a type and an address. The label is displayed to
the right of the entry name within square brackets.
| |- bbbbbb = CODE(0x8139fa0) [C3]
| |- c123 [C4 -> C3]
| `- d [R5]
| `- REF(0x8139fb8) [R5 -> C3]
=head3 Address
The addresses are linearly incremented which should make it easier to locate data.
If the entry is a reference to data already displayed, a B<->> followed with the address of the already displayed data is appended
within the label.
ex: c123 [C4 -> C3]
^ ^
| | address of the data refered to
|
| current element address
=head3 Types
B<S>: Scalar,
B<H>: Hash,
B<A>: Array,
B<C>: Code,
B<R>: Reference,
B<RS>: Scalar reference.
B<Ox>: Object, where x is the object undelying type
=head2 Empty Hash or Array
No structure is displayed for empty hashes or arrays, the string "no elements" is added to the display.
|- A [S10] = string
|- EMPTY_ARRAY (no elements) [A11]
|- B [S12] = 123
=head1 Configuration and Overrides
Data::TreeDumper has configuration options you can set to modify the output it
generates. I<DumpTree> and I<PrintTree> take overrides as trailing arguments. Those
overrides are active within the current dump call only.
ex:
$Data::TreeDumper::Maxdepth = 2 ;
# maximum depth set to 1 for the duration of the call only
print DumpTree($s, 'title', MAX_DEPTH => 1) ;
PrintTree($s, 'title', MAX_DEPTH => 1) ; # shortcut for the above call
# maximum depth is 2
print DumpTree($s, 'title') ;
=head2 $Data::TreeDumper::Displaycallerlocation
This package variable is very useful when you use B<Data::TreeDumper> and don't know where you called
B<PrintTree> or B<DumpTree>, ie when debugging. It displays the filename and line of call on STDOUT.
It can't also be set as an override, DISPLAY_CALLER_LOCATION => 1.
=head2 NO_PACKAGE_SETUP
Sometimes, the package setup you have is not what you want to use. resetting the variable,
making a call and setting the variables back is borring. You can set B<NO_PACKAGE_SETUP> to
1 and I<DumpTree> will ignore the package setup for the call.
print Data::TreeDumper::DumpTree($s, "Using package data") ;
print Data::TreeDumper::DumpTree($s, "Not Using package data", NO_PACKAGE_SETUP => 1) ;
=head2 DISPLAY_ROOT_ADDRESS
By default, B<Data::TreeDumper> doesn't display the address of the root.
DISPLAY_ROOT_ADDRESS => 1 # show the root address
=head2 DISPLAY_ADDRESS
When the dumped data is not self-referential, displaying the address of each node clutters the display. You can
direct B<Data::TreeDumper> to not display the node address by using:
DISPLAY_ADDRESS => 0
=head2 DISPLAY_PATH
Add the path of the element to the its address.
DISPLAY_PATH => 1
ex: '- CopyOfARRAY [A39 -> A18 /{'ARRAY'}]
=head2 DISPLAY_OBJECT_TYPE
B<Data::TreeDumper> displays the package in which an object is blessed. You
can suppress this display by using:
DISPLAY_OBJECT_TYPE => 0
=head2 DISPLAY_INHERITANCE
B<Data::TreeDumper> will display the inheritance hierarchy for the object:
|- object = blessed in 'SuperObject' <- Potatoe [OH55]
| `- Data = 0 [S56]
=head2 DISPLAY_AUTOLOAD
if set, B<Data::TreeDumper> will tag the object type with '[A]' if the package has an AUTOLOAD function.
|- object_with_autoload = blessed in '[A]SuperObjectWithAutoload' <- Potatoe <- [A] Vegetable [O58]
| `- Data = 0 [S56]
=head2 DISPLAY_TIE
if DISPLAY_TIE is set, B<Data::TreeDumper> will display which packae the variable is tied to. This works for
hashes and arrays as well as for object which are based on hashes and arrays.
|- tied_hash (tied to 'TiedHash') [H57]
| `- x = 1 [S58]
|- tied_hash_object = (tied to 'TiedHash') blessed in 'SuperObject' <- [A]Potatoe <- Vegetable [O59]
| |- m1 = 1 [S60]
| `- m2 = 2 [S61]
=head2 PERL DATA
Setting one of the options below will show internal perl data:
Cells: <2234> HASH(0x814F20c)
|- A1 [H1] <204> HASH(0x824620c)
| `- VALUE [S2] = datadatadatadatadatadatadatadatadatadata <85>
|- A8 [H11] <165> HASH(0x8243d68)
| `- VALUE [S12] = C <46>
`- C2 [H19] <165> HASH(0x8243dc0)
`- VALUE [S20] = B <46>
=head3 DISPLAY_PERL_SIZE
Setting this option will show the size of the memory allocated for each element in the tree within angle brackets.
DISPLAY_PERL_SIZE => 1
The excellent L<Devel::Size> is used to compute the size of the perl data. If you have deep circular data structures,
expect the dump time to be slower, 50 times slower or more.
=head3 DISPLAY_PERL_ADDRESS
Setting this option will show the perl-address of the dumped data.
DISPLAY_PERL_ADDRESS => 1
=head2 REPLACEMENT_LIST
Scalars may contain non printable characters that you rather not see in a dump. One of the
most common is "\r" embedded in text string from dos files. B<Data::TreeDumper>, by default, replaces "\n" by
'[\n]' and "\r" by '[\r]'. You can set REPLACEMENT_LIST to an array ref containing elements which
are themselves array references. The first element is the character(s) to match and the second is
the replacement.
# a fancy and stricter replacement for \n and \r
my $replacement = [ ["\n" => '[**Fancy \n replacement**]'], ["\r" => '\r'] ] ;
print DumpTree($smed->{TEXT}, 'Text:', REPLACEMENT_LIST => $replacement) ;
=head2 QUOTE_HASH_KEYS
B<QUOTE_HASH_KEYS> and its package variable B<$Data::TreeDumper::Quotehashkeys> can be set if you wish to single quote
the hash keys. Hash keys are not quoted by default.
DumpTree(\$s, 'some data:', QUOTE_HASH_KEYS => 1) ;
# output
some data:
`- REF(0x813da3c) [H1]
|- 'A' [H2]
| |- 'a' [H3]
| |- 'b' [H4]
| | |- 'a' = 0 [S5]
=head2 QUOTE_VALUES
B<QUOTE_VALUES> and its package variable B<$Data::TreeDumper::Quotevalues> can be set if you wish to single quote
the scalar values.
DumpTree(\$s, 'Cells:', QUOTE_VALUES=> 1) ;
=head2 NO_NO_ELEMENTS
If this option is set, B<Data::TreeDumper> will not add 'no elements' to empty hashes and arrays
=head2 NO_OUTPUT
This option suppresses all output generated by Data::TreeDumper.
This is useful when you want to iterate through your data structures and
display the data yourself, manipulate the data structure, or do a search
(see L<using filter as iterators> below)
=head2 Filters
Data::TreeDumper can sort the tree nodes with a user defined subroutine. By default, hash keys are sorted.
FILTER => \&ReverseSort
FILTER_ARGUMENT => ['your', 'arguments']
The filter routine is passed these arguments:
=over 2
=item 1 - a reference to the node which is going to be displayed
=item 2 - the nodes depth (this allows you to selectively display elements at a certain depth)
=item 3 - the path to the reference from the start of the dump.
=item 4 - an array reference containing the keys to be displayed (see L<Filter chaining>)
=item 5 - the dumpers setup
=item 5 - the filter arguments (see below)
=back
The filter returns the node's type, an eventual new structure (see below) and a list of 'keys' to display. The keys are hash keys or array indexes.
In Perl:
($tree_type, $replacement_tree, @nodes_to_display) = $your_filter->($tree, $level, $path, $nodes_to_display, $setup) ;
Filter are not as complicated as they sound and they are very powerful,
especially when using the path argument. The path idea was given to me by
another module writer but I forgot whom. If this writer will contact me, I
will give him the proper credit.
Lots of examples can be found in I<filters.pl> and I'll be glad to help if
you want to develop a specific filter.
=head3 FILTER_ARGUMENT
it is possible to pass arguments to your filter, passing a reference allows you to modify
the arguments when the filter is run (that happends for each node).
sub SomeSub
{
my $counter = 0 ;
my $data_structure = {.....} ;
DumpTree($data_structure, 'title', FILTER => \&CountNodes, FILTER_ARGUMENT => \$counter) ;
print "\$counter = $counter\n" ;
}
sub CountNodes
{
my ($structure, $level, $path, $nodes_to_display, $setup, $counter) = @_ ;
$$counter++ ; # remember to pass references if you want them to be changed by the filter
return(DefaultNodesToDisplay($structure)) ;
}
=head3 Key removal
Entries can be removed from the display by not returning their keys.
my $s = {visible => '', also_visible => '', not_visible => ''} ;
my $OnlyVisible = sub
{
my $s = shift ;
if('HASH' eq ref $s)
{
return('HASH', undef, grep {! /^not_visible/} keys %$s) ;
}
return(Data::TreeDumper::DefaultNodesToDisplay($s)) ;
}
DumpTree($s, 'title', FILTER => $OnlyVisible) ;
=head3 Label changing
The label for a hash keys or an array index can be altered. This can be used to add visual information to the tree dump. Instead
of returning the key name, return an array reference containing the key name and the label you want to display.
You only need to return such a reference for the entries you want to change, thus a mix of scalars and array ref is acceptable.
sub StarOnA
{
# hash entries matching /^a/i have '*' prepended
my $tree = shift ;
if('HASH' eq ref $tree)
{
my @keys_to_dump ;
for my $key_name (keys %$tree)
{
if($key_name =~ /^a/i)
{
$key_name = [$key_name, "* $key_name"] ;
}
push @keys_to_dump, $key_name ;
}
return ('HASH', undef, @keys_to_dump) ;
}
return (Data::TreeDumper::DefaultNodesToDisplay($tree)) ;
}
print DumpTree($s, "Entries matching /^a/i have '*' prepended", FILTER => \&StarOnA) ;
If you use an ANSI terminal, you can also change the color of the label.
This can greatly improve visual search time.
See the I<label coloring> example in I<colors.pl>.
=head3 Structure replacement
It is possible to replace the whole data structure in a filter. This comes handy when you want to display a I<"worked">
version of the structure. You can even change the type of the data structure, for example changing an array to a hash.
sub ReplaceArray
{
# replace arrays with hashes!!!
my $tree = shift ;
if('ARRAY' eq ref $tree)
{
my $multiplication = $tree->[0] * $tree->[1] ;
my $replacement = {MULTIPLICATION => $multiplication} ;
return('HASH', $replacement, keys %$replacement) ;
}
return (Data::TreeDumper::DefaultNodesToDisplay($tree)) ;
}
print DumpTree($s, 'replace arrays with hashes!', FILTER => \&ReplaceArray) ;
Here is a real life example. B<Tree::Simple> (L<http://search.cpan.org/dist/Tree-Simple/>) allows one
to build tree structures. The child nodes are not directly in the parent object (hash). Here is an unfiltered
dump of a tree with seven nodes:
Tree::Simple through Data::TreeDumper
|- _children
| |- 0
| | |- _children
| | | `- 0
| | | |- _children
| | | |- _depth = 1
| | | |- _node = 1.1
| | | `- _parent
| | |- _depth = 0
| | |- _node = 1
| | `- _parent
| |- 1
| | |- _children
| | | |- 0
| | | | |- _children
| | | | |- _depth = 1
| | | | |- _node = 2.1
| | | | `- _parent
| | | |- 1
| | | | |- _children
| | | | |- _depth = 1
| | | | |- _node = 2.1a
| | | | `- _parent
| | | `- 2
| | | |- _children
| | | |- _depth = 1
| | | |- _node = 2.2
| | | `- _parent
| | |- _depth = 0
| | |- _node = 2
| | `- _parent
| `- 2
| |- _children
| |- _depth = 0
| |- _node = 3
| `- _parent
|- _depth = -1
|- _node = 0
`- _parent = root
This is nice for the developer but not for a user wanting to oversee the node hierarchy. One of the
possible filters would be:
FILTER => sub
{
my $s = shift ;
if('Tree::Simple' eq ref $s)
{
my $counter = 0 ;
return
(
'ARRAY'
, $s->{_children}
, map{[$counter++, $_->{_node}]} @{$s->{_children}} # index generation
) ;
}
return(Data::TreeDumper::DefaultNodesToDisplay($s)) ;
}
Which would give this much more readable output:
Tree::Simple through Data::TreeDumper2
|- 1
| `- 1.1
|- 2
| |- 2.1
| |- 2.1a
| `- 2.2
`- 3
What about counting the children nodes? The index generating code becomes:
map{[$counter++, "$_->{_node} [" . @{$_->{_children}} . "]"]} @{$s->{_children}}
Tree::Simple through Data::TreeDumper4
|- 1 [1]
| `- 1.1 [0]
|- 2 [3]
| |- 2.1 [0]
| |- 2.1a [0]
| `- 2.2 [0]
`- 3 [0]
=head3 Filter chaining
It is possible to chain filters. I<CreateChainingFilter> takes a list of filtering sub references.
The filters must properly handle the third parameter passed to them.
Suppose you want to chain a filter that adds a star before each hash key label, with a filter
that removes all (original) keys that match /^a/i.
sub AddStar
{
my $s = shift ;
my $level = shift ;
my $path = shift ;
my $keys = shift ;
if('HASH' eq ref $s)
{
$keys = [keys %$s] unless defined $keys ;
my @new_keys ;
for (@$keys)
{
if('' eq ref $_)
{
push @new_keys, [$_, "* $_"] ;
}
else
{
# another filter has changed the label
push @new_keys, [$_->[0], "* $_->[1]"] ;
}
}
return('HASH', undef, @new_keys) ;
}
return(Data::TreeDumper::DefaultNodesToDisplay($s)) ;
} ;
sub RemoveA
{
my $s = shift ;
my $level = shift ;
my $path = shift ;
my $keys = shift ;
if('HASH' eq ref $s)
{
$keys = [keys %$s] unless defined $keys ;
my @new_keys ;
for (@$keys)
{
if('' eq ref $_)
{
push @new_keys, $_ unless /^a/i ;
}
else
{
# another filter has changed the label
push @new_keys, $_ unless $_->[0] =~ /^a/i ;
}
}
return('HASH', undef, @new_keys) ;
}
return(Data::TreeDumper::DefaultNodesToDisplay($s)) ;
} ;
DumpTree($s, 'Chained filters', FILTER => CreateChainingFilter(\&AddStar, \&RemoveA)) ;
=head2 level Filters
It is possible to define one filter for a specific level. If a filter for a specific level exists it is used
instead of the global filter.
LEVEL_FILTERS => {1 => \&FilterForLevelOne, 5 => \&FilterForLevelFive ... } ;
=head2 Type Filters
You can define filters for specific types of references. This filter type has the highest priority.
here's a very simple filter that will display the specified keys for the types
print DumpTree
(
$data,
'title',
TYPE_FILTERS =>
{
'Config::Hierarchical' => sub {'HASH', undef, qw(CATEGORIES) },
'PBS2::Node' => sub {'HASH', undef, qw(CONFIG DEPENDENCIES MATCH) },,
}
) ;
=head2 Using filters as iterators
You can iterate through your data structures and display data yourself,
manipulate the data structure, or do a search. While iterating through the
data structure, you can prune arbitrary branches to speedup processing.
# this example counts the nodes in a tree (hash based)
# a node is counted if it has a '__NAME' key
# any field that starts with '__' is considered rivate and we prune so we don't recurse in it
# anything that is not a hash (the part of the tree that interests us in this case) is pruned
my $number_of_nodes_in_the_dependency_tree = 0 ;
my $node_counter =
sub
{
my $tree = shift ;
if('HASH' eq ref $tree && exists $tree->{__NAME})
{
$number_of_nodes_in_the_dependency_tree++ if($tree->{__NAME} !~ /^__/) ;
return('HASH', $tree, grep {! /^__/} keys %$tree) ; # prune to run faster
}
else
{
return('SCALAR', 1) ; # prune
}
} ;
DumpTree($dependency_tree, '', NO_OUTPUT => 1, FILTER => $node_counter) ;
See the example under L<FILTER> which passes arguments through Data::TreeDumper instead for using a closure as above
=head2 Start level
This configuration option controls whether the tree trunk is displayed or not.
START_LEVEL => 1:
$tree:
|- A [H1]
| |- a [H2]
| |- bbbbbb = CODE(0x8139fa0) [C3]
| |- c123 [C4 -> C3]
| `- d [R5]
| `- REF(0x8139fb8) [R5 -> C3]
|- ARRAY [A6]
| |- 0 [S7] = element_1
| |- 1 [S8] = element_2
START_LEVEL => 0:
$tree:
A [H1]
|- a [H2]
|- bbbbbb = CODE(0x8139fa0) [C3]
|- c123 [C4 -> C3]
`- d [R5]
`- REF(0x8139fb8) [R5 -> C3]
ARRAY [A6]
|- 0 [S7] = element_1
|- 1 [S8] = element_2
=head2 ASCII vs ANSI
You can direct Data:TreeDumper to output ANSI codes instead of ASCII characters. The display
will be much nicer but takes slightly longer (not significant for small data structures).
USE_ASCII => 0 # will use ANSI codes instead
=head2 Display number of elements
DISPLAY_NUMBER_OF_ELEMENTS => 1
When set, the number of elements of every array and hash is displayed (not for objects based on hashes and arrays).
=head2 Maximum depth of the dump
Controls the depth beyond which which we don't recurse into a structure. Default is -1, which
means there is no maximum depth. This is useful to limit the amount of data displayed.
MAX_DEPTH => 1
=head2 Number of elements not displayed because of maximum depth limit
Data::TreDumper will display the number of elements a hash or array has but that can not be displayed
because of the maximum depth setting.
DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH => 1
=head2 Indentation
Every line of the tree dump will be appended with the value of I<INDENTATION>.
INDENTATION => ' ' ;
=head1 Custom glyphs
You can change the glyphs used by B<Data::TreeDumper>.
DumpTree(\$s, 's', , GLYPHS => ['. ', '. ', '. ', '. ']) ;
# output
s
. REF(0x813da3c) [H1]
. . A [H2]
. . . a [H3]
. . . b [H4]
. . . . a = 0 [S5]
. . . . b = 1 [S6]
. . . . c [H7]
. . . . . a = 1 [S8]
Four glyphs must be given. They replace the standard glyphs ['| ', '|- ', '`- ', ' ']. It is also possible to set
the package variable B<$Data::TreeDumper::Glyphs>. B<USE_ASCII> should be set, which it is by default.
=head1 Level numbering and tagging
Data:TreeDumper can prepend the level of the current line to the tree glyphs. This can be very useful when
searching in tree dump either visually or with a pager.
NUMBER_LEVELS => 2
NUMBER_LEVELS => \&NumberingSub
NUMBER_LEVELS can be assigned a number or a sub reference. When assigned a number, Data::TreeDumper will use that value to
define the width of the field where the level is displayed. For more control, you can define a sub that returns a string to be displayed
on the left side of the tree glyphs. The example below tags all the nodes whose level is zero.
print DumpTree($s, "Level numbering", NUMBER_LEVELS => 2) ;
sub GetLevelTagger
{
my $level_to_tag = shift ;
sub
{
my ($element, $level, $setup) = @_ ;
my $tag = "Level $level_to_tag => ";
if($level == 0)
{
return($tag) ;
}
else
{
return(' ' x length($tag)) ;
}
} ;
}
print DumpTree($s, "Level tagging", NUMBER_LEVELS => GetLevelTagger(0)) ;
=head1 Level coloring
Another way to enhance the output for easier searching is to colorize it. Data::TreeDumper can colorize the glyph elements or whole levels.
If your terminal supports ANSI codes, using Term::ANSIColors and Data::TreeDumper together can greatly ease the reading of large dumps.
See the examples in 'B<color.pl>'.
COLOR_LEVELS => [\@color_codes, $reset_code]
When passed an array reference, the first element is an array containing coloring codes. The codes are indexed
with the node level modulo the size of the array. The second element is used to reset the color after the glyph is displayed. If the second
element is an empty string, the glyph and the rest of the level is colorized.
COLOR_LEVELS => \&LevelColoringSub
If COLOR_LEVEL is assigned a sub, the sub is called for each glyph element. It is passed the following elements:
=over 2
=item 1 - the nodes depth (this allows you to selectively display elements at a certain depth)
=back
It should return a coloring code and a reset code. If you return an
empty string for the reset code, the whole node is displayed using the last glyph element color.
If level numbering is on, it is also colorized.
=head1 Wrapping
B<Data::TreeDumper> uses the Text::Wrap module to wrap your data to fit your display. Entries can be
wrapped multiple times so they snuggly fit your screen.
| | |- 1 [S21] = 1
| | `- 2 [S22] = 2
| `- 3 [OH23 -> R17]
|- ARRAY_ZERO [A24]
|- B [S25] = scalar
|- Long_name Long_name Long_name Long_name Long_name Long_name
| Long_name Long_name Long_name Long_name Long_name Long_name
| Long_name Long_name Long_name Long_name Long_name [S26] = 0
You can direct DTD to not wrap your text by setting B<NO_WRAP => 1>.
=head2 WRAP_WIDTH
if this option is set, B<Data::TreeDumper> will use it instead for the console width.
=head1 Custom Rendering
B<Data::TreeDumper> has a plug-in interface for other rendering formats. The renderer callbacks are
set by overriding the native renderer. Thanks to Stevan Little author of Tree::Simple::View for getting
B<Data::TreeDumper> on this track. Check B<Data::TreeDumper::Renderer::DHTML>.
DumpTree
(
$s
, 'Tree'
, RENDERER =>
{
BEGIN => \&RenderDhtmlBegin
, NODE => \&RenderDhtmlNode
, END => \&RenderDhtmlEnd
# data needed by the renderer
, PREVIOUS_LEVEL => -1
, PREVIOUS_ADDRESS => 'ROOT'
}
) ;
=head2 Callbacks
=over 2
=item * {RENDERER}{BEGIN} is called before the traversal of the data structure starts. This allows you
to setup the document (ex:: html header).
my ($title, $type_address, $element, $size, $perl_address, $setup) = @_ ;
=over 4
=item 1 $title
=item 2 $type_address
=item 3 $element
=item 4 $perl_size
=item 5 $perl_address
=item 6 $setup
=back
=item * {RENDERER}{NODE} is called for each node in the data structure. The following arguments are passed to the callback
=over 4
=item 1 $element
=item 2 $level
=item 3 $is_terminal (whether a deeper structure will follow or not)
=item 4 $previous_level_separator (ASCII separators before this node)
=item 5 $separator (ASCII separator for this element)
=item 6 $element_name
=item 7 $element_value
=item 8 $td_address (address of the element, Ex: C12 or H34. Unique for each element)
=item 9 $link_address (link to another element, may be undef)
=item 10 $perl_size (size of the element in bytes, see option B<DISPLAY_PERL_SIZE>)
=item 11 $perl_address (address (physical) of the element, see option B<DISPLAY_PERL_ADDRESS>)
=item 12 $setup (the dumper's settings)
=back
=item * {RENDERER}{END} is called after the last node has been processed.
=item * {RENDERER}{ ... }Arguments to the renderer can be stores within the {RENDERER} hash.
=back
=head2 Renderer modules
Renderers should be defined in modules under B<Data::TreeDumper::Renderer> and should define a function
called I<GetRenderer>. I<GetRenderer> can be passed whatever arguments the developer whishes. It is
acceptable for the modules to also export a specifc sub.
print DumpTree($s, 'Tree', Data::TreeDumper::Renderer::DHTML::GetRenderer()) ;
or
print DumpTree($s, 'Tree', GetDhtmlRenderer()) ;
If B<{RENDERER}> is set to a scalar, B<Data::TreeDumper> will load the
specified module if it exists. I<GetRenderer> will be called without
arguments.
print DumpTree($s, 'Tree', RENDERER => 'DHTML') ;
If B<{RENDERER}{NAME}> is set to a scalar, B<Data::TreeDumper> will load the specified module if it exists. I<GetRenderer>
will be called without arguments. Arguments to the renderer can aither be passed to the GetRenderer sub or as elements in the {RENDERER} hash.
print DumpTree($s, 'Tree', RENDERER => {NAME => 'DHTML', STYLE => \$style) ;
=head1 Zero width console
When no console exists, while redirecting to a file for example, Data::TreeDumper uses the variable
B<VIRTUAL_WIDTH> instead. Default is 120.
VIRTUAL_WIDTH => 120 ;
=head1 OVERRIDE list
=over 2
=item * COLOR_LEVELS
=item * DISPLAY_ADDRESS
=item * DISPLAY_PATH
=item * DISPLAY_PERL_SIZE
=item * DISPLAY_ROOT_ADDRESS
=item * DISPLAY_PERL_ADDRESS
=item * FILTER
=item * GLYPHS
=item * INDENTATION
=item * LEVEL_FILTERS
=item * MAX_DEPTH
=item * DISPLAY_NUMBER_OF_ELEMENTS_OVER_MAX_DEPTH
=item * NUMBER_LEVELS
=item * QUOTE_HASH_KEYS
=item * QUOTE_VALUES
=item * REPLACEMENT_LIST
=item * START_LEVEL
=item * USE_ASCII
=item * WRAP_WIDTH
=item * VIRTUAL_WIDTH
=item * NO_OUTPUT
=item * DISPLAY_OBJECT_TYPE
=item * DISPLAY_INHERITANCE
=item * DISPLAY_TIE
=item * DISPLAY_AUTOLOAD
=back
=head1 Interface
=head2 Package Data (à la Data::Dumper (as is the silly naming scheme))
=head3 Configuration Variables
$Data::TreeDumper::Startlevel = 1 ;
$Data::TreeDumper::Useascii = 1 ;
$Data::TreeDumper::Maxdepth = -1 ;
$Data::TreeDumper::Indentation = '' ;
$Data::TreeDumper::Virtualwidth = 120 ;
$Data::TreeDumper::Displayrootaddress = 0 ;
$Data::TreeDumper::Displayaddress = 1 ;
$Data::TreeDumper::Displaypath = 0 ;
$Data::TreeDumper::Displayobjecttype = 1 ;
$Data::TreeDumper::Displayinheritance = 0 ;
$Data::TreeDumper::Displaytie = 0 ;
$Data::TreeDumper::Displayautoload = 0 ;
$Data::TreeDumper::Displayperlsize = 0 ;
$Data::TreeDumper::Displayperladdress = 0 ;
$Data::TreeDumper::Filter = \&FlipEverySecondOne ;
$Data::TreeDumper::Levelfilters = {1 => \&Filter_1, 5 => \&Filter_5} ;
$Data::TreeDumper::Numberlevels = 0 ;
$Data::TreeDumper::Glyphs = ['| ', '|- ', '`- ', ' '] ;
$Data::TreeDumper::Colorlevels = undef ;
$Data::TreeDumper::Nooutput = 0 ; # generate an output
$Data::TreeDumper::Quotehashkeys = 0 ;
$Data::TreeDumper::Displaycallerlocation = 0 ;
=head3 API
B<PrintTree>prints on STDOUT the output of B<DumpTree>.
B<DumpTree> uses the configuration variables defined above. It takes the following arguments:
=over 2
=item [1] structure_to_dump
=item [2] title, a string to prepended to the tree (optional)
=item [3] overrides (optional)
=back
print DumpTree($s, "title", MAX_DEPTH => 1) ;
B<DumpTrees> uses the configuration variables defined above. It takes the following arguments
=over 2
=item [1] One or more array references containing
=over 4
=item [a] structure_to_dump
=item [b] title, a string to prepended to the tree (optional)
=item [c] overrides (optional)
=back
=item [2] overrides (optional)
=back
print DumpTrees
(
[$s, "title", MAX_DEPTH => 1]
, [$s2, "other_title", DISPLAY_ADDRESS => 0]
, USE_ASCII => 1
, MAX_DEPTH => 5
) ;
=head1 Bugs
None that I know of in this release but plenty, lurking in the dark
corners, waiting to be found.
=head1 Examples
Four examples files are included in the distribution.
I<usage.pl> shows you how you can use B<Data::TreeDumper>.
I<filters.pl> shows you how you how to do advance filtering.
I<colors.pl> shows you how you how to colorize a dump.
I<try_it.pl> is meant as a scratch pad for you to try B<Data::TreeDumper>.
=head1 DEPENDENCY
B<Text::Wrap>.
B<Term::Size> or B<Win32::Console>.
Optional B<Devel::Size> if you want Data::TreeDumper to show perl sizes for the tree elements.
=head1 EXPORT
I<DumpTree>, I<DumpTrees> and I<CreateChainingFilter>.
=head1 AUTHOR
Khemir Nadim ibn Hamouda. <nadim@khemir.net>
Thanks to Ed Avis for showing interest and pushing me to re-write the documentation.
Copyright (c) 2003-2010 Nadim Ibn Hamouda el Khemir. All rights
reserved. This program is free software; you can redis-
tribute it and/or modify it under the same terms as Perl
itself.
If you find any value in this module, mail me! All hints, tips, flames and wishes
are welcome at <nadim@khemir.net>.
=head1 SEE ALSO
B<Data::TreeDumper::00>. B<Data::Dumper>.
B<Data::TreeDumper::Renderer::DHTML>.
B<Devel::Size::Report>.B<Devel::Size>.
B<PBS>: the Perl Build System from which B<Data::TreeDumper> was extracted.
=cut
|