/usr/lib/perl5/Data/Dump/Streamer.pm is in libdata-dump-streamer-perl 2.36-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 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 | package Data::Dump::Streamer;
use strict;
use warnings;
use warnings::register;
use B ();
use B::Deparse ();
use B::Utils ();
use Data::Dumper ();
use DynaLoader ();
use Exporter ();
use IO::File ();
use Symbol ();
use Text::Abbrev ();
use Text::Balanced ();
use overload ();
use Data::Dump::Streamer::_::Printers;
# use overload qw("" printit); # does diabolical stuff.
use vars qw(
$VERSION
$AUTOLOAD
@ISA
@EXPORT @EXPORT_OK @EXPORT_FAIL %EXPORT_TAGS
%Freeze
%Thaw
$DEBUG
$HasPadWalker
);
$DEBUG=0;
BEGIN{ $HasPadWalker=eval "use PadWalker 0.99; 1"; }
BEGIN {
$VERSION ='2.36';
$VERSION = eval $VERSION; # used for beta stuff.
@ISA = qw(Exporter DynaLoader);
@EXPORT=qw(Dump DumpLex DumpVars);
@EXPORT_OK = qw(
Dump
DumpLex
DumpVars
Stream
alias_av
alias_hv
alias_ref
push_alias
dualvar
alias_to
blessed
reftype
refaddr
refcount
sv_refcount
looks_like_number
regex
readonly
make_ro
_make_ro
reftype_or_glob
refaddr_or_glob
globname
is_numeric
all_keys
legal_keys
hidden_keys
lock_ref_keys
lock_keys
lock_ref_keys_plus
lock_keys_plus
SvREADONLY_ref
SvREFCNT_ref
isweak
weaken
weak_refcount
readonly_set
Dumper
DDumper
alias
sqz
usqz
);
%EXPORT_TAGS = (
undump => [ qw( alias_av alias_hv alias_ref make_ro
lock_ref_keys
lock_keys
lock_ref_keys_plus
lock_keys_plus
alias_to
dualvar
weaken
usqz
)
],
special=> [ qw( readonly_set ) ],
all => [ @EXPORT,@EXPORT_OK ],
alias => [ qw( alias_av alias_hv alias_ref push_alias ) ],
bin => [ @EXPORT_OK ],
Dumper => [ qw( Dumper DDumper )],
util => [ qw (
dualvar
blessed reftype refaddr refcount sv_refcount
readonly looks_like_number regex is_numeric
make_ro readonly_set reftype_or_glob
refaddr_or_glob globname
weak_refcount isweak weaken
)
],
);
sub alias_to { return shift }
#warn $VERSION;
Data::Dump::Streamer->bootstrap();
if ($]>=5.013010) {
# As I write this, 5.13.10 doesn't exist so I'm guessing that
# we can begin using the ordinary core function again.
eval q[
use re qw(regexp_pattern is_regexp);
*regex= *regexp_pattern;
] or die $@;
}
elsif ($]>=5.013006) {
# Perl-5.13.6 through perl-5.13.9 began returning modifier
# flags that weren't yet legal at the time.
eval q[
use re qw(regexp_pattern is_regexp);
sub regex {
if (wantarray) {
my ($pat,$mod) = regexp_pattern($_[0]);
if ($mod) {
$mod =~ tr/dlua?//d;
}
return ($pat,$mod);
}
else {
return scalar regexp_pattern($_[0]);
}
}
1;
] or die $@;
}
elsif ($]>=5.009004) {
eval q[
use re qw(regexp_pattern is_regexp);
*regex= *regexp_pattern;
1;
] or die $@;
}
else {
eval q[sub is_regexp($) { defined regex($_[0]) }];
}
if ($]<=5.008) {
*hidden_keys=sub(\%) { return () };
*legal_keys=sub(\%) { return keys %{$_[0]} };
*all_keys=sub(\%\@\@) { @{$_[1]}=keys %{$_[0]}; @$_[2]=(); };
}
if ( $]<5.008 ) {
no strict 'refs';
foreach my $sub (qw(lock_keys lock_keys_plus )) {
*$sub=sub(\%;@) {
warnings::warn "$sub doesn't do anything before Perl 5.8.0\n";
return $_[0];
}
}
foreach my $sub (qw(lock_ref_keys lock_ref_keys_plus )) {
*$sub=sub($;@) {
warnings::warn "$sub doesn't do anything before Perl 5.8.0\n";
return $_[0];
}
}
} else {
eval <<'EO_HU'
use Hash::Util qw(lock_keys);
sub lock_ref_keys($;@) {
my $hash=shift;
Carp::confess("lock_ref_keys(): Not a ref '$hash'")
unless ref $hash;
lock_keys(%$hash,@_);
$hash
}
EO_HU
;
*lock_ref_keys_plus=sub($;@){
my ($hash,@keys)=@_;
my @delete;
Internals::hv_clear_placeholders(%$hash);
foreach my $key (@keys) {
unless (exists($hash->{$key})) {
$hash->{$key}=undef;
push @delete,$key;
}
}
SvREADONLY_ref($hash,1);
delete @{$hash}{@delete};
$hash
};
*lock_keys_plus=sub(\%;@){lock_ref_keys_plus(@_)};
}
if ($] <= 5.008008) {
*disable_overloading = \&SvAMAGIC_off;
*restore_overloading = sub ($$) {
SvAMAGIC_on($_[0], undef);
# Visit all classes we are ISA and fetch the () entry from
# every stash.
my %done;
my %todo = (
$_[0] => undef,
UNIVERSAL => undef,
);
no strict 'refs';
for my $todo_class (keys %todo) {
delete $todo{$todo_class};
$done{$todo_class} = undef;
for my $isa (@{"${todo_class}::ISA"}) {
$todo{$isa} = undef unless exists $done{$isa};
}
}
};
} else {
*disable_overloading = sub ($) {
# we use eval because $_[0] might be read-only
# its a crappy solution, but whatever, it works
eval { bless $_[0], 'Does::Not::Exist' };
};
*restore_overloading = sub ($$) {
eval { bless $_[0], $_[1] }
};
}
my %fail=map { ( $_ => 1 ) } @EXPORT_FAIL;
@EXPORT_OK=grep { !$fail{$_} } @EXPORT_OK;
}
sub import {
my ($pkg) = @_;
my ($idx, $alias);
if ($idx = (grep lc($_[$_]) eq 'as', 0..$#_)) {
#print "found alias at $idx:\n";
($idx, $alias) = splice(@_, $idx, 2);
#print "found alias: $idx => $alias\n";
no strict 'refs';
*{$alias.'::'} = *{__PACKAGE__.'::'};
}
$pkg->export_to_level(1,@_);
}
# NOTE
# ----
# This module uses the term 'sv' in a way that is misleading.
# It doesn't always mean the same as it would in the core.
#
# 1. data is breadth first traversed first, in the pretty much
# self contained Data() routine which farms out a bit to
# _reg_ref and _reg_scalar which handle "registering" items for
# later use, such as their depth, refcount, "name", etc. But
# ONLY for references and scalars whose refcount is over 2.
# Most real SV's will have a refcount of 2 when we look at them
# (from the perl side) so we actually don't know about them (trust me)
# They _cant_ be referenced twice, and they can't be aliased so we can
# can just ignore them until the second pass.
# 2.Once this has happened Out() is called which starts off a
# normal depth first traverse over the structure. It calls into
# 3._dump_sv which in the case of a reference falls through to _dump_rv.
# Aliasing and a bunch of stuff like that are checked here before we even
# look at the reference type.
# 4.If its a ref we fall through to dumping the reference in _dump_rv.
# Here we handle duplicate refs, and manage depth checks, blessing, refs
#(which is scary nasty horrible code) and then pass on to _dump_type where
# type is one of 'code', 'qr', 'array' etc. Each of these which have children
# then call back into _dump_sv as required.
# 5. Because of the way perl works, we can't emit anything more than a DAG in a
# single statement, so for more complex structures we need to add in the broken
# links. I call these "fix statements", and they encompass copying reference
# values, creating aliases, or even dumping globs. When a fix statement is needed
# any of the _dump_foo methods will call _add_fix and add to the list of fixes.
# after every root level _dump_sv call from Out() any fix statements possible to be
# resolved will be emitted and removed from the fix list. This happens in
# _dump_apply_fix, which is another piece of horrible code.
#
# Anyway, its terribly ugly, but for anything I can think to throw at i works.
# demerphq
=encoding utf8
=head1 NAME
Data::Dump::Streamer - Accurately serialize a data structure as Perl code.
=head1 SYNOPSIS
use Data::Dump::Streamer;
use DDS; # optionally installed alias
Dump($x,$y); # Prints to STDOUT
Dump($x,$y)->Out(); # " "
my $o=Data::Dump::Streamer->new(); # Returns a new ...
my $o=Dump(); # ... uninitialized object.
my $o=Dump($x,$y); # Returns an initialized object
my $s=Dump($x,$y)->Out(); # " a string of the dumped obj
my @l=Dump($x,$y); # " a list of code fragments
my @l=Dump($x,$y)->Out(); # " a list of code fragments
Dump($x,$y)->To(\*STDERR)->Out(); # Prints to STDERR
Dump($x,$y)->Names('foo','bar') # Specify Names
->Out();
Dump($x,$y)->Indent(0)->Out(); # No indent
Dump($x,$y)->To(\*STDERR) # Output to STDERR
->Indent(0) # ... no indent
->Names('foo','bar') # ... specify Names
->Out(); # Print...
$o->Data($x,$y); # OO form of what Dump($x,$y) does.
$o->Names('Foo','Names'); # ...
$o->Out(); # ...
=head1 DESCRIPTION
Given a list of scalars or reference variables, writes out
their contents in perl syntax. The references can also be
objects. The contents of each variable is output using the least
number of Perl statements as convenient, usually only one.
Self-referential structures, closures, and objects are output
correctly.
The return value can be evaled to get back an identical copy
of the original reference structure. In some cases this may
require the use of utility subs that
L<Data::Dump::Streamer|Data::Dump::Streamer> will optionally
export.
This module is very similar in concept to the core module
L<Data::Dumper|Data::Dumper>, with the major differences
being that this module is designed to output to a stream
instead of constructing its output in memory (trading speed
for memory), and that the traversal over the data structure
is effectively breadth first versus the depth first
traversal done by the others.
In fact the data structure is scanned twice, first in
breadth first mode to perform structural analysis, and then
in depth first mode to actually produce the output, but
obeying the depth relationships of the first pass.
=head2 Caveats Dumping Closures (CODE Refs)
As of version 1.11 DDS has had the ability to dump closures properly. This
means that the lexicals that are bound to the closure are dumped along
with the subroutine that uses them. This makes it much easier to debug
code that uses closures and to a certain extent provides a persistency
framework for closure based code. The way this works is that DDS figures
out what all the lexicals are that are bound to CODE refs it is dumping
and then pretends that it had originally been called with all of them as
its arguments, (along with the original arguments as well of course.)
One consequence of the way the dumping process works is that all of the
recreated subroutines will be in the same scope. This of course can lead
to collisions as two subroutines can easily be bound to different
variables that have the same name.
The way that DDS resolves these collisions is that it renames one of the
variables with a special name so that presumably there are no collisions.
However this process is very simplistic with no checks to prevent
collisions with other lexicals or other globals that may be used by other
dumped code. In some situations it may be necessary to change the default
value of the rename template which may be done by using the C<EclipseName>
method.
Similarly to the problem of colliding lexicals is the problem of colliding
lexicals and globals. DDS pays no attention to globals when dumping
closures which can potentially result in lexicals being declared that will
eclipse their global namesake. There is currently no way around this other
than to avoid accessing a global and a lexical with the same name from the
subs being dumped. An example is
my $a = sub { $a++ };
Dump( sub { $a->() } );
which will not be dumped correctly. Generally speaking this kind of thing
is bad practice anyway, so this should probably be viewed as a "feature".
:-)
Generally if the closures being dumped avoid accessing lexicals and
globals with the same name from out of scope and that all of the CODE
being dumped avoids vars with the C<EclipseName> in their names the dumps
should be valid and should eval back into existence properly.
Note that the behaviour of dumping closures is subject to change in future
versions as its possible that I will put some additional effort into more
sophisticated ways of avoiding name collisions in the dump.
=head1 USAGE
While Data::Dump::Streamer is at heart an object oriented module, it is
expected (based on experience with using L<Data::Dumper|Data::Dumper>)
that the common case will not exploit these features. Nevertheless the
method based approach is convenient and accordingly a compromise hybrid
approach has been provided via the C<Dump()> subroutine. Such as
Dump($foo);
$as_string= Dump($foo)->Out();
All attribute methods are designed to be chained together. This means
that when used as set attribute (called with arguments) they return the
object they were called against. When used as get attributes (called
without arguments) they return the value of the attribute.
From an OO point of view the key methods are the C<Data()> and C<Out()>
methods. These correspond to the breadth first and depth first traversal,
and need to be called in this order. Some attributes I<must> be set prior
to the C<Data()> phase and some need only be set before the C<Out()>
phase.
Attributes once set last the lifetime of the object, unless explicitly
reset.
=head2 Controlling Object Representation
This module provides hooks to allow objects to override how they are
represented. The basic idea is that a subroutine (or method) is provided
which is responsible for the override. The return of the method governs
how the object will be represented when dumped, and how it will be
restored. The basic calling convention is
my ( $proxy, $thaw, $postop )= $callback->($obj);
#or = $obj->$method();
The L<C<Freezer()>|/Freezer> method controls what methods to use as a default method
and also allows per class overrides. When dumping an object of a given
class the first time it tries to execute the class specific handler if
it is specified, then the user specific generic handler if its been
specified and then "DDS_freeze". This means that class authors can
implement a C<DDS_freeze()> method and their objects will automatically
be serialized as necessary. B<Note> that if either the class specific or
generic handler is defined but false C<DDS_freeze()> will not be used
even if it is present.
The interface of the L<C<Freezer()>|/Freezer> handler in detail is as follows:
=over 4
=item B<C<$obj>>
The object being dumped.
=item B<C<$proxy>>
This is what will be dumped instead of C<$obj>. It may be one of
the following values:
=over 8
=item I<C<undef>> (first time only)
On the first time a serialization hook is called in a dump it may return
undef or the empty list to indicate that it shouldn't be used again for
this class during this pass. Any other time undef is treated the same
as false.
=item I<FALSE value>
A false value for C<$proxy> is taken to mean that it should be ignored.
Its like saying IgnoreClass(ref($obj)); B<Note> that undef has a special
meaning when the callback is called the first time.
=item I<A Reference>
A reference that will be dumped instead of the object.
=item I<Perl Code>
A string that is to be treated as code and inserted directly into the
dump stream as a proxy for the original. Note that the code must be
able to execute inline or in other words must evaluate to a perl EXPR.
Use C<do{}> to wrap multistatement code.
=back
=item B<C<$thaw>>
This values is used to allow extra control over how the object will be
recreated when dumped. It is used for converting the C<$proxy> representation
into the real thing. It is only relevant when C<$proxy> is a reference.
=over 8
=item I<FALSE value>
Indicates no thaw action is to be included for this object.
=item I<Sub or Method Name>
A string matching C<< /^(->)?((?:\w*::)\w+)(\(\))?$/ >> in which case it
is taken as a sub name when the string ends in () and a method name
when the string doesn't. If the C<< -> >> is present then the sub or method
is called inline. If it is not then the sub or method is called
after the main dump.
=item I<Perl Code>
Any other string, in which case the result will be taken as code
which will be emitted after the main dump. It will be wrapped
in a for loop that aliases C<$_> to the variable in question.
=back
=item B<C<$postdump>>
This is the similar to C<$thaw> but is called in process instead
of being emitted as part of the dump. Any return is ignored.
It is only relevant when C<$proxy> is a reference.
=over 8
=item I<FALSE value>
No postdump action is to occur.
=item I<Code Reference>
The code ref will be called after serialization is complete
with the object as the argument.
=item I<Method Name>
The method will be called after serialization is complete
=back
=back
An example DDS_freeze method is one I had to put together for an object
which contained a key whose value was a ref to an array tied to the value
of another key. Dumping this got crazy, so I wanted to suppress dumping
the tied array. I did it this way:
sub DDS_freeze {
my $self=shift;
delete $self->{'tie'};
return ($self,'->fix_tie','fix_tie');
}
sub fix_tie {
my $self=shift;
if ( ! $self->{'tie'} ) {
$self->{str}="" unless defined $self->{str};
tie my @a, 'Tie::Array::PackedC', $self->{str};
$self->{'tie'} = \@a;
}
return $self;
}
The C<$postop> means the object is relatively unaffected after the
dump, the C<$thaw> says that we should also include the method
inline as we dump. An example dump of an object like this might be
$Foo1=bless({ str=>'' },'Foo')->fix_tie();
Wheras if we omit the C<< -> >> then we would get:
$Foo1=bless({ str=>'' },'Foo');
$Foo1->fix_tie();
In our example it wouldn't actually make a difference, but the former
style can be nicer to read if the object is embedded in another.
However the non arrow notation is slightly more dangerous, in that
its possible that the internals of the object will not be fully linked
when the method is evaluated. The second form guarantees that the object
will be fully linked when the method is evaluated.
See L<Controlling Hash Traversal and Display Order> for a different way
to control the representation of hash based objects.
=head2 Controlling Hash Traversal and Display Order
When dumping a hash you may control the order the keys will be output
and which keys will be included. The basic idea is to specify a subroutine
which takes a hash as an argument and returns a reference to an array
containing the keys to be dumped.
You can use the L<KeyOrder()|/KeyOrder TYPE_OR_OBJ> routine or the L<SortKeys()|/SortKeys> routine to
specify the sorter to be used.
The routine will be called in the following way:
( $key_array, $thaw ) = $sorter->($hash,($pass=0),$addr,$class);
( $key_array,) = $sorter->($hash,($pass=1),$addr,$class);
C<$hash> is the hash to be dumped, C<$addr> is the refaddr() of the
C<$hash>, and C<$class> will be set if the hash has been blessed.
When C<$pass> is 0 the C<$thaw> variable may be supplied as well as the
keyorder. If it is defined then it specifies what thaw action to perform
after dumping the hash. See L<C<$thaw>|/$thaw> in L<Controlling Object
Representation> for details as to how it works. This allows an object
to define those keys needed to recreate itself properly, and a followup
hook to recreate the rest.
B<Note> that if a L<Freezer()|/Freezer> method is defined and returns
a L<C<$thaw>|/$thaw> then the L<C<$thaw>|/$thaw> returned by the sorter
will override it.
=head2 Controlling Array Presentation and Run Length Encoding
By default Data::Dump::Streamer will "run length encode" array values.
This means that when an array value is simple (ie, its not referenced and
does contain a reference) and is repeated multiple times the output will
be single a list multiplier statement, and not each item output
separately. Thus: L<C<Dump([0,0,0,0])>|/Dump> will be output something like
$ARRAY1 = [ (0) x 4 ];
This is particularly useful when dealing with large arrays that are only
partly filled, and when accidentally the array has been made very large,
such as with the improper use of pseudo-hash notation.
To disable this feature you may set the L<Rle()|/Rle> property to FALSE, by
default it is enabled and set to TRUE.
=head2 Installing I<DDS> as a package alias
Its possible to have an alias to Data::Dump::Streamer created and
installed for easier usage in one liners and short scripts.
Data::Dump::Streamer is a bit long to type sometimes. However because this
technically means polluting the root level namespace, and having it listed
on CPAN, I have elected to have the installer not install it by default.
If you wish it to be installed you must explicitly state so when
Build.Pl is run:
perl Build.Pl DDS [Other Module::Build options]
Then a normal './Build test, ./Build install' invocation will install DDS.
Using DDS is identical to Data::Dump::Streamer.
=head2 use-time package aliasing
You can also specify an alias at use-time, then use that alias in the rest
of your program, thus avoiding the permanent (but modest) namespace
pollution of the previous method.
use Data::Dumper::Streamer as => 'DDS';
# or if you prefer
use Data::Dumper::Streamer;
import Data::Dumper::Streamer as => 'DDS';
You can use any alias you like, but that doesn't mean you should.. Folks
doing as => 'DBI' will be mercilessly ridiculed.
=head2 PadWalker support
If PadWalker 1.0 is installed you can use DumpLex() to try to
automatically determine the names of the vars being dumped. As
long as the vars being dumped have my or our declarations in scope
the vars will be correctly named. Padwalker will also be used
instead of the B:: modules when dumping closures when it is available.
=head1 INTERFACE
=head2 Data::Dumper Compatibility
For drop in compatibility with the Dumper() usage of Data::Dumper, you may
request that the L<Dumper()|/Dumper> method is exported. It will not be exported by
default. In addition the standard Data::Dumper::Dumper() may be exported
on request as C<DDumper>. If you provide the tag C<:Dumper> then both will
be exported.
=over 4
=item Dumper
=item Dumper LIST
A synonym for scalar Dump(LIST)->Out for usage compatibility with
L<Data::Dumper|Data::Dumper>
=item DDumper
=item DDumper LIST
A secondary export of the actual L<Data::Dumper::Dumper|Data::Dumper>
subroutine.
=back
=head2 Constructors
=over 4
=item new
Creates a new Data::Dump::Streamer object. Currently takes no
arguments and simply returns the new object with a default style
configuration.
See C<Dump()> for a better way to do things.
=cut
sub _compressor {
return "use Data::Dump::Streamer qw(usqz);\n"
if !@_;
return sqz($_[0], "usqz('", "')" );
}
sub new {
my $class = shift;
my $self = bless {
style => {
hashsep => '=>', # use this to separate key vals
arysep => ',',
pairsep => ',',
optspace => ' ',
bless => 'bless()', # use this to bless objects, needs fixing
compress => 0, # if nonzero use compressor to compress strings
# longer than this value.
compressor => \&_compressor,
indent => 2, # should we indent at all?
indentkeys => 1, # indent keys
declare => 0, # predeclare vars? allows refs to root vars if 0
sortkeys => {},
verbose => 1, # use long names and detailed fill ins
dumpglob => 1, # dump glob contents
deparseglob => 1,
deparse => 1, # deparse code refs?
freezer => 'DDS_freeze', # default freezer
freeze_class => {}, # freeze classes
rle => 1, # run length encode arrays
ignore => {}, # ignore classes
indentcols => 2, # indent this number of cols
ro => 1, # track readonly vars
dualvars => 1, # dump dualvars
eclipsename => "%s_eclipse_%d",
purity => 1, # test
# use this if deparse is 0
codestub => 'sub { Carp::confess "Dumped code stub!" }',
formatstub => 'do{ local *F; eval "format F =\nFormat Stub\n.\n"; *F{FORMAT} }',
# use these opts if deparse is 1
deparseopts => ["-sCi2v'Useless const omitted'"],
special => 0,
# not yet implemented
array_warn => 10_000, # warn if an array has more than this number of elements
array_chop => 32_767, # chop arrays over this size
array_max => 1_000_000, # die if arrays have more than this size
smart_array => 1, # special handling of very large arrays
# with hashes as their 0 index. (pseudo-hash error detection)
},
debug => 0,
cataloged => 0,
ref_id => 0,
sv_id => 0
}, $class;
return $self;
}
sub _safe_self {
my $self = shift;
unless ( ref $self ) {
$self = $self->new();
}
return $self;
}
sub Dumper {
return scalar Dump(@_)->Out();
}
sub DDumper {
return Data::Dumper::Dumper(@_);
}
#sub _is_utf8 { length $_[0] != do { use bytes; length $_[0] } }
BEGIN {
my $numeric_rex=qr/\A-?(?:0|[1-9]\d*)(\.\d+(?<!0))?\z/;
# used by _qquote below
my %esc = (
"\a" => "\\a",
"\b" => "\\b",
"\t" => "\\t",
"\n" => "\\n",
"\f" => "\\f",
"\r" => "\\r",
"\e" => "\\e",
);
# Taken from Data::Dumper::qquote() 2.12.
# Changed utf8 handling from that version
# put a string value in double quotes
# Fixes by [ysth]
sub _qquote {
my $str = shift;
my @ret;
while (length($str)) {
local($_)=substr($str,0,72,"");
s/([\\\"\@\$])/\\$1/g;
unless (/[^ !""\#\$%&''()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~]/) {
push @ret,qq("$_"); # fast exit
next;
}
s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
if ( ord('^') == 94 ) {
# ascii / utf8
# no need for 3 digits in escape if followed by a digit
s/([\0-\037])(?!\d) / sprintf '\\%o', ord($1)/xeg;
s/([\0-\037\177]) / sprintf '\\%03o', ord($1)/xeg;
if (length $_ != do { use bytes; length $_ }) {
use utf8; #perl 5.6.1 needs this, 5.9.2 doesn't. sigh
s/([\200-\377]) / sprintf '\\%03o', ord($1)/xeg;
s/([^\040-\176])/ sprintf '\\x{%x}', ord($1)/xeg;
} else {
# must not be under "use utf8" for 5.6.x
s/([\200-\377]) / sprintf '\\%03o', ord($1)/xeg;
}
} else {
# ebcdic
s{([^ !""\#\$%&''()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])(?!\d)}
{
my $v = ord($1); '\\'.sprintf(($v <= 037 ? '%o' : '%03o'), $v)
}eg;
s{([^ !""\#\$%&''()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])}
{'\\'.sprintf('%03o',ord($1))}eg;
}
push @ret,qq("$_");
}
return join ".\n\t",@ret;
}
# single quote
sub _quote {
my $v = join "", @_;
if ($v=~$numeric_rex) {
return $v;
} elsif ($v!~/[^\x20-\x7E]/) {
$v =~ s/([\\''])/\\$1/g;
return "'$v'";
}
return _qquote($v);
}
# quote a key
sub _quotekey {
my $key = shift;
if (!defined($key) or $key eq '') {
return '""'
} elsif ($key=~$numeric_rex or $key =~ /\A-?[A-Za-z_]\w*\z/) {
return $key
} else {
_qquote($key);
}
}
}
my %ttrans = (
reftype( {} ) => '%',
reftype( [] ) => '@',
reftype( \ 'foo' ) => '$',
reftype( \\'foo' ) => '$', # REF
reftype( sub{} ) => '&',
'' => '$',
);
sub _make_name {
my ( $self, $obj, $indx ) = @_;
#warn Dumper($self->{unames})."'$self->{unames}'
# : @{$self->{unames}||[]} @{[defined $indx ? $indx : '-']}";
my $uname = ( $self->{unames} || [] )->[ $indx || 0 ];
unless ($uname) {
my $name = blessed($_[1])
|| reftype($_[1])
|| (readonly($_[1]) ? "RO" : "VAR");
unless ($self->{style}{verbose}) {
my $n=1;
(my $abr=$name)=~s/(\w)\w*::/$1/g;
$self->{type_abrv}{$name}||=$name;
while ($n<=length($abr) and
$self->{type_abrv}{substr($abr,0,$n)} and
$self->{type_abrv}{substr($abr,0,$n)} ne $name) {
$n++;
}
if ($n<=length($abr)) {
$self->{type_abrv}{substr($abr,0,$n)}=$name;
return '$' .
substr($abr,0,$n) .
( ++$self->{type_ids}{$name} );
}
}
$name =~ s/::/_/g;
($name)=$name=~/(\w+)/; #take the first word;
return '$' . $name . ( ++$self->{type_ids}{$name} );
} elsif ( $uname =~ /^[-*]/ ) {
my $type = reftype( $_[1] ) || '';
$uname =~ s//$ttrans{$type}/;
$uname;
} else {
return '$' . $uname;
}
}
#=item diag
#
#Outputs to STDOUT a list of all values that have been identified of being
#worth of study. For development/debugging purposes only at this point.
#
#=cut
sub diag {
my $self=shift;
my $handle=shift || \*STDOUT;
print $handle "+---+\n";
my $oidx;
foreach my $idx (1..$self->{sv_id}) {
print $handle $self->diag_sv_idx($idx);
}
print "-----\n" if $self->{ref_id} and $self->{sv_id};
foreach my $idx (1..($self->{ref_id}||0)) {
print $handle $self->diag_ref_idx($idx);
}
print $handle "+---+\n";
$self;
}
sub remove_deref {
my $var=shift;
my ($brace,$rest,$sigil);
if ($var=~s/^([\@\%\$])(?=\$)//) {
($sigil,$brace)=($1,$var)
} else {
local $@;
($brace,$rest,$sigil)= Text::Balanced::extract_bracketed( $var, '{q}',qr/[\@\%\$]/ );
}
if ($brace and !$rest) {
$brace=~s/^\{(.*)\}$/$1/;
return wantarray ? ($sigil,$brace) : $brace;
} else {
return;
}
}
my %tname=qw(HASH % ARRAY @ SCALAR $ REF $);
sub _build_name {
my ( $self, $name, $type, $val ) = @_;
$DEBUG>1 and print STDOUT " _build_name( $name '$type' => ";
$type=$tname{$type} if $tname{$type};
if ($type=~/[[{]/) {
$name=~s/[\@\%]\$/\$/;
my ($sigil,$brace)=remove_deref($name);
if ( $name =~ /^([\@\%\$])(\w+)$/ or $sigil
or $name=~/^\*.*\{(?:SCALAR|HASH|ARRAY)\}$/
)
{
$name .= '->' if !($name =~ s/^[\@\%]/\$/)
or $sigil;
$name=~s/^\$(\$.*)->$/\$\{$1\}->/;
}
$DEBUG>1 and print STDOUT "$name => ";
if ( $type eq '[' ) {
$name .= "[$val]";
} elsif ( $type eq '{' ) {
$name .= "{" . _quotekey($val) . "}";
} else {
Carp::confess "Fallen off the end of the world...";
}
} elsif ( $type =~ /^[\@\%\$]$/ ) {
$name = "{$name}"
if $name =~ /[\[\{]/ or $name=~/^\*/;
$name = $type . $name
unless substr( $name, 0, 1 ) eq $type and $type ne '$';
} else {
no warnings; # XXX - why is this here? Yves
Carp::confess "unimplemented _build_name";
}
$DEBUG>1 and print "$name )\n";
$name;
}
sub _reset {
my $self=shift;
foreach my $key (keys %$self) {
next unless $key=~/^(sv|ref|fix|cat|type|names|reqs|cache)/;
delete $self->{$key};
}
$self->{sv_id}=$self->{ref_id}=0;
$self;
}
sub diag_sv_idx {
my $self=shift;
my $idx=shift;
my $prefix=shift||'';
my $oidx=$self->{ref}{$self->{sva}[$idx]};
my $ret=$prefix.
sprintf "S%s%2d : %#x(c%2d|%2d) Dp:%2d %s Du:%s => %s %s %s %s\n",
($self->{special}{$idx} ? '*' : ' '),$idx,
(map { $self->{$_}[$idx] } qw( sva svc svt svd )),
($self->{svro}[$idx] ? 'RO ' : 'RW'),
(!$self->{svdu}[$idx]
? '-'
: defined ${$self->{svdu}[$idx]}
? ${$self->{svdu}[$idx]}
: '?'),
$self->{svn}[$idx],
(defined $self->{unames}[$idx-1] ? "($self->{unames}[$idx-1])" : ""),
(($oidx) ? "< $self->{refn}[$oidx] >" : ""),
($self->{svon}{$idx} ? ": $self->{svon}{$idx}" : "")
;
if ($prefix and $oidx) {
$ret.=$prefix.$self->diag_ref_idx($oidx);
}
$ret;
}
sub diag_ref_idx {
my $self=shift;
my $idx=shift;
my $oidx=$self->{sv}{$self->{refa}[$idx]};
sprintf "R %2d : %#x(c%2d|%2d) Dp:%2d Du:%s => %s %s\n",
$idx,
(map {
defined $self->{$_}[$idx] ? $self->{$_}[$idx] : -1
} qw(refa refc reft refd )),
(!$self->{refdu}[$idx]
? '-'
: defined ${$self->{refdu}[$idx]}
? ${$self->{refdu}[$idx]}
: '?'),
$self->{refn}[$idx],
(($oidx) ? " < $self->{svn}[$oidx] >" : "")
;
}
=item Dump
=item Dump VALUES
Smart non method based constructor.
This routine behaves very differently depending on the context it is
called in and whether arguments are provided.
If called with no arguments it is exactly equivalent to calling
Data::Dump::Streamer->new()
which means it returns an object reference.
If called with arguments and in scalar context it is equivalent to calling
Data::Dump::Streamer->new()->Data(@vals)
except that the actual depth first traversal is I<delayed> until C<Out()>
is called. This means that options that must be provided before the
C<Data()> phase can be provided after the call to C<Dump()>. Again, it
returns a object reference.
If called with arguments and in void or list context it is equivelent to
calling
Data::Dump::Streamer->new()->Data(@vals)->Out()
The reason this is true in list context is to make
C<print Dump(...),"\n";> do the right thing. And also that combined with
method chaining options can be added or removed as required quite easily
and naturally.
So to put it short:
my $obj=Dump($x,$y); # Returns an object
my $str=Dump($x,$y)->Out(); # Returns a string of the dump.
my @code=Dump($x,$y); # Returns a list of the dump.
Dump($x,$y); # prints the dump.
print Dump($x,$y); # prints the dump.
It should be noted that the setting of C<$\> will affect the behaviour of
both of
Dump($x,$y);
print Dump($x,$y);
but it will not affect the behaviour of
print scalar Dump($x,$y);
B<Note> As of 1.11 Dump also works as a method, with identical properties
as when called as a subroutine, with the exception that when called with
no arguments it is a synonym for C<Out()>. Thus
$obj->Dump($foo)->Names('foo')->Out();
will work fine, as will the odd looking:
$obj->Dump($foo)->Names('foo')->Dump();
which are both the same as
$obj->Names('foo')->Data($foo)->Out();
Hopefully this should make method use more or less DWIM.
=cut
my %args_insideout;
sub DESTROY {
my $self=shift;
delete $args_insideout{Data::Dump::Streamer::refaddr $self} if $self;
}
sub Dump {
my $obj;
if ( blessed($_[0]) and blessed($_[0]) eq __PACKAGE__ ) {
$obj=shift;
}
if (@_) {
if ( defined wantarray and !wantarray ) {
$obj ||= __PACKAGE__->new();
$obj->_make_args(@_);
return $obj;
} else {
$obj||=__PACKAGE__;
return $obj->Data(@_)->Out();
}
} else {
if ($obj) {
return $obj->Out();
} else {
return __PACKAGE__->new();
}
}
}
=item DumpLex VALUES
DumpLex is similar to Dump except it will try to automatically determine
the names to use for the variables being dumped by using PadWalker to
have a poke around the calling lexical scope to see what is declared. If
a name for a var can't be found then it will be named according to the
normal scheme. When PadWalker isn't installed this is just a wrapper for
L<Dump()|Dump>.
Thanks to Ovid for the idea of this. See L<Data::Dumper::Simple> for a
similar wrapper around L<Data::Dumper>.
=cut
sub DumpLex {
if ( ! $HasPadWalker ) {
#warn( "Can't use DumpLex without ".
# "PadWalker v1.0 or later installed.");
goto &Dump;
}
my $obj;
if ( blessed($_[0]) and blessed($_[0]) eq __PACKAGE__ ) {
$obj=shift;
}
my @names;
# = map {
# PadWalker::var_name(1,\$_)
# || PadWalker::var_name(1,\$_)
# (ref $_ && PadWalker::var_name(1,$_));
# $str
# } @_;
#if ( !@names && @_ ) {
my %pad_vars;
foreach my $pad ( PadWalker::peek_my(1),
PadWalker::peek_our(1)
){
while (my ($var,$ref) = each %$pad) {
$pad_vars{ refaddr $ref } ||= $var;
}
}
foreach (@_) {
my $name;
INNER:foreach ( \$_, $_ ) {
$name=$pad_vars{refaddr $_}
and last INNER;
}
push @names, $name;
}
if ( defined wantarray and !wantarray ) {
$obj ||= __PACKAGE__->new();
$obj->_make_args(@_);
$obj->Names(@names);
return $obj;
} else {
$obj||=__PACKAGE__;
return $obj->Names(@names)->Data(@_)->Out();
}
}
=item DumpVars PAIRS
This is wrapper around L<Dump()|Dump> which expect to receive
a list of name=>value pairs instead of a list of values.
Otherwise behaves like L<Dump()|Dump>. Note that names starting
with a '-' are treated the same as those starting with '*' when
passed to L<Names()|Names>.
=cut
sub DumpVars {
my $obj;
if ( blessed($_[0]) and blessed($_[0]) eq __PACKAGE__ ) {
$obj=shift;
}
if (@_ % 2) {
warnings::warnif "Odd number of arguments in DumpVars";
pop @_;
}
my @names;
my @args;
for ( 0 .. $#_/2 ) {
$names[$_]=$_[$_*2];
$args[$_]=$_*2+1;
}
#die "@_:@names|@args";
if ( defined wantarray and !wantarray ) {
$obj ||= __PACKAGE__->new();
$obj->_make_args(@_[@args]);
$obj->Names(@names);
return $obj;
} else {
$obj||=__PACKAGE__;
return $obj->Data(@_[@args])->Names(@names)->Out();
}
}
sub _reg_ref {
my ($self,$item,$depth,$name,$cnt,$arg)=@_;
warn "_ref_ref($depth,$name,$cnt)\n" if $DEBUG;
my $addr=refaddr $item;
$arg->{raddr}=$addr if $arg;
my $idx;
unless ($idx=$self->{ref}{$addr}) {
$idx=$self->{ref}{$addr}=++$self->{ref_id};
$arg->{ridx}=$idx if $arg;
$self->{refn}[$idx]=$name;
$self->{refd}[$idx]=$depth;
$self->{refa}[$idx]=$addr;
$self->{refc}[$idx]=$cnt;
return wantarray ? ($idx,0) : $idx
}
$self->{reft}[$idx]++;
$arg->{ridx}=$idx if $arg;
return wantarray ? ($idx,1) : undef;
}
sub _reg_scalar {
my ($self,$item,$depth,$cnt,$ro,$name,$arg)=@_;
Carp::cluck $name if $name=~/^\$\*/;
my $addr=refaddr \$_[1];
my $idx;
$arg->{addr}=$addr if $arg;
unless ($idx=$self->{sv}{$addr}) {
$idx=$self->{sv}{$addr}=++$self->{sv_id};
$self->{svd}[$idx]=$depth;
$self->{sva}[$idx]=$addr;
$self->{svro}[$idx]=$ro;
$self->{svc}[$idx]=$cnt;
$self->{svw}{$addr}=!0
if isweak($_[1]);
($self->{svn}[$idx]=$name)=~s/^[\@\%\&]/\$/;
if ($self->{svn}[$idx] ne $name) {
$self->{svn}[$idx].="_"; #XXX
#warn "$self->{svn}[$idx] ne $name"
$self->{svon}{$idx}=$name;
}
} else{
if ($DEBUG>9) {
print $self->diag_sv_idx($idx);
print "$name is already registered as $self->{svn}[$idx] ".
"Depth ($self->{svd}[$idx]) $depth\n";
}
if ($self->{svn}[$idx]=~/^\$\{?\$/ and $name!~/^\$\{?\$/) {
$self->{svn}[$idx]=$name;
}
}
$self->{svt}[$idx]++;
$arg->{idx}=$idx if $arg;
Carp::confess "Dupe name!" if $self->{svrt}{$name};
$self->{svrt}{$name}=$idx;
return $name;
}
*Precise=\&Dump;
# we make an array of hashes containing useful info about the arguments
sub _make_args {
my $self=shift;
$args_insideout{refaddr $self}= [
map {
{
item => \$_[$_],
ro => readonly($_[$_]),
refcnt => sv_refcount($_[$_]),
}
} 0..$#_
];
return $args_insideout{refaddr $self}
}
=back
=head2 Methods
=over 4
=item Data
=item Data LIST
Analyzes a list of variables in breadth first order.
If called with arguments then the internal object state is reset before
scanning the list of arguments provided.
If called with no arguments then whatever arguments were provided to C<Dump()>
will be scanned.
Returns $self.
=cut
sub _add_queue {
my ($self,$queue,$type,$item,$depth,$name,$rcount,$arg)=@_;
if (substr($type,0,1) ne '*') {
push @$queue,[\$item,$depth,$name,$rcount,$arg];
} elsif($self->{style}{dumpglob}) {
local @_;
foreach my $t ($self->_glob_slots('FORMAT')) {
#warn $type.":$t\n";
#register?
#$self->_reg_scalar(*$item{$t},$depth+1,sv_refcount(*$item{$t}),
# readonly(*$item{$t}),'*'.$name."{$t}");
my $v=*$item{$t};
next unless defined $v;
next if $t eq 'SCALAR' and !defined($$v);
push @$queue,[
\*$item{$t},
$depth+1,
$type."{$t}",
refcount(\*$item{$t})
];
}
}
#use Scalar::Util qw(weaken);
$self;
}
sub Data {
my $self=shift->_safe_self;
my $args;
print "Data(".scalar(@_)." vars)\n"
if $DEBUG;
if (@_) {
$self->_reset;
$self->_make_args(@_);
} elsif ( $self->{cataloged} ) {
$self->_reset;
}
$args= $args_insideout{refaddr $self}
|| Carp::carp "No arguments!";
my $pass=1;
PASS:{
my @queue;
my $idx=0;
foreach my $arg (@$args) {
#($self,$item,$depth,$cnt,$ro,$name)
my $make_name=$self->_make_name(${ $arg->{item} },$idx++);
my $name=$self->_reg_scalar(
${ $arg->{item} },
1,
$arg->{refcnt},
$arg->{ro},
$make_name,
$arg
);
$arg->{name}=$name;
if (my $type=reftype_or_glob ${ $arg->{item} }) {
$self->_add_queue(\@queue, $type, ${ $arg->{item} }, 2,
$name, refcount ${ $arg->{item} },$arg)
}
}
my %lex_addr;
my %lex_addr2name;
my %lex_name;
my %lex_special;
while (@queue) {
# If the scalar (container) is of any interest it is
# already registered by the time we see it here.
# at this point we only care about the contents, not the
# container.
print Data::Dumper->new([\@queue],['*queue'])->Maxdepth(3)->Dump
if $DEBUG>=10;
my ($ritem,
$cdepth,
$cname,
$rcnt,
$arg)=@{shift @queue};
my ($frozen,$item,$raddr,$class);
DEQUEUE:{
$item=$$ritem;
$raddr=refaddr($item);
$class=blessed($item);
if ($self->{ref_fz}{$raddr}) {
print "Skipping frozen element $raddr\n" if $DEBUG;
next;
}
$DEBUG and
print "Q-> $item $cdepth $cname $rcnt ($raddr)\n";
unless ($raddr) {
$DEBUG and
print " Skipping '$cname' as it isn't a reference.\n";
next;
}
last DEQUEUE if $frozen;
$frozen=1;
if ($self->{style}{ignore}{"#$raddr"} || ($class&& $self->{style}{ignore}{".$class"})) {
$DEBUG and
print "Ignoring '$cname' as its class ($class) in ".
"our ignore list.\n";
next;
} elsif ($class && !$self->{"cache_skip_freeze"}{$class}) {
my $freezer= $self->{cache_freeze_class}{$class};
my ( $proxy, $thaw, $postop );
if (! defined $freezer ) {
for ( $self->{style}{freeze_class}{$class},
$self->{style}{freezer},
'DDS_freeze' )
{
$freezer= $_;
if ( $freezer ) {
if (ref $freezer) {
eval {
($proxy,$thaw,$postop)= $freezer->($$ritem);
};
last if !$@;
} elsif ( $class->can($freezer) ) {
eval {
($proxy,$thaw,$postop)= ${$ritem}->$freezer();
};
last if !$@;
}
} elsif ( defined $freezer ) {
last;
}
}
if (! defined $proxy) {
$self->{"cache_skip_freeze"}{$class}=1;
} else {
$self->{cache_freeze_class}{$class}= $freezer;
}
} elsif (ref $freezer) {
($proxy,$thaw)= $freezer->($$ritem);
} else {
($proxy,$thaw)= ${$ritem}->$freezer();
}
if ( $thaw ) {
$self->{ref_thaw}{$raddr}= $thaw;
}
if ( $postop ) {
$self->{ref_postop}{$raddr}= $postop;
}
if ( refaddr($proxy) != $raddr ) {
$self->{ref_fz}{$raddr}= $proxy;
$ritem= \$proxy;
if (ref $proxy) {
redo DEQUEUE;
} else {
next;
}
}
}
}
my ($idx,$dupe)=$self->_reg_ref($item,$cdepth,$cname,$rcnt,$arg);
$DEBUG and print " Skipping '$cname' as it is a dupe of ".
"$self->{refn}[$idx]\n"
if $dupe;
$DEBUG>9 and $self->diag;
next if $dupe;
my $reftype=reftype $item;
my $cnt=refcount($item);
my $overloaded=undef;
my $isoverloaded=0;
if (defined $class and overload::Overloaded($item)) {
disable_overloading( $item );
$overloaded= $class;
$isoverloaded= 1;
}
if ( $reftype eq 'SCALAR' or
$reftype eq 'REF' or
$reftype eq 'GLOB' )
{
my $name=$self->_build_name($cname,'$');
my $cnt=sv_refcount($$item);
if ($cnt>1) {
$self->_reg_scalar($$item,$cdepth+1,$cnt,
readonly($$item),$name);
}
if (my $type=reftype_or_glob $$item) {
$self->_add_queue(\@queue,$type,$$item,
$cdepth+2,$name,$cnt)
}
} elsif ($reftype eq 'ARRAY') {
foreach my $idx (0..$#$item) {
my $name=$self->_build_name($cname,'[',$idx);
my $cnt=sv_refcount($item->[$idx]);
if ($cnt>1) {
print "refcount($name)==$cnt\n"
if $DEBUG>9;
$self->_reg_scalar($item->[$idx],$cdepth+1,$cnt,
readonly($item->[$idx]),$name);
}
if (my $type=reftype_or_glob $item->[$idx]) {
$self->_add_queue(\@queue,$type,$item->[$idx],
$cdepth+2,$name,$cnt)
}
}
} elsif ($reftype eq 'HASH') {
my $ik=$self->{style}{indentkeys};
my ($keyary, $thaw)= $self->_get_keys($item,0,$raddr,$class);
if ($thaw) {
$self->{ref_thaw}{$raddr}= $thaw;
}
my $key_len=0;
my $key_sum=0;
my $key_count=0;
die reftype $keyary if $keyary && reftype($keyary) ne 'ARRAY';
while ( defined( my $key =
defined $keyary ? $keyary->[$key_count] : each %$item
))
{
if ($ik) {
my $qk=_quotekey($key);
$key_sum+=length($qk);
$key_len=length($qk) if $key_len<length($qk);
}
$key_count++;
my $name=$self->_build_name($cname,'{',$key);
my $cnt=sv_refcount($item->{$key});
if ($cnt>1) {
$self->_reg_scalar($item->{$key},$cdepth+1,$cnt,
readonly($item->{$key}),$name);
}
if (my $type=reftype_or_glob $item->{$key}) {
$self->_add_queue(\@queue,$type,$item->{$key},
$cdepth+2,$name,$cnt);
}
}
my $avg=$key_count>0 ? $key_sum/$key_count : 0;
$self->{ref_hklen}{$raddr}=($key_len>8 && (2/3*$key_len)>$avg)
? int(0.5+$avg) : $key_len;
$self->{ref_hkcnt}{$raddr}=$key_count;
#warn "$raddr => $key_count";
} elsif ($reftype eq 'CODE') {
if ($pass == 1) {
my $used=_get_lexicals($item);
foreach my $name (keys %$used) {
next unless $name=~/\D/;
my $addr=refaddr($used->{$name});
if ( !$lex_addr{$addr} ) {
$lex_addr{$addr}=$used->{$name};
if ( $lex_name{$name} ) {
my $tmpname=sprintf "%s".$self->{style}{eclipsename},
substr($name,0,1),
$self->{style}{eclipsename}=~/^[^%]*%s/
? ( substr($name,1),
++$lex_special{$name}, )
: ( ++$lex_special{$name},
substr($name,1), );
$lex_name{$tmpname}=$addr;
$lex_addr2name{$addr}=$tmpname;
$self->_add_queue(\@queue,reftype_or_glob $used->{$name},
$used->{$name},$cdepth+1,$tmpname,2);
} else {
$lex_name{$name}=$addr;
$lex_addr2name{$addr}=$name;
$self->_add_queue(\@queue,reftype_or_glob $used->{$name},
$used->{$name},$cdepth+1,$name,2);
}
}
}
}
} elsif ($reftype eq 'FORMAT') {
# Code similar to that of CODE should go here I think.
} else {
# IO?
Carp::confess "Data() can't handle '$reftype' objects yet ($item)\n :-(\n"
if $ENV{DDS_STRICT};
}
if ($isoverloaded) {
restore_overloading( $item, $overloaded );
}
}
if ( $pass++ == 1 ) {
my %items;
for my $idx ( 0..$#{$args_insideout{refaddr $self}} ) {
my $item=$args_insideout{refaddr $self}[$idx];
$items{ refaddr $item->{item} } = $idx;
}
my @add;
my $added=0;
if (0) {
@add=keys %lex_addr;
} else {
for my $addr (keys %lex_addr) {
if ( exists $items{$addr} ) {
my $idx = $items{$addr};
if ( !$self->{unames}[$idx] ){
for ($self->{unames}[$idx] = $lex_addr2name{$addr}) {
s/^[^\$]/*/; s/^\$//;
}
$added++;
} else {
my $new=$self->{unames}[$idx];
my $old=$lex_addr2name{$addr};
$new=~s/^(\*)?/substr($old,0,1)/e;
delete $lex_name{$lex_addr2name{$addr}};
$lex_addr2name{$addr}=$new;
$lex_name{$self->{unames}[$idx]} = $addr; # xxx
}
} else {
push @add,$addr;
}
}
}
@add=sort {$lex_addr2name{$a} cmp $lex_addr2name{$b}} @add;
$self->{lexicals}={
a2n => \%lex_addr2name,
name => \%lex_name
};
if (@add) {
unshift @{$args_insideout{refaddr $self}},
map {
my $rt=reftype($lex_addr{$_});
my $item;
if ($rt ne 'SCALAR' and $rt ne 'GLOB' and $rt ne 'REF') {
$item=\$lex_addr{$_};
} else {
$item=$lex_addr{$_};
}
{
item => $item,
usemy => 1,
ro => 0,
refcnt => refcount($lex_addr{$_}),
}
} @add;
$self->{lexicals}{added}={ map { $lex_addr2name{$_} => 1 } @add };
unshift @{$self->{unames}},
map {
(my $n=$lex_addr2name{$_})=~s/^[^\$]/*/;
$n=~s/^\$//;
$n
} @add;
$self->_reset;
redo PASS;
} elsif ($added) {
$self->_reset;
redo PASS;
}
}
}
$self->{cataloged}=1;
return $self;
}
sub _add_fix {
my ($self,@args)=@_;
# 'var','glob','method call','lock','ref','sv','#'
# TODO
# add a fix statement to the list of fixes.
my $fix=@args==1 ? shift @args : [@args];
unless ($fix->[0]=~/^(var|glob|thaw|ref|sv|#|sub call|lock|bless)$/) {
Carp::confess "Unknown variant:".Dumper($fix);
}
if ($args[0] eq 'var') {
unshift @{$self->{fix}},$fix;
} else {
push @{$self->{fix}},$fix;
}
}
sub _glob_slots {
my ($self,$inc_format)=@_;
# $inc_format is for a special case.
return (
qw(SCALAR HASH ARRAY),
(($self->{style}{deparse} && $self->{style}{deparseglob})
? 'CODE' : ()),
(($inc_format && $self->{style}{deparse} && $self->{style}{deparseglob})
? 'FORMAT' : () )
);
}
sub _dump_apply_fix { #handle fix statements and GLOB's here.
my ($self,$isfinal)=@_;
# go through the fix statements and out any that are
# now fully dumped.
# currently the following types are grokked:
# 'var','glob','method call','tlock','ref','sv','#'
my @globs;
GLOB:{
@globs=();
@{$self->{fix}}=grep {
my $keep=1;
my $fix=$_;
if (ref $fix) {
my ($type,$lhs,$rhs,$class)=@$fix;
if ($type eq '#') {
$self->{fh}->print(map "# $_\n",@$fix[0..$#$fix]);
$keep=0;
} elsif ($type eq 'bless') {
if ($isfinal) { # $self->{"refdu"}[$lhs]
$lhs=$self->{"refn"}[$lhs];
$self->{fh}->print(
substr($self->{style}{bless},0,-1)," ",$lhs,", ",
_quote($rhs)," ",substr($self->{style}{bless},-1),
";\n");
$keep=0;
}
} elsif ($type eq 'sv') {
my $dref=$_->[-1];
if ($self->{$type."du"}[$rhs] and ${$self->{$type."du"}[$rhs]}) {
$rhs=$self->{$type."n"}[$rhs];
my ($sigil,$var)=remove_deref($lhs);
if ($sigil) {
$rhs="\\".$rhs;
$lhs=$var;
}
$self->{fh}->print("$lhs = $rhs;\n");
$$dref=1 if ref $dref;
$keep=0
}
} elsif ($type eq 'ref') {
if ($self->{$type."du"}[$rhs] and ${$self->{$type."du"}[$rhs]}) {
$rhs=$self->{$type."n"}[$rhs];
if ($rhs=~/^[\@\%\&]/) {
$rhs="\\".$rhs;
$rhs="bless( $rhs, "._quote($class).' )'
if $class;
} # Warn if
$self->{fh}->print("$lhs = $rhs;\n");
$keep=0
}
} elsif ($type eq 'lock') {
if ($self->{refdu}[$lhs] and ${$self->{"refdu"}[$lhs]}) {
$lhs=$self->{"refn"}[$lhs];
$self->{fh}->print(@$rhs ? "lock_keys_plus( $lhs, "
: "lock_keys( $lhs ",
join(", ",map{ _quote($_) } @$rhs),
");\n");
$keep=0;
}
} elsif ($type eq 'thaw') {
# these have to happen at the end.
if ($isfinal) {
#if ($self->{refdu}[$lhs] and ${$self->{"refdu"}[$lhs]}) {
${$self->{refdu}[$lhs]}=1;
$lhs=$self->{"refn"}[$lhs];
my @args=@$_[3..$#$_];
if ($rhs=~/^(->)?((?:\w*::)*\w+)(\(\))?$/) {
if ($3) {
$self->{fh}->print("$2( ".join(", ",$lhs,@args)." );\n");
} else {
$self->{fh}->print("$lhs->$2(".join(", ",@args).");\n");
}
} else {
$rhs=~s/^\t//mg;
$self->{fh}->print("for ($lhs) {\n$rhs\n}\n");
}
$keep=0;
}
} elsif ($type eq 'glob') {
push @globs,$_;
$keep=0;
} elsif ($type eq 'var') {
$rhs="\\".$rhs;
$rhs="bless( $rhs, "._quote($class).' )'
if $class;
$self->{fh}->print(($self->{style}{declare} ? 'my ' : ""),"$lhs = $rhs;\n");
$keep=0;
} elsif ($type eq 'sub call') {
my @r=grep { ref $_ and (!$self->{svdu}[$$_] or !${$self->{svdu}[$$_]}) } @$fix;
unless (@r) {
my ($type,$sub,@args)=map { ref $_ ? $self->{svn}[$$_] : $_ } @$fix;
$self->{fh}->print("$sub(",join(", ",@args),");\n");
$keep=0;
}
} else {
die "Bad fix: ",Dumper($fix);
}
}
$keep;
} @{$self->{fix}};
foreach my $glob (@globs) {
my ($type,$lhs,$rhs,$depth,$name)=@$glob;
print "Symbol: $name\n" if $DEBUG and $name;
local @_;
$name=$name ? '*'.$name : $rhs;
my $overloaded=undef;
my $isoverloaded=0;
if (defined( blessed $lhs ) and
overload::Overloaded( $lhs ) )
{
$overloaded=blessed $lhs;
disable_overloading( $lhs );
$isoverloaded=1;
}
foreach my $t ($self->_glob_slots(''))
{
my $v=*$lhs{$t};
if ( not(defined $v) or
($t eq 'SCALAR' and !defined($$v)))
{
next;
}
my $dumped=0;
my $gaddr=refaddr(*$lhs{$t});
my $gidx=$self->{ref}{$gaddr};
unless ($gidx) {
next
} elsif ($self->{refd}[$gidx]<$depth+1) {
$self->_add_fix('ref',$name,$gidx,blessed(*$lhs{$t}));
next;
}
$self->{fh}->print("$name = ");
my $ret=$self->_dump_sv(*$lhs{$t},$depth,\$dumped,$name,length($name)+3);
Carp::confess "\nUnhandled alias value '$ret' returned to _dump_apply_fix()!"
if $ret;
$self->{fh}->print(";\n");
$dumped=1;
}
if ($self->{style}{deparse} && $self->{style}{deparseglob}
#and defined *$lhs{FORMAT}
) {
# from link from [ysth]: http://groups.google.com/groups?selm=laUs8gzkgOlT092yn%40efn.org
# translate arg (or reference to it) into a B::* object
# To work-around perl commit
# 2acc3314e31a9342e325f35c5b592967c9850c9b, keep the
# value \*$lhs alive while we inspect it as a B object
# or else it'll be reaped while we're using it.
my $lhs_glob = \*$lhs;
my $Bobj = B::svref_2object($lhs_glob);
# if passed a glob or globref, get the format
$Bobj = B::GV::FORM($Bobj) if ref $Bobj eq 'B::GV';
if (ref $Bobj eq 'B::FM') {
(my $cleaned=$name)=~s/^\*(::)?//;
$self->{fh}->print("format $cleaned =\n");
my $deparser = Data::Dump::Streamer::Deparser->new();
$self->{fh}->print(
$deparser->indent($deparser->deparse_format($Bobj))
);
$self->{fh}->print("\n");
}
}
if ($isoverloaded) {
restore_overloading( $lhs, $overloaded );
}
}
redo GLOB if @globs;
}
}
=item Out
=item Out VALUES
Prints out a set of values to the appropriate location. If provided a list
of values then the values are first scanned with C<Data()> and then
printed, if called with no values then whatever was scanned last with
C<Data()> or C<Dump()> is printed.
If the C<To()> attribute was provided then will dump to whatever object
was specified there (any object, including filehandles that accept the
print() method), and will always return $self.
If the C<To()> attribute was not provided then will use an internal
printing object, returning either a list or scalar or printing to STDOUT
in void context.
This routine is virtually always called without arguments as the last
method in the method chain.
Dump->Arguments(1)->Out(@vars);
$obj->Data(@vars)->Out();
Dump(@vars)->Out;
Data::Dump::Streamer->Out(@vars);
All should DWIM.
=cut
#
# Out is just a wrapper. The overall dumping process works like this:
#
# Out
# foreach root value
# _dump_sv
# _dump_rv if ref
# (optionally one of)
# _dump_array
# _dump_hash
# _dump_code
# _dump_qr
# _dump_apply_fix
# (which may call)
# _dump_sv
#
# _dump_array, _dump_hash, _dump_rv if needed may also call _dump_sv
#
# essentially _dump_sv and _dump_rv handle uniqueness checks for scalars,
# and refs. _dump_sv handles the SV's containers and _dump_rv
# handles the things that the SV contains a reference to. _dump_sv also
# handles simple values and globs, and works with _dump_rv to handle
# references to scalars correctly. If "fix" statements are required
# to complete the definition of the structure (self referential structures)
# then _add_fix adds them to the list, and _dump_apply_fix pulls them off.
# note that _dump_apply_fix can also call _dump_sv if needed (to handle globs),
# and will also emit fix statements as early as possible. no require/use
# logic is currently in place. its the evalers responsibility to use the mod
# w/the right tags for now...
sub Out {
local($\,$",$,)=("","",""); # prevent globals from messing with our output via print
my $self = shift->_safe_self;
print "Out(".scalar(@_)." vars)\n"
if $DEBUG;
if ( !$self->{in_printit} and (@_ or !$self->{cataloged} )) {
$self->Data(@_);
}
my $fh;
unless ( $self->{fh} ) {
print " no filehandle using "
if $DEBUG;
if (defined wantarray) {
my $class= __PACKAGE__ . (wantarray ? "::_::ListPrinter" : "::_::StringPrinter");
print $class,"\n"
if $DEBUG;
$fh = $class->new()
or Carp::confess "$class failed to build!";
$self->{'return'} = $fh;
} else {
print "STDOUT\n" if $DEBUG;
$fh = \*STDOUT;
}
$self->{fh} = $fh;
}
# loop over the list
# and dump out each one in turn
# handling any potential fixes after
# each definition is complete
$self->{out_names}=[];
$self->{declare}=[];
$self->{special}={};
$DEBUG>9 and $self->diag;
my @items=@{$args_insideout{refaddr $self}};
my $namestr="";
push @{$self->{out_names}},map{$_->{name}}@items; #must
push @{$self->{declare}},map{$_->{name}}@items;
if ($self->{style}{special}) {
warn DDumper(\@items) if $DEBUG;
$namestr="# (".join (", ",@{$self->{out_names}}).")\n";
@items=sort { $self->{svc}[$b->{idx}] <=> $self->{svc}[$a->{idx}]||
($b->{raddr} ? $self->{refc}[$b->{ridx}] : 0)
<=>
($a->{raddr} ? $self->{refc}[$a->{ridx}] : 0)
} @items;
warn DDumper(\@items) if $DEBUG;
}
if ($self->{style}{compress} && $self->{style}{compressor}) {
my $prelude=$self->{style}{compressor}->();
$self->{fh}->print($prelude) if $prelude;
}
$self->{fh}->print("my (",join(",",sort keys %{$self->{lexicals}{added}}),");\n")
if $self->{lexicals}{added};
foreach my $item (@items) {
my $dumped=0;
my $ret=$self->_dump_sv(${$item->{item}},1,\$dumped,$item->{name});
Carp::confess "\nUnhandled alias value '$ret' returned to Out()!"
if $ret;
$self->{fh}->print(";\n");
$dumped=1;
$self->_dump_apply_fix();
}
$self->_dump_apply_fix('final');
$self->{fh}->print($namestr) if $namestr;
$self->diag if $DEBUG;
#warn "@{$self->{out_names}}";
if ( $self->{return} and defined wantarray) {
my $r = delete $self->{return};
delete $self->{fh};
return $r->value;
} else {
return $self;
}
}
sub print_token {
my ($self, $str)=@_;
$self->{fh}->print($str);
}
sub print_quoted {
my ( $self, $str )=@_;
$self->{fh}->print($str);
}
# sqz(str,begin,end)
sub sqz {
require Compress::Zlib;
require MIME::Base64;
my $res= Compress::Zlib::compress($_[0],9);
return $_[1]
? $_[1]
. MIME::Base64::encode($res,"")
. $_[2]
: MIME::Base64::encode($res,"");
}
# usqz(str)
sub usqz {
return Compress::Zlib::uncompress(
MIME::Base64::decode($_[0])
);
}
sub _dump_sv {
my ($self,$item,$depth,$dumped,$name,$indent,$is_ref)=@_;
$self->{do_nl}=0;
my $addr=refaddr(\$_[1]);
my $idx=$self->{sv}{$addr};
my $ro;
$DEBUG and printf "_dump_sv %d %s %#x - %d\n",$depth, $name,$addr,$idx||0;
$name||=$self->{svn}[$idx];
(my $clean_name=$name)=~s/^[\@\%\&](\w+)/\$${1}_/; # XXX
my $optspace=$self->{style}{optspace};
if ($idx) {
# Its a monitored scalar.
my $pre_dumped=$self->{svdu}[$idx];
my $name_diff=(
$self->{svd}[$idx]==$depth
and $self->{svn}[$idx] ne $clean_name
and $clean_name!~/\*/
and $name!~/^\&/
);
#print "Idx: $idx Special keys:",join("-",keys %{$self->{special}}),"\n"
# if $DEBUG and keys %{$self->{special}};
print "sv_dump Monitored:\n",$self->diag_sv_idx($idx," ") if $DEBUG;
if (( $pre_dumped and !$self->{svon}{$idx})
or (!$self->{svon}{$idx} ? ($self->{svd}[$idx]<$depth or $name_diff) : undef) )
{
print "PREDUMPED: $self->{svon}{$idx}\n"
if $DEBUG and $self->{svon}{$idx} and $pre_dumped and $$pre_dumped;
# We've seen it before.
# Unless its a ref it must be an alias
print(($name_diff ? "Name diff" : "No name diff"), " $name, $clean_name","\n")
if $DEBUG;
my ($str,$ret)=('',undef);
if ($is_ref) {
if ($self->{svd}[$idx]==1 && !$self->{style}{declare}
|| ($pre_dumped && $$pre_dumped)
) {
$str="\\$self->{svn}[$idx]";
} else {
#see the 'Many refs' tests in t\dump.t for
#why this is here. basically we need to
#ensure the ref is modifiable. If its two $'s
#then its modifiable anyway, more and it wont be.
# $ref=\\$x; $ref=RW $$ref=RO $$$ref=$x=RW
unless ($self->{style}{purity}) {
$str="\\$self->{svn}[$idx]";
} else {
my $need_do=($name=~/^\$\$\$+/);
if ($need_do) {
$str.=join($optspace,qw( do { my $f = ),'');
}
$str.=!$self->{style}{verbose}
? "'R'" : _quote($DEBUG ? 'SR: ' : 'R: ',
"$self->{svn}[$idx]");
$ret=\do{my $nope=0};
$self->_add_fix('sv',$name,$idx,$ret);
$str.="$optspace}" if ($need_do)
}
}
} else {
if ($depth==1) {
if ($self->{style}{declare}) {
$str.="my $name;\n";
}
#push @{$self->{out_names}},$name;
#push @{$self->{declare}},$name;
$str.="alias_ref(\\$name,\\$self->{svn}[$idx])";
} elsif ($self->{style}{purity}) {
$str.=!$self->{style}{verbose} ? "'A'" : _quote("A: ",$self->{svn}[$idx]);
$ret=\$idx;
} else {
$str.="alias_to($self->{svn}[$idx])";
$ret='';
}
}
$self->{buf}+=length($str);
$self->{buf}=length($1) if $str=~/\n([^\n]*)\s*\z/;
$self->{fh}->print($str);
return $ret ? $ret : ()
} else {
# we've never seen it before and we need to dump it.
$self->{svdu}[$idx]||=$dumped;
print "Defining Special:".$self->diag_sv_idx($idx)
if $DEBUG and $self->{special}{$idx};
$self->{svn}[$idx]=$name if $self->{special}{$idx};
$self->{svd}[$idx]=$depth if $self->{special}{$idx};
}
$ro=$self->{svro}[$idx];
} else {
$ro=readonly $_[1] unless defined $ro;
}
print "sv_dump: Postindexed\n" if $DEBUG;
if ($depth==1) {
# root level object. declare it
if ($name ne $clean_name and $name!~/^\*/ and $self->{svc}[$idx]>1) {
print "Special $name\n" if $DEBUG;
my $oidx=$self->{ref}{$self->{sva}[$idx]};
if ($oidx) {
#theres a ref to us out there
my $name=$self->_build_name($self->{refn}[$oidx],'$');
$self->{svn}[$idx]=$name;
print "Oindex! $oidx $name\n" if $DEBUG;
#$self->{svd}[$idx]=$self->{refd}[$idx]+1;
}
#$self->{special}{$idx}++;
$self->{svdu}[$idx]=undef;
print $self->diag_sv_idx($idx,1) if $DEBUG;
}
#push @{$self->{out_names}},$name; #must
#push @{$self->{declare}},$name;
unless ($name=~/^\&/) { # XXX
my $str=(($self->{style}{declare} && $name!~/^\*/
&& !$self->{lexicals}{added}{$name}
) ? "my$optspace" : ""
)."$name$optspace=$optspace";
$self->{fh}->print($str);
$indent=length($str);
$self->{buf}=0;
} else {
$indent=0;
}
print "toplevel\n" if $DEBUG;
}
my $iaddr=refaddr $item;
$self->{fh}->print("\\")
if $is_ref;
my $glob=globname $item;
my $add_do=$self->{style}{purity} && !$ro && $is_ref
&& {''=>1,SCALAR=>1,REF=>0}->{reftype($_[1])}
&& !blessed($_[1]) && !$glob;
if ($add_do) {
#warn "\n!$ro && $is_ref && !blessed($_[1]) && !$glob";
$self->{fh}->print(join $optspace,qw(do { my $v = ),'');
$self->{buf}+=13;
}
unless ($iaddr) {
print "iaddr $glob\n" if $DEBUG;
unless (defined $item) {
$self->{fh}->print('undef');
$self->{buf}+=5;
} else {
my $is_ro=($self->{style}{ro} && $ro && !$is_ref);
if ($is_ro and !$self->{style}{purity}) {
$self->{fh}->print("make_ro($optspace");
}
if ($glob) {
if ($glob=~/^\*Symbol::GEN/) {
$self->_dump_symbol($_[1],$name,$glob,'deref',$depth);
} else
{
$self->{buf}+=length($glob);
$self->{fh}->print($glob);
if ($self->{style}{dumpglob} and
!$self->{sv_glob_du}{$glob}++) {
$self->_add_fix('glob',$_[1],$glob,$depth+1);
}
}
} else {
my $quoted;
if ($self->{style}{dualvars}) {
no warnings 'numeric'; # XXX: is this required?
if (_could_be_dualvar($item) && 0+$item ne $item && "$item" != $item ) {
$quoted="dualvar( ".join(",$optspace",0+$item,_quote("$item"))."$optspace)";
}
}
# XXX main scalar output here!
if ( ! $quoted ) {
my $style= $self->{style};
if ( $style->{compress} &&
$style->{compressor} &&
length($_[1]) > $style->{compress}
){
$quoted= $style->{compressor}->($_[1],$self);
} else {
$quoted=_quote($item);
}
}
$self->{buf}+=length($quoted);
$self->{buf}=length($1) if $quoted=~/\n([^\n]*)\s*\z/;
$self->{fh}->print($quoted); #;
}
if ($is_ro && $self->{style}{purity}) {
$self->_add_fix('sub call','make_ro',$name);
} elsif ($is_ro) {
$self->{fh}->print("$optspace)");
}
#return
}
$self->{do_nl}=0;
} else {
$self->{do_nl}=1;
$self->_dump_rv($item,$depth+1,$dumped,$name,$indent,$is_ref && !$add_do);
}
$self->{fh}->print("$optspace}")
if $add_do;
$self->_add_fix('sub call','weaken',$name)
if $self->{svw}{$addr};
return
}
sub _brace {
my ($self,$name,$type,$cond,$indent,$child)=@_;
my $open=$type=~/[\{\[\(]/;
my $brace= $name !~ /^[%@]/
? $type
: $type =~ /[\{\[\(]/
? '('
: ')';
$child= $child ? $self->{style}{optspace} : "";
if ( $cond ) {
$_[-2] += $open ? $self->{style}{indentcols}
: -$self->{style}{indentcols};
$self->{fh}->print($open ? "" : "\n".(" " x $_[-2]),
$brace,
$open ? "\n".(" " x $_[-2]) : "");
} else {
$self->{fh}->print($open ? "" : $child ,
$brace,
$open ? $child : "");
}
return
}
sub _dump_qr {
my ($self,$pat,$mod)=@_;
my %counts;
$counts{$_}++ foreach split //,$pat;
my ($quotes,$best)=('',length($pat)+1);
foreach my $char (qw( / ! % & <> {} " ),'#') { #"
my $bad=0;
$bad+=$counts{$_}||0 for split //,$char;
($quotes,$best)=($char,$bad) if $bad<$best;
last unless $best;
}
$pat=~s/(?!\\)([$quotes])/\\$1/g
if $best;
{
use utf8;
#$pat=~s/([^\x00-\x7f])/sprintf '\\x{%x}',ord $1/ge;
$pat=~s/([^\040-\176])/sprintf "\\x{%x}", ord($1)/ge;
}
$self->{fh}->print('qr',substr($quotes,0,1),$pat,substr($quotes,-1),$mod);
return
}
=for uedit32
sub _default_key_sorters{}
=cut
my %default_key_sorters= (
numeric => sub { [ sort {$a <=> $b} keys %{$_[0]} ] },
lexical => sub { [ sort keys %{$_[0]} ] },
smart => sub {
[
map { $_->[-1] }
sort {
( $a->[2] <=> $b->[2] )
||
( defined($a->[0])
? $a->[0] <=> $b->[0] || ($a->[1] cmp $b->[1])
: $a->[1] cmp $b->[1] )
||
( $a->[-1] cmp $b->[-1] )
}
map {
my $chars=lc($_);
my $num;
$num=$1 if $chars=~
s/\A(-?(?:0|[1-9]\d{0,8})(?:\.\d{0,15})?)(?!\d)//;
$chars=~s/\W//g;
[ $num, $chars, !defined $num ? 2 :
# length($chars) ? 1 :
0, $_ ]
} keys %{$_[0]}
]
},
'each'=>sub { undef },
);
$default_key_sorters{alphabetical}=$default_key_sorters{lexical};
$default_key_sorters{intelligent}=$default_key_sorters{smart};
for my $h (\%default_key_sorters) {
my $abr=Text::Abbrev::abbrev(keys %$h);
foreach my $short (keys %$abr) {
$h->{$short}=$h->{$abr->{$short}};
}
}
sub _get_keys {
my ($self,$item,$pass,$addr,$class)=@_;
my $sorter;
$class= "" if ! defined $class;
$sorter= $self->{style}{sortkeys}{"#$addr"}
|| $self->{cache_sorter}{$class};
if ( ! $sorter ) {
$sorter= $self->{style}{sortkeys}{".$class"}
|| ($class && $class->can("DDS_sortkeys") )
|| $self->{style}{sortkeys}{"."};
;
$self->{cache_sorter}{$class}=
($sorter ||= $default_key_sorters{smart});
}
my ($ary,$thaw)=$sorter->( $item, $pass, $addr, $class );
die "$item:$pass:$addr:$class:$ary:$thaw"
if $ary and reftype($ary) ne "ARRAY";
return ($ary,$thaw);
}
sub _dump_hash {
my ($self,$item,$depth,$dumped,$name,$indent,$addr,$class)=@_;
#Carp::confess "$name" unless defined $self->{ref_hkcnt}{$addr};
my ($keyary)= $self->_get_keys($item,1,$addr,$class);
if ($keyary and $DEBUG) {
warn "Keys: $keyary : @$keyary"
}
my $full_indent=$self->{style}{indent}>1;
my $ind=($self->{style}{indent}) &&
(!defined($self->{ref_hkcnt}{$addr}) or $self->{ref_hkcnt}{$addr}>1);
$self->_brace($name,'{',$ind,$indent,$self->{ref_hkcnt}{$addr}) ;
my $indkey=($ind && $self->{style}{indentkeys}) ? $self->{ref_hklen}{$addr} : 0;
my $cindent= $indent;
my $style= $self->{style};
my $optspace= $style->{optspace};
my $sep= $optspace . $self->{style}{hashsep} . $optspace;
my $pairsep= $self->{style}{pairsep};
if ($indkey) {
$cindent+= $indkey + length($sep);
}
$DEBUG==10 and print "Indent $ind $indkey $cindent\n";
my ($kc,$ix)=(0,0);
my $last_n=0;
my $ind_str=" " x $indent;
while (defined(my $k=defined $keyary ? $keyary->[$ix++] : each %$item)) {
$last_n=0 if ref $item->{$k};
if ( $kc ) {
my $do_ind=$ind && !$last_n ;
$self->{fh}->print($pairsep, $do_ind ? "\n$ind_str" : $optspace);
$self->{buf}++;
if ($do_ind) {
$self->{buf}=0;
} elsif (!$do_ind && !$optspace && $self->{buf} > 1024 ) {
$self->{fh}->print("\n");
$self->{buf}=0;
}
} else {
#$self->{fh}->print("\n$ind_str") if !$last_n;
$kc=1;
}
if ($indkey) {
my $qk=_quotekey($k);
my $str=$indkey>=length($qk)
? join "",$qk," " x ($indkey-length($qk)), $sep
: join "",$qk,"\n$ind_str"," " x $indkey, $sep
;
$self->{buf}+=length($str);
$self->{fh}->print($str);
} else {
my $str=_quotekey($k).$sep;
$self->{buf}+=length($str);
$self->{fh}->print($str);
}
my $alias=$self->_dump_sv($item->{$k},$depth+1,$dumped,
$self->_build_name($name,'{',$k),
$cindent
);
if (!$full_indent and !$self->{do_nl} and $self->{buf}<60) {
#warn "$self->{buf}\n";
$last_n++;
} else {
#warn "$self->{buf}\n";
$last_n=0;
}
if ($alias) {
$self->_add_fix('sub call','alias_hv',
$self->_build_name($name,'%'),
_quote($k),
$alias
);
}
}
$self->_brace($name,'}',$ind,$indent,$self->{ref_hkcnt}{$addr});
return
}
sub _dump_array {
my ($self,$item,$depth,$dumped,$name,$indent)=@_;
my $full_indent=$self->{style}{indent}>1;
my $ind=$self->{style}{indent} && @$item>1;
$self->_brace($name,'[',$ind,$indent,scalar @$item);
my $last_n=0;
my $ind_str=(" " x $indent);
my ($optspace,$sep)=@{$self->{style}}{qw(optspace arysep)};
unless ($self->{style}{rle} ) {
foreach my $k (0..$#$item) {
my $do_ind=$ind && (!$last_n || ref $item->[$k]);
if ($k) {
$self->{fh}->print($sep, $do_ind ? "\n$ind_str" : $optspace);
if ($do_ind) {
$self->{buf}=0;
} elsif (!$do_ind && !$optspace && $self->{buf} > 1024 ) {
$self->{fh}->print("\n");
$self->{buf}=0;
}
}
my $alias=$self->_dump_sv($item->[$k],$depth+1,$dumped,
$self->_build_name($name,'[',$k),
$indent
);
if (!$full_indent and !$self->{do_nl} and $self->{buf}<60) {
#warn "$last_n\n";
$last_n++;
} else {
$last_n=0;
}
if ($alias) {
$self->_add_fix('sub call','alias_av',
$self->_build_name($name,'@'),
$k,
$alias
);
}
}
} else {
# this is evil and must be changed.
# ... evil ... totally evil... blech
for ( my $k = 0 ; $k <= $#$item ; ) {
my $v = $item->[$k];
my $count = 1;
if (!refaddr($item->[$k]) and !readonly($item->[$k])
and (!$self->{sv}{refaddr(\$item->[$k])} or
$self->{svt}[$self->{sv}{refaddr(\$item->[$k])}]==1)
)
{
COUNT:while (
$k + $count <= $#$item
and !refaddr($item->[ $k + $count ])
and !readonly($item->[ $k + $count ])
and (!$self->{sv}{refaddr(\$item->[$k + $count])} or
$self->{svt}[$self->{sv}{refaddr(\$item->[$k + $count])}]==1)
and !$v == !$item->[ $k + $count ]
and defined($v) == defined($item->[ $k + $count ])
)
{
if (!defined( $item->[ $k + $count ] )) {
last COUNT if defined($v);
} else {
last COUNT if
$v ne overload::StrVal( $item->[ $k + $count ] )
}
$count++;
}
}
my $do_ind=$ind && (!$last_n || ref $item->[$k]);
$self->{fh}->print($sep, $do_ind ? "\n$ind_str" : $optspace)
if $k;
$self->{buf}=0 if $do_ind and $k;
if ($count>1){
$self->{fh}->print("($optspace");
$self->{buf}+=2;
}
my $alias=$self->_dump_sv($item->[$k],$depth+1,$dumped,
$self->_build_name($name,'[',$k),
$indent
);
if (!$full_indent and !$self->{do_nl} and $self->{buf}<60) {
$last_n++;
} else {
$last_n=0;
}
if ($alias) {
$self->_add_fix('sub call','alias_av',
$self->_build_name($name,'@'),
$k,
$alias
);
}
if ($count>1) {
my $str=join $optspace,'',')','x',$count;
$self->{buf}+=length($str);
$self->{fh}->print($str);
}
$k += $count;
}
}
$self->_brace($name,']',$ind,$indent,scalar @$item);
return
}
sub __vstr {
my ($v,@v);
unless (@_) {
$v=$];
} elsif (@_==1) {
$v=shift;
} else {
@v=@_;
}
return join ".", @v ? (@v,(0) x 3)[0..2]
: map { $v * 1000**$_ % 1000 } 0..2
}
sub _dump_code {
my ($self,$item,$name,$indent,$class)=@_;
unless ($self->{style}{deparse}) {
$self->{fh}->print($self->{style}{codestub});
} else { #deparseopts
my $cv=B::svref_2object($item);
if (ref($cv->ROOT)=~/NULL/) {
my $gv=$cv->GV;
$self->{fh}->print("\\&",$gv->STASH->NAME,"::",$gv->SAFENAME);
return;
}
my $deparser=Data::Dump::Streamer::Deparser->new(@{$self->{style}{deparseopts}});
my $used= _get_lexicals($item);
my %targ;
foreach my $targ (keys %$used) {
next if $targ=~/\D/;
my $addr=refaddr($used->{$targ});
$targ{$targ}=$self->{lexicals}{a2n}{$addr}
if $self->{lexicals}{a2n}{$addr};
}
# we added this method, its not a normal method. see bottom of file.
$deparser->dds_usenames(\%targ);
my $bless=undef;
my $code;
DEPARSE:{
$bless=($class,bless($item,$bless))[0] if defined $bless;
eval { $code=$deparser->coderef2text($item) };
bless $item,$bless if defined $bless;
if (!defined $bless and $@ and
$@ =~ /^\QUsage: ->coderef2text(CODEREF)\E/)
{
$bless='CODE';
redo DEPARSE;
} elsif ($@) {
warnings::warnif "Using CODE stub for $name as ".
"B::Deparse->coderef2text (v$B::Deparse::VERSION".
" on v@{[__vstr]}) failed. Message was:\n $@";
$self->{fh}->print($self->{style}{codestub});
return;
}
}
#$self->{fh}->print("\n#",join " ",keys %$used,"\n");
#$code=~s/^\s*(\([^)]+\)|)\s*/sub$1\n/;
$code="sub".($code=~/^\s*\(/ ? "" : " ").$code;
if ($self->{style}{indent}) {
$code=~s/\n/"\n"." " x $indent/meg;
}
#warn $name;
if ($name=~s/^\&//) {
$code=~s/sub(\s)?/sub $name$1/;
}
$self->{fh}->print("$code");
}
return
}
sub _dump_format {
# from link from [ysth]: http://groups.google.com/groups?selm=laUs8gzkgOlT092yn%40efn.org
# translate arg (or reference to it) into a B::* object
my ($self,$item,$name,$indent)=@_;
if ($self->{style}{deparse}) {
my $Bobj = B::svref_2object($item);
# if passed a glob or globref, get the format
$Bobj = B::GV::FORM($Bobj) if ref $Bobj eq 'B::GV';
if (ref $Bobj eq 'B::FM') {
my $format;
eval {
my $deparser = Data::Dump::Streamer::Deparser->new();
$format=$deparser->indent($deparser->deparse_format($Bobj));
};
if ($@) {
warnings::warnif "B::Deparse (v$B::Deparse::VERSION on v@{[__vstr]}) failed FORMAT ref deparse.\n";
$format="B::Deparse (v$B::Deparse::VERSION on v@{[__vstr]}) failed FORMAT ref deparse.\n.\n";
}
my $ind=$self->{style}{indent} ? ' ' x $indent : '';
$format="format F =\n$format";
$format=~s/^/${ind}# /gm;
my $end='_EOF_FORMAT_';
$end=~s/T(\d*)_/sprintf "T%02d_",($1||0)+1/e
while $format=~/$end/;
$self->{fh}->print("do{ local *F; my \$F=<<'$end'; \$F=~s/^\\s+# //mg; eval \$F; die \$F.\$@ if \$@; *F{FORMAT};\n$format\n$end\n$ind}");
return
}
}
$self->{fh}->print($self->{style}{formatstub});
}
sub _dump_symbol {
my ($self,$item,$name,$glob,$deref,$depth)=@_;
my $ret="Symbol::gensym";
$ret="do{ require Symbol; $ret }"
unless $self->{reqs}{Symbol}++;
$ret="*{ $ret }"
if $deref;
$self->{fh}->print( $ret );
if ($self->{style}{dumpglob} and !$self->{sv_glob_du}{$glob}++) {
$self->_add_fix('glob',$_[1],$glob,$depth+1,$name);
}
}
sub _dump_rv {
my ($self,$item,$depth,$dumped,$name,$indent,$add_do)=@_;
my ($addr,$idx,$type,$class,$is_frozen_replacement,$overloaded,
$raddr);
GETITEM: {
$addr=refaddr($item) or Carp::confess "$name : $item";
$idx=$self->{ref}{$addr};
$type=reftype($item);
$class=blessed($item);
$class=undef if $class and $class eq 'Regexp' and is_regexp $item;
$DEBUG and
printf "_dump_rv %d %s %#x\n",$depth,$name,$addr;
my $ignore=0;
if ($self->{ref_fz}{$addr}) {
$item= $self->{ref_fz}{$addr};
if ( ! $item ) {
$ignore=1;
} elsif (ref $item) {
$is_frozen_replacement=1;
$dumped= \do{my $d};
$raddr=$addr;
redo GETITEM;
} else {
$self->{buf}+=length($item);
$self->{fh}->print($item);
return
}
}
if ($ignore or $self->{style}{ignore}{"#".($raddr||$addr)} or
(defined $class and $self->{style}{ignore}{".$class"} )
){
my $str= _quote("Ignored Obj [".overload::StrVal($item)."]");
$self->{buf} += length($str);
$self->{fh}->print($str);
return
}
}
unless ($idx) {
#Carp::confess "Unhandled address $addr $name\n";
# this should only happen for localized globs.
($idx)=$self->_reg_ref($item,$depth,$name,refcount($item));
}
my $optspace=$self->{style}{optspace};
if ($idx) {
my $pre_dumped=$self->{refdu}[$idx];
my $str="";
if ($pre_dumped and $$pre_dumped) {
# its been dumped totally
$DEBUG and print " predumped $self->{refn}[$idx]\n";
if ($self->{refn}[$idx]=~/^[\@\%\&]/) {
if (SvREADONLY_ref($item)) {
my @hidden_keys=sort(hidden_keys(%$item));
$self->_add_fix('lock',$idx,\@hidden_keys);
}
$str=join "",($class ? "bless($optspace" : ''),
'\\'.$self->{refn}[$idx],
($class ? ",$optspace"._quote($class)."$optspace)" : '');
} else {
$str=$self->{refn}[$idx];
}
$self->{buf}+=length($str);
$self->{fh}->print($str);
return
} elsif ($pre_dumped or $self->{refd}[$idx] < $depth) {
$DEBUG and print " inprocess or depth violation: $self->{refd}[$idx] < $depth\n";
# we are in the process of dumping it
# output a place holder and add a fix statement
# XXX is this sigil test correct? why not $?
if ($self->{refn}[$idx]=~/^[\@\%\&]/ and (!$self->{style}{declare})) {
$str=join"",( $class ? "bless($optspace" : '' ),
'\\'.$self->{refn}[$idx],
( $class ? ",$optspace"._quote($class)."$optspace)" : '' );
} else {
if ($self->{style}{purity}) {
$str=join"",$add_do ? join($optspace,qw(do { my $v = ),'') : '',
!$self->{style}{verbose} ? "'V'" : _quote("V: ",$self->{refn}[$idx]),
$add_do ? $optspace."}" : '';
#Carp::cluck "$name $self->{refd}[$idx] < $depth" if $name=~/\*/;
$self->_add_fix('ref',$name,$idx,$class);
} else {
$str=$self->{refn}[$idx];
}
}
$self->{buf}+=length($str);
$self->{fh}->print($str);
return
}
$self->{refdu}[$idx]||=$dumped;
#$name=$self->{refn}[$idx]; # override inherited names. ??? maybe not needed
} else {
Carp::confess "Unhandled object '$item'\n";
}
my $isoverloaded=0;
if (defined $class and overload::Overloaded($item)) {
disable_overloading( $item );
$overloaded= $class;
$isoverloaded= 1;
}
my $thaw= $self->{ref_thaw}{$raddr||$addr};
my ($inline,$thawtype);
if ( $thaw ) {
if ($thaw =~ /[^\w:>()-]/) {
$thawtype= "code";
} else{
$inline= $thaw=~s/^->//;
$thawtype= $thaw=~s/\(\)$// ? "sub" : "method";
}
if ($inline && $thawtype eq 'sub') {
$self->{buf}+=length($thaw)+1;
$self->{fh}->print($thaw."(${optspace}");
}
}
$self->{do_nl}=1;
my $add_lock=($type eq 'HASH') && SvREADONLY_ref($item);
my $fix_lock=0;
my @hidden_keys=$add_lock ? sort(hidden_keys(%$item)) : ();
if ($add_lock) {
#warn "$name\n";
if ($name!~/^\$/) {
$fix_lock=1;
$add_lock=0;
} else {
$self->{fh}->print("lock_ref_keys",
@hidden_keys ? '_plus' : '',
"(${optspace}"
);
}
}
my $add_bless=defined($class) && ($name!~/^[\@\%\&]/);
if ($add_bless && !$overloaded) {
$self->{fh}->print(substr($self->{style}{bless},0,-1),$optspace);
}
$DEBUG and print " $type : Start typecheck\n";
if ($type eq 'SCALAR' or $type eq 'REF' or $type eq 'GLOB') {
my ($pat,$mod)=$type eq 'SCALAR' ? regex($item) : ();
my $glob=$type eq 'GLOB' ? globname $$item : '';
if ($glob=~/^\*Symbol::GEN/) {
$self->_dump_symbol($_[1],$name,$glob,0,$depth);
} elsif (defined $pat) {
# its a regex
$self->_dump_qr($pat,$mod);
} else {
my $ret=$self->_dump_sv($$item,$depth+1,$dumped,
$self->_build_name($name,'$'),
$indent,'is_ref'
);
$self->{refdu}[$idx]=$ret if $ret;
}
} elsif ($type eq 'ARRAY') {
$self->_dump_array($item,$depth,$dumped,$name,$indent);
} elsif ($type eq 'HASH') {
$self->_dump_hash($item,$depth,$dumped,$name,$indent,$addr,$class);
} elsif ($type eq 'CODE') {
$self->_dump_code($item,$name,$indent,$class);
} elsif ($type eq 'FORMAT') {
#$self->_dump_code($item,$name,$indent,$class); #muwhahahah
$self->_dump_format($item,$name,$indent);
} elsif ($type eq 'IO') {
$self->{fh}->print("*{Symbol::gensym()}{IO}");
} elsif ($type eq 'ORANGE' || $type eq 'Regexp' || $type eq 'REGEXP') {
my ($pat,$mod)=regex($item);
$self->_dump_qr($pat,$mod);
} else {
Carp::confess "_dump_rv() can't handle '$type' objects yet\n :-(\n";
}
if ($add_bless) {
unless ( defined $overloaded ) {
$self->{fh}->print(",${optspace}",_quote($class),$optspace,substr($self->{style}{bless},-1))
} else {
$self->_add_fix('bless',$idx,$overloaded);
}
if ($isoverloaded) {
restore_overloading( $item, $overloaded );
}
}
if ($fix_lock && !defined($class)) {
$self->_add_fix('lock',$idx,\@hidden_keys);
}
if ($add_lock) {
if (@hidden_keys) {
$self->{fh}->print(",${optspace}",join(",${optspace}",map {_quote($_)} @hidden_keys));
}
$self->{fh}->print("${optspace})");
}
if ( $thaw ) {
if ($inline) {
if ($thawtype eq 'sub') {
$self->{fh}->print("${optspace})");
} elsif ($thawtype eq 'method') {
$self->{fh}->print("->$thaw()");
}
#$$dumped=1;
} else {
$self->_add_fix('thaw', $idx, $thaw.($thawtype eq 'sub' ? "()" :"" ));
}
}
if ( my $postop=$self->{ref_postop}{$raddr||$addr} ) {
if (ref $postop) {
$postop->($_[1]);
} else {
$_[1]->$postop();
}
}
$self->{do_nl}=1;
return
}
=item Names
=item Names LIST
=item Names ARRAYREF
Takes a list of strings or a reference to an array of strings to use for
var names for the objects dumped. The names may be prefixed by a *
indicating the variable is to be dumped as its dereferenced type if it is
an array, hash or code ref. Otherwise the star is ignored. Other sigils
may be prefixed but they will be silently converted to *'s.
If no names are provided then names are generated automatically based on
the type of object being dumped, with abbreviations applied to compound
class names.
If called with arguments then returns the object itself, otherwise in list
context returns the list of names in use, or in scalar context a reference
or undef. In void context with no arguments the names are cleared.
B<NOTE:>
Must be called before C<Data()> is called.
=cut
sub Names {
my $self = shift->_safe_self;
if (@_) {
my $v=(@_==1 and reftype $_[0] eq 'ARRAY') ? shift @_ : \@_;
$self->{unames} = [
map {
( my $s = $_ ) =~ s/^[\@\%\&-]/*/;
$s=~s/^\$//;
Carp::confess "Bad name '$_'"
if $s && $s!~/^\*?\w+$/;
$s
} grep {defined} @$v ];
return $self;
} elsif (! defined wantarray ) {
$self->{unames}=[];
}
#elsif ( eval { require PadWalker; 1 } ) {
# print DDumper(PadWalker::peek_my(1));
# return $self;
#}
return wantarray ? @{$self->{unames}||[]} : $self->{unames}
}
=for UEDIT
sub Purity {}
=item Purity
=item Purity BOOL
This option can be used to set the level of purity in the output. It
defaults to TRUE, which results in the module doing its best to ensure
that the resulting dump when eval()ed is precisely the same as the input.
However, at times such as debugging this can be tedious, resulting in
extremely long dumps with many "fix" statements involved. By setting
Purity to FALSE the resulting output won't necessarily be legal Perl, but
it will be more legible. In this mode the output is broadly similar to
that of the default setting of Data::Dumper (Purity(0)). When set to TRUE
the behaviour is likewise similar to Data::Dumper in Purity(1) but more
accurate.
When Purity() is set to FALSE aliases will be output with a function call
wrapper of 'alias_to' whose argument will be the value the item is an
alias to. This wrapper does nothing, and is only there as a visual cue.
Likewise, 'make_ro' will be output when the value was readonly, and again
the effect is cosmetic only.
=item To
=item To STREAMER
Specifies the object to print to. Data::Dump::Streamer can stream its
output to any object supporting the print method. This is primarily meant
for streaming to a filehandle, however any object that supports the method
will do.
If a filehandle is specified then it is used until it is explicitly
changed, or the object is destroyed.
=cut
sub To {
my $self = shift->_safe_self;
if (@_) {
$self->{fh} = shift;
return $self;
}
return $self->{fh};
}
=for UEDIT
sub Declare {}
=item Declare
=item Declare BOOL
If Declare is True then each object is dumped with 'my' declarations
included, and all rules that follow are obeyed. (Ie, not referencing an
undeclared variable). If Declare is False then all objects are expected to
be previously defined and references to top level objects can be made at
any time.
Defaults to False.
=cut
sub Indent {
my $self=shift->_safe_self();
if (@_) {
my $val=shift;
if ( $val == 0 && length $self->{style}{optspace} ) {
$self->{style}{last_optspace}= $self->{style}{optspace};
$self->{style}{optspace}= "";
} elsif( !$self->{style}{indent} && ! length $self->{style}{optspace} )
{
$self->{style}{optspace}= $self->{style}{last_optspace};
}
$self->{style}{indent}= $val;
return $self
} else {
return $self->{style}{indent}
}
}
=item Indent
=item Indent INT
If Indent is True then data is output in an indented and fairly neat
fashion. If the value is 2 then hash key/value pairs and array values each
on their own line. If the value is 1 then a "smart" indenting mode is
activated where multiple key/value or values may be printed to the same
line. The heuristics for this mode are still experimental so it may
occasional not indent very nicely.
Default is Indent(2)
If indent is False then no indentation is done, and all optional whitespace.
is omitted. See <OptSpace()|/OptSpace> for more details.
Defaults to True.
Newlines are appended to each statement regardless of this value.
=for UEDIT
sub IndentKeys {}
=item Indentkeys
=item Indentkeys BOOL
If Indent() and Indentkeys are True then hashes with more than one key
value pair are dumped such that the keys and values line up. Note however
this means each key has to be quoted twice. Not advised for very large
data structures. Additional logic may enhance this feature soon.
Defaults to True.
B<NOTE:>
Must be set before C<Data()> is called.
=for UEDIT
sub OptSpace {}
=item OptSpace
=item OptSpace STR
Normally DDS emits a lot of whitespace in between tokens that it
emits. Using this method you can control how much whitespace it
will emit, or even if some other string should be used.
If Indent is set to 0 then this value is automatically set to
the empty string. When Indent is set back to a non zero value
the old value will be restored if it has not been changed from
the empty string in the intervening time.
=for UEDIT
sub Keyorder {}
=item KeyOrder TYPE_OR_OBJ
=item KeyOrder TYPE_OR_OBJ, VALUE
Sets or returns the key order to for use for a given type or object.
TYPE_OR_OBJ may be a string representing a class, or "" for representing
unblessed objects, or it maybe a reference to a hash.
VALUE may be a string representing one of built in sort mechanisms, or
it may be a reference to a subroutine, or a method name if TYPE_OR_OBJ
is not an object.
The built in sort mechanisms are 'aphabetical'/'lexical', 'numeric',
'smart'/'intelligent' and 'each'.
If VALUE is omitted returns the current value for the given type.
If TYPE_OR_OBJ is omitted or FALSE it defaults to "" which represents
unblessed hashes.
See L<"Controlling Hash Traversal and Display Order"> for more details.
=item SortKeys
=item SortKeys VALUE
This is a wrapper for KeyOrder. It allows only the generic hash
sort order to be specified a little more elegantly than via KeyOrder().
It is syntactically equivalent to
$self->KeyOrder( "", @_ );
=for UEDIT
sub Verbose {}
=item Verbose
=item Verbose BOOL
If Verbose is True then when references that cannot be resolved in a
single statement are encountered the reference is substituted for a
descriptive tag saying what type of forward reference it is, and to what
is being referenced. The type is provided through a prefix, "R:" for
reference, and "A:" for alias, "V:" for a value and then the name of the
var in a string. Automatically generated var names are also reduced to
the shortest possible unique abbreviation, with some tricks thrown in
for Long::Class::Names::Like::This (which would abbreviate most likely
to LCNLT1)
If Verbose if False then a simple placeholder saying 'A' or 'R' is
provided. (In most situations perl requires a placeholder, and as such
one is always provided, even if technically it could be omitted.)
This setting does not change the followup statements that fix up the
structure, and does not result in a loss of accuracy, it just makes it a
little harder to read. OTOH, it means dumps can be quite a bit smaller
and less noisy.
Defaults to True.
B<NOTE:>
Must be set before C<Data()> is called.
=for UEDIT
sub DumpGlob {}
=item DumpGlob
=item DumpGlob BOOL
If True then globs will be followed and fully defined, otherwise the globs
will still be referenced but their current value will not be set.
Defaults to True
B<NOTE:>
Must be set before C<Data()> is called.
=for UEDIT
sub Deparse {}
=item Deparse
=item Deparse BOOL
If True then CODE refs will be deparsed use L<B::Deparse|B::Deparse> and
included in the dump. If it is False the a stub subroutine reference will
be output as per the setting of C<CodeStub()>.
Caveat Emptor, dumping subroutine references is hardly a secure act, and
it is provided here only for convenience.
Note using this routine is at your own risk as of DDS 1.11, how it
interacts with the newer advanced closure dumping process is undefined.
=for UEDIT
sub EclipseName {}
=item EclipseName
=item EclipseName SPRINTF_FORMAT
When necessary DDS will rename vars output during deparsing with this
value. It is a sprintf format string that should contain only and both of
the "%s" and a "%d" formats in any order along with whatever other literal
text you want in the name. No checks are performed on the validity of this
value so be careful. It defaults to
"%s_eclipse_%d"
where the "%s" represents the name of the var being eclipsed, and the "%d"
a counter to ensure all such mappings are unique.
=for UEDIT
sub DeparseOpts {}
=item DeparseOpts
=item DeparseOpts LIST
=item DeparseOpts ARRAY
If Deparse is True then these options will be passed to B::Deparse->new()
when dumping a CODE ref. If passed a list of scalars the list is used as
the arguments. If passed an array reference then this array is assumed to
contain a list of arguments. If no arguments are provided returns a an
array ref of arguments in scalar context, and a list of arguments in list
context.
Note using this routine is at your own risk as of DDS 1.11, how it
interacts with the newer advanced closure dumping process is undefined.
=for UEDIT
sub CodeStub {}
=item CodeStub
=item CodeStub STRING
If Deparse is False then this string will be used in place of CODE
references. Its the users responsibility to make sure its compilable and
blessable.
Defaults to 'sub { Carp::confess "Dumped code stub!" }'
=for UEDIT
sub FormatStub {}
=item FormatStub
=item FormatStub STRING
If Deparse is False then this string will be used in place of FORMAT
references. Its the users responsibility to make sure its compilable and
blessable.
Defaults to 'do{ local *F; eval "format F =\nFormat Stub\n.\n"; *F{FORMAT} }'
=for UEDIT
sub DeparseGlob {}
=item DeparseGlob
=item DeparseGlob BOOL
If Deparse is TRUE then this style attribute will determine if subroutines
and FORMAT's contained in globs that are dumped will be deparsed or not.
Defaults to True.
=for UEDIT
sub DualVars {}
sub Dualvars {}
=item Dualvars
=item Dualvars BOOL
=item Dualvars
=item Dualvars BOOL
If TRUE then dualvar checking will occur and the required statements
emitted to recreate dualvars when they are encountered, otherwise items
will be dumped in their stringified form always. It defaults to TRUE.
=for UEDIT
sub Rle {}
sub RLE {}
=item Rle
=item Rle BOOL
=item RLE
=item RLE BOOL
If True then arrays will be run length encoded using the C<x> operator.
What this means is that if an array contains repeated elements then
instead of outputting each and every one a list multiplier will be output.
This means that considerably less space is taken to dump redundant data.
=item Freezer
=item Freezer ACTION
=item Freezer CLASS, ACTION
This method can be used to override the DDS_freeze hook for a
specific class. If CLASS is omitted then the ACTION applies to
all blessed object.
If ACTION is false it indicates that the given CLASS should not
have any serilization hooks called.
If ACTION is a string then it is taken to be the method name that
will be executed to freeze the object. CLASS->can(METHOD) must return
true or the setting will be ignored.
If ACTION is a code ref it is executed with the object as the argument.
When called with no arguments returns in scalar context the generic
serialization method (defaults to 'DDS_freeze'), in list context
returns the generic serialization method followed by a list of pairs
of Classname=>ACTION.
If the action executes a sub or method it is expected to return
a list of three values:
( $proxy, $thaw, $postdump )=$obj->DDS_Freeze();
See L<Controlling Object Representation> for more details.
B<NOTE:>
Must be set before C<Data()> is called.
=cut
sub Freezer {
my $self= shift;
if ( @_==1 ) {
$self->{style}{freezer}= shift;
return $self;
} elsif ( @_==2 ) {
my ( $class, $action )= @_;
$self->{style}{freeze_class}{$class}= $action;
return $self;
}
return wantarray ? ($self->{style}{freezer},
map { $_ => $self->{style}{freeze_class}{$_} }
keys %{$self->{style}{freeze_class}} )
: $self->{style}{freezer};
}
sub ResetFreezer {
my $self=shift;
$self->{style}{freezer}='DDS_freeze';
$self->{style}{freeze_class}={};
return $self;
}
=item Ignore
=item Ignore OBJ_OR_CLASS
=item Ignore OBJ_OR_CLASS, BOOL
Allows a given object or class to be ignored, and replaced with
a string containing the name of the item ignored.
If called with no args returns a list of items ignored (using the refaddr
to represent objects). If called with a single argument returns whether
that argument is ignored. If called with more than one arguments then
expects a list of pairs of object => is_ignored.
Returns $self when setting.
B<NOTE:>
Must be set before C<Data()> is called.
=cut
sub Ignore {
my $self=shift;
if (@_==0) {
return map { s/^.//; $_ } keys %{$self->{style}{ignore}};
}
Carp::confess("Must have an even number of arguments in Ignore()")
if @_>1 && @_ %2;
while (@_) {
my $item=shift;
if ( ref $item ) {
$item="#".refaddr($item);
} else {
$item=".$item";
}
if ( ! @_ ) {
return $self->{style}{ignore}{$item};
}
if ( shift ) {
$self->{style}{ignore}{$item}= 1;
} else {
delete $self->{style}{ignore}{$item};
}
}
return $self;
}
=for UEDIT
sub Compress {}
=item Compress
=item Compress SIZE
Controls compression of string values (not keys). If this value
is nonzero and a string to be dumped is longer than its value then
the L<Compressor()|/Compressor> if defined is used to compress
the string. Setting size to -1 will cause all strings to be
processed, setting size to 0 will cause no strings to be processed.
=for UEDIT
sub Compressor {}
=item Compressor
=item Compressor CODE
This attribute is used to control the compression of strings.
It is expected to be a reference to a subroutine with the following
interface:
my $prelude_code=$compressor->(); # no arguments.
my $code=$compressor->('string'); # string argument
The sub will be called with no arguments at the beginning of the
dump to allow any require statements or similar to be added. During
the dump the sub will be called with a single argument when
compression is required. The code returned in this case is expected
to be an EXPR that will evaluate back to the original string.
By default DDS will use L<Compress::Zlib> in conjunction with
L<MIME::Base64> to do compression and encoding, and exposes the
'usqz' subroutine for handling the decoding and decompression.
The abbreviated name was chosen as when using the default compressor
every string will be represented by a string like
usqz('....')
Meaning that eight characters are required without considering the
data itself. Likewise Base64 was chosen because it is a representation
that is high-bit safe, compact and easy to quote. Escaped strings are
much less efficient for storing binary data.
=cut
# weird styling here deliberate.
sub
DeparseOpts
{
my $self=shift;
if (@_) {
if (ref $_[0]) {
$self->{style}{deparseopts}=shift;
} else {
$self->{style}{deparseopts}=[@_];
}
return $self;
} else {
return wantarray ? @{$self->{style}{deparseopts}}
: $self->{style}{deparseopts};
}
}
sub KeyOrder {
my $self= shift;
Carp::croak("KeyOrder() Must have an even number of arguments if doing a multiple set.")
if @_>2 and @_ % 2;
while (@_) {
my $obj= shift;
my $name;
if (ref $obj) {
$name= "#" .refaddr($obj)
} else {
$name= "" if ! defined $obj;
$name= ".$obj";
}
if ( ! @_ ) {
return $self->{style}{sortkeys_string}{$name}||
$self->{style}{sortkeys}{$name};
}
my $val= shift;
if ( ! defined $val ) {
delete $self->{style}{sortkeys}{$name};
delete $self->{style}{sortkeys_string}{$name};
} else {
if ( ! ref $val ) {
my $subref= $default_key_sorters{$val};
Carp::confess("Unblessed or per object Sortkeys() must be coderefs:'$val'\n")
if (!$subref or $name eq "." )
and reftype($subref) ne "CODE";
$subref ||= $obj->can($val);
die "Unknown sortkeys '$val', and "
. (ref($obj)||$obj)." doesn't know how to do it.\n"
if !$subref;
$self->{style}{sortkeys_string}{$name}=$val;
$val= $subref;
} elsif ( reftype($val) eq 'ARRAY' ) {
my $aryref= $val;
$val= sub{ return $aryref; };
} elsif ( reftype($val) ne 'CODE' ) {
Carp::confess("Can't use '$val' as KeyOrder() value");
}
$self->{style}{sortkeys}{$name}= $val;
}
}
return $self;
}
*Keyorder=*KeyOrder;
sub SortKeys {
my $self=shift;
$self->KeyOrder("",@_);
}
*Sortkeys= *SortKeys;
*HashKeys = *Hashkeys = *KeyOrder;
my %scalar_meth=map{ $_ => lc($_)}
qw(Declare Indent IndentCols IndentKeys
Verbose DumpGlob Deparse DeparseGlob DeparseFormat CodeStub
FormatStub Rle RLE Purity DualVars Dualvars EclipseName
Compress Compressor OptSpace);
sub AUTOLOAD {
(my $meth=$AUTOLOAD)=~s/^((?:\w+::)+)//;
my $name;
if (defined($name=$scalar_meth{$meth})) {
$DEBUG and print "AUTLOADING scalar meth $meth ($name)\n";
eval '
sub '.$meth.' {
my $self=shift->_safe_self();
if (@_) {
$self->{style}{'.$name.'}=shift;
return $self
} else {
return $self->{style}{'.$name.'}
}
}
';
$@ and die "$meth:$@\n";
goto &$meth;
} elsif ($meth=~/[^A-Z]/) {
Carp::confess "Unhandled method/subroutine call $AUTOLOAD";
}
}
sub _get_lexicals {
my $cv=shift;
if ($HasPadWalker) {
my ($names,$targs)=PadWalker::closed_over($cv);
if ($PadWalker::VERSION < 1) {
$names->{$_}=$names->{$targs->{$_}} for keys %$targs;
} else {
%$names=(%$names,%$targs);
}
return $names;
}
my $svo=B::svref_2object($cv);
my @pl_array = $svo->PADLIST->ARRAY;
my @name_obj = $pl_array[0]->ARRAY;
my %named;
for my $i ( 0..$#name_obj ) {
if ( ref($name_obj[$i])!~/SPECIAL/) {
$named{$i} = "${ $name_obj[$i]->object_2svref }";
}
}
my %inited;
my %used;
B::Utils::walkoptree_filtered(
$svo->ROOT,
sub { B::Utils::opgrep { name => [ qw[ padsv padav padhv ] ] }, @_ },
sub {
my ( $op, @items )=@_;
my $targ = $op->targ;
my $name = $named{$targ}
or return;
$inited{$name}++
if $op->private & 128;
if ( !$inited{$name} ) {
$used{$name} = $pl_array[1]->ARRAYelt($targ)->object_2svref;
$used{$targ} = $used{$name};
$inited{$name}++;
}
}
);
return \%used;
}
package Data::Dump::Streamer::Deparser;
use B::Deparse;
our @ISA=qw(B::Deparse);
my %cache;
our $VERSION = '2.36';
if ( $VERSION ne $Data::Dump::Streamer::VERSION ) {
die "Incompatible Data::Dump::Streamer::Deparser v$VERSION vs Data::Dump::Streamer v$Data::Dump::Streamer::VERSION";
}
sub dds_usenames {
my $self=shift;
my $names=shift;
$cache{Data::Dump::Streamer::refaddr $self}=$names;
}
sub padname {
my $self = shift;
my $targ = shift;
if ( $cache{Data::Dump::Streamer::refaddr $self} and $cache{Data::Dump::Streamer::refaddr $self}{$targ} ) {
return $cache{Data::Dump::Streamer::refaddr $self}{$targ}
}
return $self->padname_sv($targ)->PVX;
}
sub DESTROY {
my $self=shift;
delete $cache{Data::Dump::Streamer::refaddr $self};
}
unless (B::AV->can('ARRAYelt')) {
eval <<' EOF_EVAL';
sub B::AV::ARRAYelt {
my ($obj,$idx)=@_;
my @array=$obj->ARRAY;
return $array[$idx];
}
EOF_EVAL
}
1;
__END__
=back
=head2 Reading the Output
As mentioned in L<Verbose> there is a notation used to make understanding
the output easier. However at first glance it can probably be a bit
confusing. Take the following example:
my $x=1;
my $y=[];
my $array=sub{\@_ }->( $x,$x,$y );
push @$array,$y,1;
unshift @$array,\$array->[-1];
Dump($array);
Which prints (without the comments of course):
$ARRAY1 = [
'R: $ARRAY1->[5]', # resolved by fix 1
1,
'A: $ARRAY1->[1]', # resolved by fix 2
[],
'V: $ARRAY1->[3]', # resolved by fix 3
1
];
$ARRAY1->[0] = \$ARRAY1->[5]; # fix 1
alias_av(@$ARRAY1, 2, $ARRAY1->[1]); # fix 2
$ARRAY1->[4] = $ARRAY1->[3]; # fix 3
The first entry, C<< 'R: $ARRAY1->[5]' >> indicates that this slot in the
array holds a reference to the currently undefined C<< $ARRAY1->[5] >>,
and as such the value will have to be provided later in what the author
calls 'fix' statements. The third entry C<< 'A: $ARRAY1->[1]' >> indicates
that is element of the array is in fact the exact same scalar as exists in
C<< $ARRAY1->[1] >>, or is in other words, an alias to that variable.
Again, this cannot be expressed in a single statement and so generates
another, different, fix statement. The fifth entry C<< 'V: $ARRAY1->[3]' >>
indicates that this slots holds a value (actually a reference value)
that is identical to one elsewhere, but is currently undefined. In this
case it is because the value it needs is the reference returned by the
anonymous array constructor in the fourth element (C<< $ARRAY1->[3] >>).
Again this results in yet another different fix statement. If Verbose()
is off then only a 'R' 'A' or 'V' tag is emitted as a marker of some form
is necessary.
All of this specialized behaviour can be bypassed by setting Purity() to
FALSE, in which case the output will look very similar to what
Data::Dumper outputs in low Purity setting.
In a later version I'll try to expand this section with more examples.
=head2 A Note About Speed
Data::Dumper is much faster than this module for many things. However IMO
it is less readable, and definitely less accurate. YMMV.
=head1 EXPORT
By default exports the Dump() command. Or may export on request the same
command as Stream(). A Data::Dumper::Dumper compatibility routine is
provided via requesting Dumper and access to the real Data::Dumper::Dumper
routine is provided via DDumper. The later two are exported together with
the :Dumper tag.
Additionally there are a set of internally used routines that are exposed.
These are mostly direct copies of routines from Array::RefElem,
Lexical::Alias and Scalar::Util, however some where marked have had their
semantics slightly changed, returning defined but false instead of undef
for negative checks, or throwing errors on failure.
The following XS subs (and tagnames for various groupings) are exportable
on request.
:Dumper
Dumper
DDumper
:undump # Collection of routines needed to undump something
alias_av # aliases a given array value to a scalar
alias_hv # aliases a given hashes value to a scalar
alias_ref # aliases a scalar to another scalar
make_ro # makes a scalar read only
lock_keys # pass through to Hash::Util::lock_keys
lock_keys_plus # like lock_keys, but adds keys to those present
lock_ref_keys # like lock_keys but operates on a hashref
lock_ref_keys_plus # like lock_keys_plus but operates on a hashref
dualvar # make a variable with different string/numeric
# representation
alias_to # pretend to return an alias, used in low
# purity mode to indicate a value is actually
# an alias to something else.
:alias # all croak on failure
alias_av(@Array,$index,$var);
alias_hv(%hash,$key,$var);
alias_ref(\$var1,\$var2);
push_alias(@array,$var);
:util
blessed($var) #undef or a class name.
isweak($var) #returns true if $var contains a weakref
reftype($var) #the underlying type or false but defined.
refaddr($var) #a references address
refcount($var) #the number of times a reference is referenced
sv_refcount($var) #the number of times a scalar is referenced.
weak_refcount($var) #the number of weakrefs to an object.
#sv_refcount($var)-weak_refcount($var) is the true
#SvREFCOUNT() of the var.
looks_like_number($var) #if perl will think this is a number.
regex($var) # In list context returns the pattern and the modifiers,
# in scalar context returns the pattern in (?msix:) form.
# If not a regex returns false.
readonly($var) # returns whether the $var is readonly
weaken($var) # cause the reference contained in var to become weak.
make_ro($var) # causes $var to become readonly, returns the value of $var.
reftype_or_glob # returns the reftype of a reference, or if its not
# a reference but a glob then the globs name
refaddr_or_glob # similar to reftype_or_glob but returns an address
# in the case of a reference.
globname # returns an evalable string to represent a glob, or
# the empty string if not a glob.
:all # (Dump() and Stream() and Dumper() and DDumper()
# and all of the XS)
:bin # (not Dump() but all of the rest of the XS)
By default exports only Dump(), DumpLex() and DumpVars(). Tags are
provided for exporting 'all' subroutines, as well as 'bin' (not Dump()),
'util' (only introspection utilities) and 'alias' for the aliasing
utilities. If you need to ensure that you can eval the results (undump)
then use the 'undump' tag.
=head1 BUGS
Code with this many debug statements is certain to have errors. :-)
Please report them with as much of the error output as possible.
Be aware that to a certain extent this module is subject to whimsies of
your local perl. The same code may not produce the same dump on two
different installs and versions. Luckily these don't seem to pop up often.
=head1 AUTHOR AND COPYRIGHT
Yves Orton, yves at cpan org.
Copyright (C) 2003-2005 Yves Orton
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Contains code derived from works by Gisle Aas, Graham Barr, Jeff Pinyan,
Richard Clamp, and Gurusamy Sarathy.
Thanks to Dan Brook, Yitzchak Scott-Thoennes, eric256, Joshua ben
Jore, Jim Cromie, Curtis "Ovid" Poe, Lars Dɪᴇᴄᴋᴏᴡ, and anybody that
I've forgotten for patches, feedback and ideas.
=head1 SEE ALSO (its a crowded space, isn't it!)
L<Data::Dumper>
- the mother of them all
L<Data::Dumper::Simple>
- Auto named vars with source filter interface.
L<Data::Dumper::Names>
- Auto named vars without source filtering.
L<Data::Dumper::EasyOO>
- easy to use wrapper for DD
L<Data::Dump>
- Has cool feature to squeeze data
L<Data::Dump::Streamer>
- The best perl dumper. But I would say that. :-)
L<Data::TreeDumper>
- Non perl output, lots of rendering options
And of course L<www.perlmonks.org> and L<perl> itself.
=cut
|