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 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503
|
---
layout: default
built_from_commit: f78896bb8131670bca861ce0bbbd06ae1a8e36f8
title: Built-in function reference
canonical: "/puppet/latest/function.html"
toc_levels: 2
toc: columns
---
# Built-in function reference
> **NOTE:** This page was generated from the Puppet source code on 2024-08-29 16:24:44 -0700
This page is a list of Puppet's built-in functions, with descriptions of what they do and how to use them.
Functions are plugins you can call during catalog compilation. A call to any function is an expression that resolves to a value. For more information on how to call functions, see [the language reference page about function calls.](lang_functions.dita)
Many of these function descriptions include auto-detected _signatures,_ which are short reminders of the function's allowed arguments. These signatures aren't identical to the syntax you use to call the function; instead, they resemble a parameter list from a Puppet [class](lang_classes.dita), [defined resource type](lang_defined_types.dita), [function](lang_write_functions_in_puppet.dita), or [lambda](lang_lambdas.dita). The syntax of a signature is:
```
<FUNCTION NAME>(<DATA TYPE> <ARGUMENT NAME>, ...)
```
The `<DATA TYPE>` is a [Puppet data type value](lang_data_type.dita), like `String` or `Optional[Array[String]]`. The `<ARGUMENT NAME>` is a descriptive name chosen by the function's author to indicate what the argument is used for.
* Any arguments with an `Optional` data type can be omitted from the function call.
* Arguments that start with an asterisk (like `*$values`) can be repeated any number of times.
* Arguments that start with an ampersand (like `&$block`) aren't normal arguments; they represent a code block, provided with [Puppet's lambda syntax.](lang_lambdas.dita)
## `undef` values in Puppet 6
In Puppet 6, many Puppet types were moved out of the Puppet codebase, and into modules on the Puppet Forge. The new functions handle `undef` values more strictly than their stdlib counterparts. In Puppet 6, code that relies on `undef` values being implicitly treated as other types will return an evaluation error. For more information on which types were moved into modules, see the [Puppet 6 release notes](https://puppet.com/docs/puppet/6.0/release_notes_puppet.html#select-types-moved-to-modules).
## `abs`
Returns the absolute value of a Numeric value, for example -34.56 becomes
34.56. Takes a single `Integer` or `Float` value as an argument.
*Deprecated behavior*
For backwards compatibility reasons this function also works when given a
number in `String` format such that it first attempts to covert it to either a `Float` or
an `Integer` and then taking the absolute value of the result. Only strings representing
a number in decimal format is supported - an error is raised if
value is not decimal (using base 10). Leading 0 chars in the string
are ignored. A floating point value in string form can use some forms of
scientific notation but not all.
Callers should convert strings to `Numeric` before calling
this function to have full control over the conversion.
```puppet
abs(Numeric($str_val))
```
It is worth noting that `Numeric` can convert to absolute value
directly as in the following examples:
```puppet
Numeric($strval, true) # Converts to absolute Integer or Float
Integer($strval, 10, true) # Converts to absolute Integer using base 10 (decimal)
Integer($strval, 16, true) # Converts to absolute Integer using base 16 (hex)
Float($strval, true) # Converts to absolute Float
```
Signature 1
`abs(Numeric $val)`
Signature 2
`abs(String $val)`
## `alert`
Logs a message on the server at level `alert`.
`alert(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `all`
Runs a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
repeatedly using each value in a data structure until the lambda returns a non "truthy" value which
makes the function return `false`, or if the end of the iteration is reached, `true` is returned.
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can
request one or two parameters.
`$data.all |$parameter| { <PUPPET CODE BLOCK> }`
or
`all($data) |$parameter| { <PUPPET CODE BLOCK> }`
```puppet
# For the array $data, run a lambda that checks that all values are multiples of 10
$data = [10, 20, 30]
notice $data.all |$item| { $item % 10 == 0 }
```
Would notice `true`.
When the first argument is a `Hash`, Puppet passes each key and value pair to the lambda
as an array in the form `[key, value]`.
```puppet
# For the hash $data, run a lambda using each item as a key-value array
$data = { 'a_0'=> 10, 'b_1' => 20 }
notice $data.all |$item| { $item[1] % 10 == 0 }
```
Would notice `true` if all values in the hash are multiples of 10.
When the lambda accepts two arguments, the first argument gets the index in an array
or the key from a hash, and the second argument the value.
```puppet
# Check that all values are a multiple of 10 and keys start with 'abc'
$data = {abc_123 => 10, abc_42 => 20, abc_blue => 30}
notice $data.all |$key, $value| { $value % 10 == 0 and $key =~ /^abc/ }
```
Would notice `true`.
For an general examples that demonstrates iteration, see the Puppet
[iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
documentation.
Signature 1
`all(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`all(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`all(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`all(Iterable $enumerable, Callable[1,1] &$block)`
## `annotate`
Handles annotations on objects. The function can be used in four different ways.
With two arguments, an `Annotation` type and an object, the function returns the annotation
for the object of the given type, or `undef` if no such annotation exists.
```puppet
$annotation = Mod::NickNameAdapter.annotate(o)
$annotation = annotate(Mod::NickNameAdapter.annotate, o)
```
With three arguments, an `Annotation` type, an object, and a block, the function returns the
annotation for the object of the given type, or annotates it with a new annotation initialized
from the hash returned by the given block when no such annotation exists. The block will not
be called when an annotation of the given type is already present.
```puppet
$annotation = Mod::NickNameAdapter.annotate(o) || { { 'nick_name' => 'Buddy' } }
$annotation = annotate(Mod::NickNameAdapter.annotate, o) || { { 'nick_name' => 'Buddy' } }
```
With three arguments, an `Annotation` type, an object, and an `Hash`, the function will annotate
the given object with a new annotation of the given type that is initialized from the given hash.
An existing annotation of the given type is discarded.
```puppet
$annotation = Mod::NickNameAdapter.annotate(o, { 'nick_name' => 'Buddy' })
$annotation = annotate(Mod::NickNameAdapter.annotate, o, { 'nick_name' => 'Buddy' })
```
With three arguments, an `Annotation` type, an object, and an the string `clear`, the function will
clear the annotation of the given type in the given object. The old annotation is returned if
it existed.
```puppet
$annotation = Mod::NickNameAdapter.annotate(o, clear)
$annotation = annotate(Mod::NickNameAdapter.annotate, o, clear)
```
With three arguments, the type `Pcore`, an object, and a Hash of hashes keyed by `Annotation` types,
the function will annotate the given object with all types used as keys in the given hash. Each annotation
is initialized with the nested hash for the respective type. The annotated object is returned.
```puppet
$person = Pcore.annotate(Mod::Person({'name' => 'William'}), {
Mod::NickNameAdapter >= { 'nick_name' => 'Bill' },
Mod::HobbiesAdapter => { 'hobbies' => ['Ham Radio', 'Philatelist'] }
})
```
Signature 1
`annotate(Type[Annotation] $type, Any $value, Optional[Callable[0, 0]] &$block)`
Signature 2
`annotate(Type[Annotation] $type, Any $value, Variant[Enum[clear],Hash[Pcore::MemberName,Any]] $annotation_hash)`
Signature 3
`annotate(Type[Pcore] $type, Any $value, Hash[Type[Annotation], Hash[Pcore::MemberName,Any]] $annotations)`
## `any`
Runs a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
repeatedly using each value in a data structure until the lambda returns a "truthy" value which
makes the function return `true`, or if the end of the iteration is reached, false is returned.
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can
request one or two parameters.
`$data.any |$parameter| { <PUPPET CODE BLOCK> }`
or
`any($data) |$parameter| { <PUPPET CODE BLOCK> }`
```puppet
# For the array $data, run a lambda that checks if an unknown hash contains those keys
$data = ["routers", "servers", "workstations"]
$looked_up = lookup('somekey', Hash)
notice $data.any |$item| { $looked_up[$item] }
```
Would notice `true` if the looked up hash had a value that is neither `false` nor `undef` for at least
one of the keys. That is, it is equivalent to the expression
`$looked_up[routers] || $looked_up[servers] || $looked_up[workstations]`.
When the first argument is a `Hash`, Puppet passes each key and value pair to the lambda
as an array in the form `[key, value]`.
```puppet
# For the hash $data, run a lambda using each item as a key-value array.
$data = {"rtr" => "Router", "svr" => "Server", "wks" => "Workstation"}
$looked_up = lookup('somekey', Hash)
notice $data.any |$item| { $looked_up[$item[0]] }
```
Would notice `true` if the looked up hash had a value for one of the wanted key that is
neither `false` nor `undef`.
When the lambda accepts two arguments, the first argument gets the index in an array
or the key from a hash, and the second argument the value.
```puppet
# Check if there is an even numbered index that has a non String value
$data = [key1, 1, 2, 2]
notice $data.any |$index, $value| { $index % 2 == 0 and $value !~ String }
```
Would notice true as the index `2` is even and not a `String`
For an general examples that demonstrates iteration, see the Puppet
[iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
documentation.
Signature 1
`any(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`any(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`any(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`any(Iterable $enumerable, Callable[1,1] &$block)`
## `assert_type`
Returns the given value if it is of the given
[data type](https://puppet.com/docs/puppet/latest/lang_data.html), or
otherwise either raises an error or executes an optional two-parameter
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html).
The function takes two mandatory arguments, in this order:
1. The expected data type.
2. A value to compare against the expected data type.
```puppet
$raw_username = 'Amy Berry'
# Assert that $raw_username is a non-empty string and assign it to $valid_username.
$valid_username = assert_type(String[1], $raw_username)
# $valid_username contains "Amy Berry".
# If $raw_username was an empty string or a different data type, the Puppet run would
# fail with an "Expected type does not match actual" error.
```
You can use an optional lambda to provide enhanced feedback. The lambda takes two
mandatory parameters, in this order:
1. The expected data type as described in the function's first argument.
2. The actual data type of the value.
```puppet
$raw_username = 'Amy Berry'
# Assert that $raw_username is a non-empty string and assign it to $valid_username.
# If it isn't, output a warning describing the problem and use a default value.
$valid_username = assert_type(String[1], $raw_username) |$expected, $actual| {
warning( "The username should be \'${expected}\', not \'${actual}\'. Using 'anonymous'." )
'anonymous'
}
# $valid_username contains "Amy Berry".
# If $raw_username was an empty string, the Puppet run would set $valid_username to
# "anonymous" and output a warning: "The username should be 'String[1, default]', not
# 'String[0, 0]'. Using 'anonymous'."
```
For more information about data types, see the
[documentation](https://puppet.com/docs/puppet/latest/lang_data.html).
Signature 1
`assert_type(Type $type, Any $value, Optional[Callable[Type, Type]] &$block)`
Signature 2
`assert_type(String $type_string, Any $value, Optional[Callable[Type, Type]] &$block)`
## `binary_file`
Loads a binary file from a module or file system and returns its contents as a `Binary`.
The argument to this function should be a `<MODULE NAME>/<FILE>`
reference, which will load `<FILE>` from a module's `files`
directory. (For example, the reference `mysql/mysqltuner.pl` will load the
file `<MODULES DIRECTORY>/mysql/files/mysqltuner.pl`.)
This function also accepts an absolute file path that allows reading
binary file content from anywhere on disk.
An error is raised if the given file does not exists.
To search for the existence of files, use the `find_file()` function.
- since 4.8.0
`binary_file(String $path)`
## `break`
Breaks an innermost iteration as if it encountered an end of input.
This function does not return to the caller.
The signal produced to stop the iteration bubbles up through
the call stack until either terminating the innermost iteration or
raising an error if the end of the call stack is reached.
The break() function does not accept an argument.
```puppet
$data = [1,2,3]
notice $data.map |$x| { if $x == 3 { break() } $x*10 }
```
Would notice the value `[10, 20]`
```puppet
function break_if_even($x) {
if $x % 2 == 0 { break() }
}
$data = [1,2,3]
notice $data.map |$x| { break_if_even($x); $x*10 }
```
Would notice the value `[10]`
* Also see functions `next` and `return`
`break()`
## `call`
Calls an arbitrary Puppet function by name.
This function takes one mandatory argument and one or more optional arguments:
1. A string corresponding to a function name.
2. Any number of arguments to be passed to the called function.
3. An optional lambda, if the function being called supports it.
This function can also be used to resolve a `Deferred` given as
the only argument to the function (does not accept arguments nor
a block).
```puppet
$a = 'notice'
call($a, 'message')
```
```puppet
$a = 'each'
$b = [1,2,3]
call($a, $b) |$item| {
notify { $item: }
}
```
The `call` function can be used to call either Ruby functions or Puppet language
functions.
When used with `Deferred` values, the deferred value can either describe
a function call, or a dig into a variable.
```puppet
$d = Deferred('join', [[1,2,3], ':']) # A future call to join that joins the arguments 1,2,3 with ':'
notice($d.call())
```
Would notice the string "1:2:3".
```puppet
$d = Deferred('$facts', ['processors', 'count'])
notice($d.call())
```
Would notice the value of `$facts['processors']['count']` at the time when the `call` is made.
* Deferred values supported since Puppet 6.0
Signature 1
`call(String $function_name, Any *$arguments, Optional[Callable] &$block)`
Signature 2
`call(Deferred $deferred)`
## `camelcase`
Creates a Camel Case version of a String
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion replaces all combinations of `*_<char>*` with an upcased version of the
character following the _. This is done using Ruby system locale which handles some, but not all
special international up-casing rules (for example German double-s ß is upcased to "Ss").
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is capitalized and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
* The result will not contain any underscore characters.
Please note: This function relies directly on Ruby's String implementation and as such may not be entirely UTF8 compatible.
To ensure best compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
```puppet
'hello_friend'.camelcase()
camelcase('hello_friend')
```
Would both result in `"HelloFriend"`
```puppet
['abc_def', 'bcd_xyz'].camelcase()
camelcase(['abc_def', 'bcd_xyz'])
```
Would both result in `['AbcDef', 'BcdXyz']`
Signature 1
`camelcase(Numeric $arg)`
Signature 2
`camelcase(String $arg)`
Signature 3
`camelcase(Iterable[Variant[String, Numeric]] $arg)`
## `capitalize`
Capitalizes the first character of a String, or the first character of every String in an Iterable value (such as an Array).
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String`, a string is returned in which the first character is uppercase.
This is done using Ruby system locale which handles some, but not all
special international up-casing rules (for example German double-s ß is capitalized to "Ss").
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is capitalized and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
Please note: This function relies directly on Ruby's String implementation and as such may not be entirely UTF8 compatible.
To ensure best compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
```puppet
'hello'.capitalize()
capitalize('hello')
```
Would both result in `"Hello"`
```puppet
['abc', 'bcd'].capitalize()
capitalize(['abc', 'bcd'])
```
Would both result in `['Abc', 'Bcd']`
Signature 1
`capitalize(Numeric $arg)`
Signature 2
`capitalize(String $arg)`
Signature 3
`capitalize(Iterable[Variant[String, Numeric]] $arg)`
## `ceiling`
Returns the smallest `Integer` greater or equal to the argument.
Takes a single numeric value as an argument.
This function is backwards compatible with the same function in stdlib
and accepts a `Numeric` value. A `String` that can be converted
to a floating point number can also be used in this version - but this
is deprecated.
In general convert string input to `Numeric` before calling this function
to have full control over how the conversion is done.
Signature 1
`ceiling(Numeric $val)`
Signature 2
`ceiling(String $val)`
## `chomp`
Returns a new string with the record separator character(s) removed.
The record separator is the line ending characters `\r` and `\n`.
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion removes `\r\n`, `\n` or `\r` from the end of a string.
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
```puppet
"hello\r\n".chomp()
chomp("hello\r\n")
```
Would both result in `"hello"`
```puppet
["hello\r\n", "hi\r\n"].chomp()
chomp(["hello\r\n", "hi\r\n"])
```
Would both result in `['hello', 'hi']`
Signature 1
`chomp(Numeric $arg)`
Signature 2
`chomp(String $arg)`
Signature 3
`chomp(Iterable[Variant[String, Numeric]] $arg)`
## `chop`
Returns a new string with the last character removed.
If the string ends with `\r\n`, both characters are removed. Applying chop to an empty
string returns an empty string. If you wish to merely remove record
separators then you should use the `chomp` function.
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion removes the last character, or if it ends with \r\n` it removes both. If String is empty
an empty string is returned.
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
```puppet
"hello\r\n".chop()
chop("hello\r\n")
```
Would both result in `"hello"`
```puppet
"hello".chop()
chop("hello")
```
Would both result in `"hell"`
```puppet
["hello\r\n", "hi\r\n"].chop()
chop(["hello\r\n", "hi\r\n"])
```
Would both result in `['hello', 'hi']`
Signature 1
`chop(Numeric $arg)`
Signature 2
`chop(String $arg)`
Signature 3
`chop(Iterable[Variant[String, Numeric]] $arg)`
## `compare`
Compares two values and returns -1, 0 or 1 if first value is smaller, equal or larger than the second value.
The compare function accepts arguments of the data types `String`, `Numeric`, `Timespan`, `Timestamp`, and `Semver`,
such that:
* two of the same data type can be compared
* `Timespan` and `Timestamp` can be compared with each other and with `Numeric`
When comparing two `String` values the comparison can be made to consider case by passing a third (optional)
boolean `false` value - the default is `true` which ignores case as the comparison operators
in the Puppet Language.
Signature 1
`compare(Numeric $a, Numeric $b)`
Signature 2
`compare(String $a, String $b, Optional[Boolean] $ignore_case)`
Signature 3
`compare(Semver $a, Semver $b)`
Signature 4
`compare(Numeric $a, Variant[Timespan, Timestamp] $b)`
Signature 5
`compare(Timestamp $a, Variant[Timestamp, Numeric] $b)`
Signature 6
`compare(Timespan $a, Variant[Timespan, Numeric] $b)`
## `contain`
Makes one or more classes be contained inside the current class.
If any of these classes are undeclared, they will be declared as if
there were declared with the `include` function.
Accepts a class name, an array of class names, or a comma-separated
list of class names.
A contained class will not be applied before the containing class is
begun, and will be finished before the containing class is finished.
You must use the class's full name;
relative names are not allowed. In addition to names in string form,
you may also directly use `Class` and `Resource` `Type`-values that are produced by
evaluating resource and relationship expressions.
The function returns an array of references to the classes that were contained thus
allowing the function call to `contain` to directly continue.
- Since 4.0.0 support for `Class` and `Resource` `Type`-values, absolute names
- Since 4.7.0 a value of type `Array[Type[Class[n]]]` is returned with all the contained classes
`contain(Any *$names)`
## `convert_to`
The `convert_to(value, type)` is a convenience function that does the same as `new(type, value)`.
The difference in the argument ordering allows it to be used in chained style for
improved readability "left to right".
When the function is given a lambda, it is called with the converted value, and the function
returns what the lambda returns, otherwise the converted value.
```puppet
# The harder to read variant:
# Using new operator - that is "calling the type" with operator ()
Hash(Array("abc").map |$i,$v| { [$i, $v] })
# The easier to read variant:
# using 'convert_to'
"abc".convert_to(Array).map |$i,$v| { [$i, $v] }.convert_to(Hash)
```
`convert_to(Any $value, Type $type, Optional[Any] *$args, Optional[Callable[1,1]] &$block)`
## `create_resources`
Converts a hash into a set of resources and adds them to the catalog.
**Note**: Use this function selectively. It's generally better to write resources in
[Puppet](https://puppet.com/docs/puppet/latest/lang_resources.html), as
resources created with `create_resource` are difficult to read and troubleshoot.
This function takes two mandatory arguments: a resource type, and a hash describing
a set of resources. The hash should be in the form `{title => {parameters} }`:
# A hash of user resources:
$myusers = {
'nick' => { uid => '1330',
gid => allstaff,
groups => ['developers', 'operations', 'release'], },
'dan' => { uid => '1308',
gid => allstaff,
groups => ['developers', 'prosvc', 'release'], },
}
create_resources(user, $myusers)
A third, optional parameter may be given, also as a hash:
$defaults = {
'ensure' => present,
'provider' => 'ldap',
}
create_resources(user, $myusers, $defaults)
The values given on the third argument are added to the parameters of each resource
present in the set given on the second argument. If a parameter is present on both
the second and third arguments, the one on the second argument takes precedence.
This function can be used to create defined resources and classes, as well
as native resources.
Virtual and Exported resources may be created by prefixing the type name
with @ or @@ respectively. For example, the $myusers hash may be exported
in the following manner:
create_resources("@@user", $myusers)
The $myusers may be declared as virtual resources using:
create_resources("@user", $myusers)
Note that `create_resources` filters out parameter values that are `undef` so that normal
data binding and Puppet default value expressions are considered (in that order) for the
final value of a parameter (just as when setting a parameter to `undef` in a Puppet language
resource declaration).
`create_resources()`
## `crit`
Logs a message on the server at level `crit`.
`crit(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `debug`
Logs a message on the server at level `debug`.
`debug(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `defined`
Determines whether a given class or resource type is defined and returns a Boolean
value. You can also use `defined` to determine whether a specific resource is defined,
or whether a variable has a value (including `undef`, as opposed to the variable never
being declared or assigned).
This function takes at least one string argument, which can be a class name, type name,
resource reference, or variable reference of the form `'$name'`. (Note that the `$` sign
is included in the string which must be in single quotes to prevent the `$` character
to be interpreted as interpolation.
The `defined` function checks both native and defined types, including types
provided by modules. Types and classes are matched by their names. The function matches
resource declarations by using resource references.
```puppet
# Matching resource types
defined("file")
defined("customtype")
# Matching defines and classes
defined("foo")
defined("foo::bar")
# Matching variables (note the single quotes)
defined('$name')
# Matching declared resources
defined(File['/tmp/file'])
```
Puppet depends on the configuration's evaluation order when checking whether a resource
is declared.
```puppet
# Assign values to $is_defined_before and $is_defined_after using identical `defined`
# functions.
$is_defined_before = defined(File['/tmp/file'])
file { "/tmp/file":
ensure => present,
}
$is_defined_after = defined(File['/tmp/file'])
# $is_defined_before returns false, but $is_defined_after returns true.
```
This order requirement only refers to evaluation order. The order of resources in the
configuration graph (e.g. with `before` or `require`) does not affect the `defined`
function's behavior.
> **Warning:** Avoid relying on the result of the `defined` function in modules, as you
> might not be able to guarantee the evaluation order well enough to produce consistent
> results. This can cause other code that relies on the function's result to behave
> inconsistently or fail.
If you pass more than one argument to `defined`, the function returns `true` if _any_
of the arguments are defined. You can also match resources by type, allowing you to
match conditions of different levels of specificity, such as whether a specific resource
is of a specific data type.
```puppet
file { "/tmp/file1":
ensure => file,
}
$tmp_file = file { "/tmp/file2":
ensure => file,
}
# Each of these statements return `true` ...
defined(File['/tmp/file1'])
defined(File['/tmp/file1'],File['/tmp/file2'])
defined(File['/tmp/file1'],File['/tmp/file2'],File['/tmp/file3'])
# ... but this returns `false`.
defined(File['/tmp/file3'])
# Each of these statements returns `true` ...
defined(Type[Resource['file','/tmp/file2']])
defined(Resource['file','/tmp/file2'])
defined(File['/tmp/file2'])
defined('$tmp_file')
# ... but each of these returns `false`.
defined(Type[Resource['exec','/tmp/file2']])
defined(Resource['exec','/tmp/file2'])
defined(File['/tmp/file3'])
defined('$tmp_file2')
```
`defined(Variant[String, Type[CatalogEntry], Type[Type[CatalogEntry]]] *$vals)`
## `dig`
Returns a value for a sequence of given keys/indexes into a structure, such as
an array or hash.
This function is used to "dig into" a complex data structure by
using a sequence of keys / indexes to access a value from which
the next key/index is accessed recursively.
The first encountered `undef` value or key stops the "dig" and `undef` is returned.
An error is raised if an attempt is made to "dig" into
something other than an `undef` (which immediately returns `undef`), an `Array` or a `Hash`.
```puppet
$data = {a => { b => [{x => 10, y => 20}, {x => 100, y => 200}]}}
notice $data.dig('a', 'b', 1, 'x')
```
Would notice the value 100.
This is roughly equivalent to `$data['a']['b'][1]['x']`. However, a standard
index will return an error and cause catalog compilation failure if any parent
of the final key (`'x'`) is `undef`. The `dig` function will return `undef`,
rather than failing catalog compilation. This allows you to check if data
exists in a structure without mandating that it always exists.
`dig(Optional[Collection] $data, Any *$arg)`
## `digest`
Returns a hash value from a provided string using the digest_algorithm setting from the Puppet config file.
`digest()`
## `downcase`
Converts a String, Array or Hash (recursively) into lower case.
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String`, its lower case version is returned. This is done using Ruby system locale which handles some, but not all
special international up-casing rules (for example German double-s ß is upcased to "SS", whereas upper case double-s
is downcased to ß).
* For `Array` and `Hash` the conversion to lower case is recursive and each key and value must be convertible by
this function.
* When a `Hash` is converted, some keys could result in the same key - in those cases, the
latest key-value wins. For example if keys "aBC", and "abC" where both present, after downcase there would only be one
key "abc".
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
Please note: This function relies directly on Ruby's String implementation and as such may not be entirely UTF8 compatible.
To ensure best compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
```puppet
'HELLO'.downcase()
downcase('HEllO')
```
Would both result in `"hello"`
```puppet
['A', 'B'].downcase()
downcase(['A', 'B'])
```
Would both result in `['a', 'b']`
```puppet
{'A' => 'HEllO', 'B' => 'GOODBYE'}.downcase()
```
Would result in `{'a' => 'hello', 'b' => 'goodbye'}`
```puppet
['A', 'B', ['C', ['D']], {'X' => 'Y'}].downcase
```
Would result in `['a', 'b', ['c', ['d']], {'x' => 'y'}]`
Signature 1
`downcase(Numeric $arg)`
Signature 2
`downcase(String $arg)`
Signature 3
`downcase(Array[StringData] $arg)`
Signature 4
`downcase(Hash[StringData, StringData] $arg)`
## `each`
Runs a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
repeatedly using each value in a data structure, then returns the values unchanged.
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can
request one or two parameters.
`$data.each |$parameter| { <PUPPET CODE BLOCK> }`
or
`each($data) |$parameter| { <PUPPET CODE BLOCK> }`
When the first argument (`$data` in the above example) is an array, Puppet passes each
value in turn to the lambda, then returns the original values.
```puppet
# For the array $data, run a lambda that creates a resource for each item.
$data = ["routers", "servers", "workstations"]
$data.each |$item| {
notify { $item:
message => $item
}
}
# Puppet creates one resource for each of the three items in $data. Each resource is
# named after the item's value and uses the item's value in a parameter.
```
When the first argument is a hash, Puppet passes each key and value pair to the lambda
as an array in the form `[key, value]` and returns the original hash.
```puppet
# For the hash $data, run a lambda using each item as a key-value array that creates a
# resource for each item.
$data = {"rtr" => "Router", "svr" => "Server", "wks" => "Workstation"}
$data.each |$items| {
notify { $items[0]:
message => $items[1]
}
}
# Puppet creates one resource for each of the three items in $data, each named after the
# item's key and containing a parameter using the item's value.
```
When the first argument is an array and the lambda has two parameters, Puppet passes the
array's indexes (enumerated from 0) in the first parameter and its values in the second
parameter.
```puppet
# For the array $data, run a lambda using each item's index and value that creates a
# resource for each item.
$data = ["routers", "servers", "workstations"]
$data.each |$index, $value| {
notify { $value:
message => $index
}
}
# Puppet creates one resource for each of the three items in $data, each named after the
# item's value and containing a parameter using the item's index.
```
When the first argument is a hash, Puppet passes its keys to the first parameter and its
values to the second parameter.
```puppet
# For the hash $data, run a lambda using each item's key and value to create a resource
# for each item.
$data = {"rtr" => "Router", "svr" => "Server", "wks" => "Workstation"}
$data.each |$key, $value| {
notify { $key:
message => $value
}
}
# Puppet creates one resource for each of the three items in $data, each named after the
# item's key and containing a parameter using the item's value.
```
For an example that demonstrates how to create multiple `file` resources using `each`,
see the Puppet
[iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
documentation.
Signature 1
`each(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`each(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`each(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`each(Iterable $enumerable, Callable[1,1] &$block)`
## `emerg`
Logs a message on the server at level `emerg`.
`emerg(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `empty`
Returns `true` if the given argument is an empty collection of values.
This function can answer if one of the following is empty:
* `Array`, `Hash` - having zero entries
* `String`, `Binary` - having zero length
For backwards compatibility with the stdlib function with the same name the
following data types are also accepted by the function instead of raising an error.
Using these is deprecated and will raise a warning:
* `Numeric` - `false` is returned for all `Numeric` values.
* `Undef` - `true` is returned for all `Undef` values.
```puppet
notice([].empty)
notice(empty([]))
# would both notice 'true'
```
Signature 1
`empty(Collection $coll)`
Signature 2
`empty(Sensitive[String] $str)`
Signature 3
`empty(String $str)`
Signature 4
`empty(Numeric $num)`
Signature 5
`empty(Binary $bin)`
Signature 6
`empty(Undef $x)`
## `epp`
Evaluates an Embedded Puppet (EPP) template file and returns the rendered text
result as a String.
`epp('<MODULE NAME>/<TEMPLATE FILE>', <PARAMETER HASH>)`
The first argument to this function should be a `<MODULE NAME>/<TEMPLATE FILE>`
reference, which loads `<TEMPLATE FILE>` from `<MODULE NAME>`'s `templates`
directory. In most cases, the last argument is optional; if used, it should be a
[hash](https://puppet.com/docs/puppet/latest/lang_data_hash.html) that contains parameters to
pass to the template.
- See the [template](https://puppet.com/docs/puppet/latest/lang_template.html)
documentation for general template usage information.
- See the [EPP syntax](https://puppet.com/docs/puppet/latest/lang_template_epp.html)
documentation for examples of EPP.
For example, to call the apache module's `templates/vhost/_docroot.epp`
template and pass the `docroot` and `virtual_docroot` parameters, call the `epp`
function like this:
`epp('apache/vhost/_docroot.epp', { 'docroot' => '/var/www/html',
'virtual_docroot' => '/var/www/example' })`
This function can also accept an absolute path, which can load a template file
from anywhere on disk.
Puppet produces a syntax error if you pass more parameters than are declared in
the template's parameter tag. When passing parameters to a template that
contains a parameter tag, use the same names as the tag's declared parameters.
Parameters are required only if they are declared in the called template's
parameter tag without default values. Puppet produces an error if the `epp`
function fails to pass any required parameter.
`epp(String $path, Optional[Hash[Pattern[/^\w+$/], Any]] $parameters)`
## `err`
Logs a message on the server at level `err`.
`err(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `eyaml_lookup_key`
The `eyaml_lookup_key` is a hiera 5 `lookup_key` data provider function.
See [the configuration guide documentation](https://puppet.com/docs/puppet/latest/hiera_config_yaml_5.html#configuring-a-hierarchy-level-hiera-eyaml) for
how to use this function.
`eyaml_lookup_key(String[1] $key, Hash[String[1],Any] $options, Puppet::LookupContext $context)`
## `fail`
Fail with a parse error. Any parameters will be stringified,
concatenated, and passed to the exception-handler.
`fail()`
## `file`
Loads a file from a module and returns its contents as a string.
The argument to this function should be a `<MODULE NAME>/<FILE>`
reference, which will load `<FILE>` from a module's `files`
directory. (For example, the reference `mysql/mysqltuner.pl` will load the
file `<MODULES DIRECTORY>/mysql/files/mysqltuner.pl`.)
This function can also accept:
* An absolute path, which can load a file from anywhere on disk.
* Multiple arguments, which will return the contents of the **first** file
found, skipping any files that don't exist.
`file()`
## `filter`
Applies a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
to every value in a data structure and returns an array or hash containing any elements
for which the lambda evaluates to a truthy value (not `false` or `undef`).
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can
request one or two parameters.
`$filtered_data = $data.filter |$parameter| { <PUPPET CODE BLOCK> }`
or
`$filtered_data = filter($data) |$parameter| { <PUPPET CODE BLOCK> }`
When the first argument (`$data` in the above example) is an array, Puppet passes each
value in turn to the lambda and returns an array containing the results.
```puppet
# For the array $data, return an array containing the values that end with "berry"
$data = ["orange", "blueberry", "raspberry"]
$filtered_data = $data.filter |$items| { $items =~ /berry$/ }
# $filtered_data = [blueberry, raspberry]
```
When the first argument is a hash, Puppet passes each key and value pair to the lambda
as an array in the form `[key, value]` and returns a hash containing the results.
```puppet
# For the hash $data, return a hash containing all values of keys that end with "berry"
$data = { "orange" => 0, "blueberry" => 1, "raspberry" => 2 }
$filtered_data = $data.filter |$items| { $items[0] =~ /berry$/ }
# $filtered_data = {blueberry => 1, raspberry => 2}
```
When the first argument is an array and the lambda has two parameters, Puppet passes the
array's indexes (enumerated from 0) in the first parameter and its values in the second
parameter.
```puppet
# For the array $data, return an array of all keys that both end with "berry" and have
# an even-numbered index
$data = ["orange", "blueberry", "raspberry"]
$filtered_data = $data.filter |$indexes, $values| { $indexes % 2 == 0 and $values =~ /berry$/ }
# $filtered_data = [raspberry]
```
When the first argument is a hash, Puppet passes its keys to the first parameter and its
values to the second parameter.
```puppet
# For the hash $data, return a hash of all keys that both end with "berry" and have
# values less than or equal to 1
$data = { "orange" => 0, "blueberry" => 1, "raspberry" => 2 }
$filtered_data = $data.filter |$keys, $values| { $keys =~ /berry$/ and $values <= 1 }
# $filtered_data = {blueberry => 1}
```
Signature 1
`filter(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`filter(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`filter(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`filter(Iterable $enumerable, Callable[1,1] &$block)`
## `find_file`
Finds an existing file from a module and returns its path.
This function accepts an argument that is a String as a `<MODULE NAME>/<FILE>`
reference, which searches for `<FILE>` relative to a module's `files`
directory. (For example, the reference `mysql/mysqltuner.pl` will search for the
file `<MODULES DIRECTORY>/mysql/files/mysqltuner.pl`.)
If this function is run via puppet agent, it checks for file existence on the
Puppet Primary server. If run via puppet apply, it checks on the local host.
In both cases, the check is performed before any resources are changed.
This function can also accept:
* An absolute String path, which checks for the existence of a file from anywhere on disk.
* Multiple String arguments, which returns the path of the **first** file
found, skipping nonexistent files.
* An array of string paths, which returns the path of the **first** file
found from the given paths in the array, skipping nonexistent files.
The function returns `undef` if none of the given paths were found.
Signature 1
`find_file(String *$paths)`
Signature 2
`find_file(Array[String] *$paths_array)`
## `find_template`
Finds an existing template from a module and returns its path.
This function accepts an argument that is a String as a `<MODULE NAME>/<TEMPLATE>`
reference, which searches for `<TEMPLATE>` relative to a module's `templates`
directory on the primary server. (For example, the reference `mymod/secret.conf.epp`
will search for the file `<MODULES DIRECTORY>/mymod/templates/secret.conf.epp`.)
The primary use case is for agent-side template rendering with late-bound variables
resolved, such as from secret stores inaccessible to the primary server, such as
```
$variables = {
'password' => Deferred('vault_lookup::lookup',
['secret/mymod', 'https://vault.example.com:8200']),
}
# compile the template source into the catalog
file { '/etc/secrets.conf':
ensure => file,
content => Deferred('inline_epp',
[find_template('mymod/secret.conf.epp').file, $variables]),
}
```
This function can also accept:
* An absolute String path, which checks for the existence of a template from anywhere on disk.
* Multiple String arguments, which returns the path of the **first** template
found, skipping nonexistent files.
* An array of string paths, which returns the path of the **first** template
found from the given paths in the array, skipping nonexistent files.
The function returns `undef` if none of the given paths were found.
Signature 1
`find_template(String *$paths)`
Signature 2
`find_template(Array[String] *$paths_array)`
## `flatten`
Returns a flat Array produced from its possibly deeply nested given arguments.
One or more arguments of any data type can be given to this function.
The result is always a flat array representation where any nested arrays are recursively flattened.
```puppet
flatten(['a', ['b', ['c']]])
# Would return: ['a','b','c']
```
To flatten other kinds of iterables (for example hashes, or intermediate results like from a `reverse_each`)
first convert the result to an array using `Array($x)`, or `$x.convert_to(Array)`. See the `new` function
for details and options when performing a conversion.
```puppet
$hsh = { a => 1, b => 2}
# -- without conversion
$hsh.flatten()
# Would return [{a => 1, b => 2}]
# -- with conversion
$hsh.convert_to(Array).flatten()
# Would return [a,1,b,2]
flatten(Array($hsh))
# Would also return [a,1,b,2]
```
```puppet
$a1 = [1, [2, 3]]
$a2 = [[4,[5,6]]
$x = 7
flatten($a1, $a2, $x)
# would return [1,2,3,4,5,6,7]
```
```puppet
flatten(42)
# Would return [42]
flatten([42])
# Would also return [42]
```
`flatten(Any *$args)`
## `floor`
Returns the largest `Integer` less or equal to the argument.
Takes a single numeric value as an argument.
This function is backwards compatible with the same function in stdlib
and accepts a `Numeric` value. A `String` that can be converted
to a floating point number can also be used in this version - but this
is deprecated.
In general convert string input to `Numeric` before calling this function
to have full control over how the conversion is done.
Signature 1
`floor(Numeric $val)`
Signature 2
`floor(String $val)`
## `fqdn_rand`
Usage: `fqdn_rand(MAX, [SEED], [DOWNCASE])`. MAX is required and must be a positive
integer; SEED is optional and may be any number or string; DOWNCASE is optional
and should be a boolean true or false.
Generates a random Integer number greater than or equal to 0 and less than MAX,
combining the `$fqdn` fact and the value of SEED for repeatable randomness.
(That is, each node will get a different random number from this function, but
a given node's result will be the same every time unless its hostname changes.) If
DOWNCASE is true, then the `fqdn` fact will be downcased when computing the value
so that the result is not sensitive to the case of the `fqdn` fact.
This function is usually used for spacing out runs of resource-intensive cron
tasks that run on many nodes, which could cause a thundering herd or degrade
other services if they all fire at once. Adding a SEED can be useful when you
have more than one such task and need several unrelated random numbers per
node. (For example, `fqdn_rand(30)`, `fqdn_rand(30, 'expensive job 1')`, and
`fqdn_rand(30, 'expensive job 2')` will produce totally different numbers.)
`fqdn_rand()`
## `generate`
Calls an external command on the Puppet master and returns
the results of the command. Any arguments are passed to the external command as
arguments. If the generator does not exit with return code of 0,
the generator is considered to have failed and a parse error is
thrown. Generators can only have file separators, alphanumerics, dashes,
and periods in them. This function will attempt to protect you from
malicious generator calls (e.g., those with '..' in them), but it can
never be entirely safe. No subshell is used to execute
generators, so all shell metacharacters are passed directly to
the generator, and all metacharacters are returned by the function.
Consider cleaning white space from any string generated.
`generate()`
## `get`
Digs into a value with dot notation to get a value from within a structure.
**To dig into a given value**, call the function with (at least) two arguments:
* The **first** argument must be an Array, or Hash. Value can also be `undef`
(which also makes the result `undef` unless a _default value_ is given).
* The **second** argument must be a _dot notation navigation string_.
* The **optional third** argument can be any type of value and it is used
as the _default value_ if the function would otherwise return `undef`.
* An **optional lambda** for error handling taking one `Error` argument.
**Dot notation navigation string** -
The dot string consists of period `.` separated segments where each
segment is either the index into an array or the value of a hash key.
If a wanted key contains a period it must be quoted to avoid it being
taken as a segment separator. Quoting can be done with either
single quotes `'` or double quotes `"`. If a segment is
a decimal number it is converted to an Integer index. This conversion
can be prevented by quoting the value.
```puppet
#get($facts, 'os.family')
$facts.get('os.family')
```
Would both result in the value of `$facts['os']['family']`
```puppet
get([1,2,[{'name' =>'waldo'}]], '2.0.name')
```
Would result in `'waldo'`
```puppet
get([1,2,[{'name' =>'waldo'}]], '2.1.name', 'not waldo')
```
Would result in `'not waldo'`
```puppet
$x = [1, 2, { 'readme.md' => "This is a readme."}]
$x.get('2."readme.md"')
```
```puppet
$x = [1, 2, { '10' => "ten"}]
$x.get('2."0"')
```
**Error Handling** - There are two types of common errors that can
be handled by giving the function a code block to execute.
(A third kind or error; when the navigation string has syntax errors
(for example an empty segment or unbalanced quotes) will always raise
an error).
The given block will be given an instance of the `Error` data type,
and it has methods to extract `msg`, `issue_code`, `kind`, and
`details`.
The `msg` will be a preformatted message describing the error.
This is the error message that would have surfaced if there was
no block to handle the error.
The `kind` is the string `'SLICE_ERROR'` for both kinds of errors,
and the `issue_code` is either the string `'EXPECTED_INTEGER_INDEX'`
for an attempt to index into an array with a String,
or `'EXPECTED_COLLECTION'` for an attempt to index into something that
is not a Collection.
The `details` is a Hash that for both issue codes contain the
entry `'walked_path'` which is an Array with each key in the
progression of the dig up to the place where the error occurred.
For an `EXPECTED_INTEGER_INDEX`-issue the detail `'index_type'` is
set to the data type of the index value and for an
`'EXPECTED_COLLECTION'`-issue the detail `'value_type'` is set
to the type of the value.
The logic in the error handling block can inspect the details,
and either call `fail()` with a custom error message or produce
the wanted value.
If the block produces `undef` it will not be replaced with a
given default value.
```puppet
$x = 'blue'
$x.get('0.color', 'green') |$error| { undef } # result is undef
$y = ['blue']
$y.get('color', 'green') |$error| { undef } # result is undef
```
```puppet
$x = [1, 2, ['blue']]
$x.get('2.color') |$error| {
notice("Walked path is ${error.details['walked_path']}")
}
```
Would notice `Walked path is [2, color]`
Also see:
* `getvar()` that takes the first segment to be the name of a variable
and then delegates to this function.
* `dig()` function which is similar but uses an
array of navigation values instead of a dot notation string.
`get(Any $value, String $dotted_string, Optional[Any] $default_value, Optional[Callable[1,1]] &$block)`
## `getvar`
Digs into a variable with dot notation to get a value from a structure.
**To get the value from a variable** (that may or may not exist), call the function with
one or two arguments:
* The **first** argument must be a string, and must start with a variable name without leading `$`,
for example `get('facts')`. The variable name can be followed
by a _dot notation navigation string_ to dig out a value in the array or hash value
of the variable.
* The **optional second** argument can be any type of value and it is used as the
_default value_ if the function would otherwise return `undef`.
* An **optional lambda** for error handling taking one `Error` argument.
**Dot notation navigation string** -
The dot string consists of period `.` separated segments where each
segment is either the index into an array or the value of a hash key.
If a wanted key contains a period it must be quoted to avoid it being
taken as a segment separator. Quoting can be done with either
single quotes `'` or double quotes `"`. If a segment is
a decimal number it is converted to an Integer index. This conversion
can be prevented by quoting the value.
```puppet
getvar('facts') # results in the value of $facts
```
```puppet
getvar('facts.os.family') # results in the value of $facts['os']['family']
```
```puppet
$x = [1,2,[{'name' =>'waldo'}]]
getvar('x.2.1.name', 'not waldo')
# results in 'not waldo'
```
For further examples and how to perform error handling, see the `get()` function
which this function delegates to after having resolved the variable value.
`getvar(Pattern[/\A(?:::)?(?:[a-z]\w*::)*[a-z_]\w*(?:\.|\Z)/] $get_string, Optional[Any] $default_value, Optional[Callable[1,1]] &$block)`
## `group_by`
Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block
and the values are arrays of elements in the collection that correspond to the key.
Signature 1
`group_by(Collection $collection, Callable[1,1] &$block)`
### Parameters
* `collection` --- A collection of things to group.
Return type(s): `Hash`.
### Examples
Group array of strings by length, results in e.g. `{ 1 => [a, b], 2 => [ab] }`
```puppet
[a, b, ab].group_by |$s| { $s.length }
```
Group array of strings by length and index, results in e.g. `{1 => ['a'], 2 => ['b', 'ab']}`
```puppet
[a, b, ab].group_by |$i, $s| { $i%2 + $s.length }
```
Group hash iterating by key-value pair, results in e.g. `{ 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }`
```puppet
{ a => [1, 2], b => [1] }.group_by |$kv| { $kv[1].length }
```
Group hash iterating by key and value, results in e.g. `{ 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }`
```puppet
{ a => [1, 2], b => [1] }.group_by |$k, $v| { $v.length }
```
Signature 2
`group_by(Array $array, Callable[2,2] &$block)`
Signature 3
`group_by(Collection $collection, Callable[2,2] &$block)`
## `hiera`
Performs a standard priority lookup of the hierarchy and returns the most specific value
for a given key. The returned value can be any type of data.
This function is deprecated in favor of the `lookup` function. While this function
continues to work, it does **not** support:
* `lookup_options` stored in the data
* lookup across global, environment, and module layers
The function takes up to three arguments, in this order:
1. A string key that Hiera searches for in the hierarchy. **Required**.
2. An optional default value to return if Hiera doesn't find anything matching the key.
* If this argument isn't provided and this function results in a lookup failure, Puppet
fails with a compilation error.
3. The optional name of an arbitrary
[hierarchy level](https://puppet.com/docs/hiera/latest/hierarchy.html) to insert at the
top of the hierarchy. This lets you temporarily modify the hierarchy for a single lookup.
* If Hiera doesn't find a matching key in the overriding hierarchy level, it continues
searching the rest of the hierarchy.
The `hiera` function does **not** find all matches throughout a hierarchy, instead
returning the first specific value starting at the top of the hierarchy. To search
throughout a hierarchy, use the `hiera_array` or `hiera_hash` functions.
```yaml
# Assuming hiera.yaml
# :hierarchy:
# - web01.example.com
# - common
# Assuming web01.example.com.yaml:
# users:
# - "Amy Barry"
# - "Carrie Douglas"
# Assuming common.yaml:
users:
admins:
- "Edith Franklin"
- "Ginny Hamilton"
regular:
- "Iris Jackson"
- "Kelly Lambert"
```
```puppet
# Assuming we are not web01.example.com:
$users = hiera('users', undef)
# $users contains {admins => ["Edith Franklin", "Ginny Hamilton"],
# regular => ["Iris Jackson", "Kelly Lambert"]}
```
You can optionally generate the default value with a
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html) that
takes one parameter.
```puppet
# Assuming the same Hiera data as the previous example:
$users = hiera('users') | $key | { "Key \'${key}\' not found" }
# $users contains {admins => ["Edith Franklin", "Ginny Hamilton"],
# regular => ["Iris Jackson", "Kelly Lambert"]}
# If hiera couldn't match its key, it would return the lambda result,
# "Key 'users' not found".
```
The returned value's data type depends on the types of the results. In the example
above, Hiera matches the 'users' key and returns it as a hash.
See
[the 'Using the lookup function' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html) for how to perform lookup of data.
Also see
[the 'Using the deprecated hiera functions' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html)
for more information about the Hiera 3 functions.
`hiera()`
## `hiera_array`
Finds all matches of a key throughout the hierarchy and returns them as a single flattened
array of unique values. If any of the matched values are arrays, they're flattened and
included in the results. This is called an
[array merge lookup](https://puppet.com/docs/hiera/latest/lookup_types.html#array-merge).
This function is deprecated in favor of the `lookup` function. While this function
continues to work, it does **not** support:
* `lookup_options` stored in the data
* lookup across global, environment, and module layers
The `hiera_array` function takes up to three arguments, in this order:
1. A string key that Hiera searches for in the hierarchy. **Required**.
2. An optional default value to return if Hiera doesn't find anything matching the key.
* If this argument isn't provided and this function results in a lookup failure, Puppet
fails with a compilation error.
3. The optional name of an arbitrary
[hierarchy level](https://puppet.com/docs/hiera/latest/hierarchy.html) to insert at the
top of the hierarchy. This lets you temporarily modify the hierarchy for a single lookup.
* If Hiera doesn't find a matching key in the overriding hierarchy level, it continues
searching the rest of the hierarchy.
```yaml
# Assuming hiera.yaml
# :hierarchy:
# - web01.example.com
# - common
# Assuming common.yaml:
# users:
# - 'cdouglas = regular'
# - 'efranklin = regular'
# Assuming web01.example.com.yaml:
# users: 'abarry = admin'
```
```puppet
$allusers = hiera_array('users', undef)
# $allusers contains ["cdouglas = regular", "efranklin = regular", "abarry = admin"].
```
You can optionally generate the default value with a
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html) that
takes one parameter.
```puppet
# Assuming the same Hiera data as the previous example:
$allusers = hiera_array('users') | $key | { "Key \'${key}\' not found" }
# $allusers contains ["cdouglas = regular", "efranklin = regular", "abarry = admin"].
# If hiera_array couldn't match its key, it would return the lambda result,
# "Key 'users' not found".
```
`hiera_array` expects that all values returned will be strings or arrays. If any matched
value is a hash, Puppet raises a type mismatch error.
See
[the 'Using the lookup function' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html) for how to perform lookup of data.
Also see
[the 'Using the deprecated hiera functions' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html)
for more information about the Hiera 3 functions.
`hiera_array()`
## `hiera_hash`
Finds all matches of a key throughout the hierarchy and returns them in a merged hash.
This function is deprecated in favor of the `lookup` function. While this function
continues to work, it does **not** support:
* `lookup_options` stored in the data
* lookup across global, environment, and module layers
If any of the matched hashes share keys, the final hash uses the value from the
highest priority match. This is called a
[hash merge lookup](https://puppet.com/docs/hiera/latest/lookup_types.html#hash-merge).
The merge strategy is determined by Hiera's
[`:merge_behavior`](https://puppet.com/docs/hiera/latest/configuring.html#mergebehavior)
setting.
The `hiera_hash` function takes up to three arguments, in this order:
1. A string key that Hiera searches for in the hierarchy. **Required**.
2. An optional default value to return if Hiera doesn't find anything matching the key.
* If this argument isn't provided and this function results in a lookup failure, Puppet
fails with a compilation error.
3. The optional name of an arbitrary
[hierarchy level](https://puppet.com/docs/hiera/latest/hierarchy.html) to insert at the
top of the hierarchy. This lets you temporarily modify the hierarchy for a single lookup.
* If Hiera doesn't find a matching key in the overriding hierarchy level, it continues
searching the rest of the hierarchy.
```yaml
# Assuming hiera.yaml
# :hierarchy:
# - web01.example.com
# - common
# Assuming common.yaml:
# users:
# regular:
# 'cdouglas': 'Carrie Douglas'
# Assuming web01.example.com.yaml:
# users:
# administrators:
# 'aberry': 'Amy Berry'
```
```puppet
# Assuming we are not web01.example.com:
$allusers = hiera_hash('users', undef)
# $allusers contains {regular => {"cdouglas" => "Carrie Douglas"},
# administrators => {"aberry" => "Amy Berry"}}
```
You can optionally generate the default value with a
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html) that
takes one parameter.
```puppet
# Assuming the same Hiera data as the previous example:
$allusers = hiera_hash('users') | $key | { "Key \'${key}\' not found" }
# $allusers contains {regular => {"cdouglas" => "Carrie Douglas"},
# administrators => {"aberry" => "Amy Berry"}}
# If hiera_hash couldn't match its key, it would return the lambda result,
# "Key 'users' not found".
```
`hiera_hash` expects that all values returned will be hashes. If any of the values
found in the data sources are strings or arrays, Puppet raises a type mismatch error.
See
[the 'Using the lookup function' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html) for how to perform lookup of data.
Also see
[the 'Using the deprecated hiera functions' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html)
for more information about the Hiera 3 functions.
`hiera_hash()`
## `hiera_include`
Assigns classes to a node using an
[array merge lookup](https://puppet.com/docs/hiera/latest/lookup_types.html#array-merge)
that retrieves the value for a user-specified key from Hiera's data.
This function is deprecated in favor of the `lookup` function in combination with `include`.
While this function continues to work, it does **not** support:
* `lookup_options` stored in the data
* lookup across global, environment, and module layers
```puppet
# In site.pp, outside of any node definitions and below any top-scope variables:
lookup('classes', Array[String], 'unique').include
```
The `hiera_include` function requires:
- A string key name to use for classes.
- A call to this function (i.e. `hiera_include('classes')`) in your environment's
`sites.pp` manifest, outside of any node definitions and below any top-scope variables
that Hiera uses in lookups.
- `classes` keys in the appropriate Hiera data sources, with an array for each
`classes` key and each value of the array containing the name of a class.
The function takes up to three arguments, in this order:
1. A string key that Hiera searches for in the hierarchy. **Required**.
2. An optional default value to return if Hiera doesn't find anything matching the key.
* If this argument isn't provided and this function results in a lookup failure, Puppet
fails with a compilation error.
3. The optional name of an arbitrary
[hierarchy level](https://puppet.com/docs/hiera/latest/hierarchy.html) to insert at the
top of the hierarchy. This lets you temporarily modify the hierarchy for a single lookup.
* If Hiera doesn't find a matching key in the overriding hierarchy level, it continues
searching the rest of the hierarchy.
The function uses an
[array merge lookup](https://puppet.com/docs/hiera/latest/lookup_types.html#array-merge)
to retrieve the `classes` array, so every node gets every class from the hierarchy.
```yaml
# Assuming hiera.yaml
# :hierarchy:
# - web01.example.com
# - common
# Assuming web01.example.com.yaml:
# classes:
# - apache::mod::php
# Assuming common.yaml:
# classes:
# - apache
```
```puppet
# In site.pp, outside of any node definitions and below any top-scope variables:
hiera_include('classes', undef)
# Puppet assigns the apache and apache::mod::php classes to the web01.example.com node.
```
You can optionally generate the default value with a
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html) that
takes one parameter.
```puppet
# Assuming the same Hiera data as the previous example:
# In site.pp, outside of any node definitions and below any top-scope variables:
hiera_include('classes') | $key | {"Key \'${key}\' not found" }
# Puppet assigns the apache and apache::mod::php classes to the web01.example.com node.
# If hiera_include couldn't match its key, it would return the lambda result,
# "Key 'classes' not found".
```
See
[the 'Using the lookup function' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html) for how to perform lookup of data.
Also see
[the 'Using the deprecated hiera functions' documentation](https://puppet.com/docs/puppet/latest/hiera_automatic.html)
for more information about the Hiera 3 functions.
`hiera_include()`
## `hocon_data`
The `hocon_data` is a hiera 5 `data_hash` data provider function.
See [the configuration guide documentation](https://puppet.com/docs/puppet/latest/hiera_config_yaml_5.html#configuring-a-hierarchy-level-built-in-backends) for
how to use this function.
Note that this function is not supported without a hocon library being present.
`hocon_data(Struct[{path=>String[1]}] $options, Puppet::LookupContext $context)`
## `import`
The import function raises an error when called to inform the user that import is no longer supported.
`import(Any *$args)`
## `include`
Declares one or more classes, causing the resources in them to be
evaluated and added to the catalog. Accepts a class name, an array of class
names, or a comma-separated list of class names.
The `include` function can be used multiple times on the same class and will
only declare a given class once. If a class declared with `include` has any
parameters, Puppet will automatically look up values for them in Hiera, using
`<class name>::<parameter name>` as the lookup key.
Contrast this behavior with resource-like class declarations
(`class {'name': parameter => 'value',}`), which must be used in only one place
per class and can directly set parameters. You should avoid using both `include`
and resource-like declarations with the same class.
The `include` function does not cause classes to be contained in the class
where they are declared. For that, see the `contain` function. It also
does not create a dependency relationship between the declared class and the
surrounding class; for that, see the `require` function.
You must use the class's full name;
relative names are not allowed. In addition to names in string form,
you may also directly use `Class` and `Resource` `Type`-values that are produced by
the resource and relationship expressions.
- Since < 3.0.0
- Since 4.0.0 support for class and resource type values, absolute names
- Since 4.7.0 returns an `Array[Type[Class]]` of all included classes
`include(Any *$names)`
## `index`
Returns the index (or key in a hash) to a first-found value in an `Iterable` value.
When called with a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
the lambda is called repeatedly using each value in a data structure until the lambda returns a "truthy" value which
makes the function return the index or key, or if the end of the iteration is reached, undef is returned.
This function can be called in two different ways; with a value to be searched for, or with
a lambda that determines if an entry in the iterable matches.
When called with a lambda the function takes two mandatory arguments, in this order:
1. An array, hash, string, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can request one (value) or two (index/key, value) parameters.
`$data.index |$parameter| { <PUPPET CODE BLOCK> }`
or
`index($data) |$parameter| { <PUPPET CODE BLOCK> }`
```puppet
$data = ["routers", "servers", "workstations"]
notice $data.index |$value| { $value == 'servers' } # notices 1
notice $data.index |$value| { $value == 'hosts' } # notices undef
```
```puppet
$data = {types => ["routers", "servers", "workstations"], colors => ['red', 'blue', 'green']}
notice $data.index |$value| { 'servers' in $value } # notices 'types'
notice $data.index |$value| { 'red' in $value } # notices 'colors'
```
Note that the lambda gets the value and not an array with `[key, value]` as in other
iterative functions.
Using a lambda that accepts two values works the same way. The lambda gets the index/key
as the first parameter and the value as the second parameter.
```puppet
# Find the first even numbered index that has a non String value
$data = [key1, 1, 3, 5]
notice $data.index |$idx, $value| { $idx % 2 == 0 and $value !~ String } # notices 2
```
When called on a `String`, the lambda is given each character as a value. What is typically wanted is to
find a sequence of characters which is achieved by calling the function with a value to search for instead
of giving a lambda.
```puppet
# Find first occurrence of 'ah'
$data = "blablahbleh"
notice $data.index('ah') # notices 5
```
```puppet
# Find first occurrence of 'la' or 'le'
$data = "blablahbleh"
notice $data.index(/l(a|e)/ # notices 1
```
When searching in a `String` with a given value that is neither `String` nor `Regexp` the answer is always `undef`.
When searching in any other iterable, the value is matched against each value in the iteration using strict
Ruby `==` semantics. If Puppet Language semantics are wanted (where string compare is case insensitive) use a
lambda and the `==` operator in Puppet.
```puppet
$data = ['routers', 'servers', 'WORKstations']
notice $data.index('servers') # notices 1
notice $data.index('workstations') # notices undef (not matching case)
```
For an general examples that demonstrates iteration, see the Puppet
[iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
documentation.
Signature 1
`index(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`index(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`index(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`index(Iterable $enumerable, Callable[1,1] &$block)`
Signature 5
`index(String $str, Variant[String,Regexp] $match)`
Signature 6
`index(Iterable $enumerable, Any $match)`
## `info`
Logs a message on the server at level `info`.
`info(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `inline_epp`
Evaluates an Embedded Puppet (EPP) template string and returns the rendered
text result as a String.
`inline_epp('<EPP TEMPLATE STRING>', <PARAMETER HASH>)`
The first argument to this function should be a string containing an EPP
template. In most cases, the last argument is optional; if used, it should be a
[hash](https://puppet.com/docs/puppet/latest/lang_data_hash.html) that contains parameters to
pass to the template.
- See the [template](https://puppet.com/docs/puppet/latest/lang_template.html)
documentation for general template usage information.
- See the [EPP syntax](https://puppet.com/docs/puppet/latest/lang_template_epp.html)
documentation for examples of EPP.
For example, to evaluate an inline EPP template and pass it the `docroot` and
`virtual_docroot` parameters, call the `inline_epp` function like this:
`inline_epp('docroot: <%= $docroot %> Virtual docroot: <%= $virtual_docroot %>',
{ 'docroot' => '/var/www/html', 'virtual_docroot' => '/var/www/example' })`
Puppet produces a syntax error if you pass more parameters than are declared in
the template's parameter tag. When passing parameters to a template that
contains a parameter tag, use the same names as the tag's declared parameters.
Parameters are required only if they are declared in the called template's
parameter tag without default values. Puppet produces an error if the
`inline_epp` function fails to pass any required parameter.
An inline EPP template should be written as a single-quoted string or
[heredoc](https://puppet.com/docs/puppet/latest/lang_data_string.html#heredocs).
A double-quoted string is subject to expression interpolation before the string
is parsed as an EPP template.
For example, to evaluate an inline EPP template using a heredoc, call the
`inline_epp` function like this:
```puppet
# Outputs 'Hello given argument planet!'
inline_epp(@(END), { x => 'given argument' })
<%- | $x, $y = planet | -%>
Hello <%= $x %> <%= $y %>!
END
```
`inline_epp(String $template, Optional[Hash[Pattern[/^\w+$/], Any]] $parameters)`
## `inline_template`
Evaluate a template string and return its value. See
[the templating docs](https://puppet.com/docs/puppet/latest/lang_template.html) for
more information. Note that if multiple template strings are specified, their
output is all concatenated and returned as the output of the function.
`inline_template()`
## `join`
Joins the values of an Array into a string with elements separated by a delimiter.
Supports up to two arguments
* **values** - first argument is required and must be an an `Array`
* **delimiter** - second arguments is the delimiter between elements, must be a `String` if given, and defaults to an empty string.
```puppet
join(['a','b','c'], ",")
# Would result in: "a,b,c"
```
Note that array is flattened before elements are joined, but flattening does not extend to arrays nested in hashes or other objects.
```puppet
$a = [1,2, undef, 'hello', [x,y,z], {a => 2, b => [3, 4]}]
notice join($a, ', ')
# would result in noticing:
# 1, 2, , hello, x, y, z, {"a"=>2, "b"=>[3, 4]}
```
For joining iterators and other containers of elements a conversion must first be made to
an `Array`. The reason for this is that there are many options how such a conversion should
be made.
```puppet
[1,2,3].reverse_each.convert_to(Array).join(', ')
# would result in: "3, 2, 1"
```
```puppet
{a => 1, b => 2}.convert_to(Array).join(', ')
# would result in "a, 1, b, 2"
```
For more detailed control over the formatting (including indentations and line breaks, delimiters around arrays
and hash entries, between key/values in hash entries, and individual formatting of values in the array)
see the `new` function for `String` and its formatting options for `Array` and `Hash`.
`join(Array $arg, Optional[String] $delimiter)`
## `json_data`
The `json_data` is a hiera 5 `data_hash` data provider function.
See [the configuration guide documentation](https://puppet.com/docs/puppet/latest/hiera_config_yaml_5.html#configuring-a-hierarchy-level-built-in-backends) for
how to use this function.
`json_data(Struct[{path=>String[1]}] $options, Puppet::LookupContext $context)`
## `keys`
Returns the keys of a hash as an Array
```puppet
$hsh = {"apples" => 3, "oranges" => 4 }
$hsh.keys()
keys($hsh)
# both results in the array ["apples", "oranges"]
```
* Note that a hash in the puppet language accepts any data value (including `undef`) unless
it is constrained with a `Hash` data type that narrows the allowed data types.
* For an empty hash, an empty array is returned.
* The order of the keys is the same as the order in the hash (typically the order in which they were added).
`keys(Hash $hsh)`
## `length`
Returns the length of an Array, Hash, String, or Binary value.
The returned value is a positive integer indicating the number
of elements in the container; counting (possibly multibyte) characters for a `String`,
bytes in a `Binary`, number of elements in an `Array`, and number of
key-value associations in a Hash.
```puppet
"roses".length() # 5
length("violets") # 7
[10, 20].length # 2
{a => 1, b => 3}.length # 2
```
Signature 1
`length(Collection $arg)`
Signature 2
`length(String $arg)`
Signature 3
`length(Binary $arg)`
## `lest`
Calls a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
without arguments if the value given to `lest` is `undef`.
Returns the result of calling the lambda if the argument is `undef`, otherwise the
given argument.
The `lest` function is useful in a chain of `then` calls, or in general
as a guard against `undef` values. The function can be used to call `fail`, or to
return a default value.
These two expressions are equivalent:
```puppet
if $x == undef { do_things() }
lest($x) || { do_things() }
```
```puppet
$data = {a => [ b, c ] }
notice $data.dig(a, b, c)
.then |$x| { $x * 2 }
.lest || { fail("no value for $data[a][b][c]" }
```
Would fail the operation because `$data[a][b][c]` results in `undef`
(there is no `b` key in `a`).
In contrast - this example:
```puppet
$data = {a => { b => { c => 10 } } }
notice $data.dig(a, b, c)
.then |$x| { $x * 2 }
.lest || { fail("no value for $data[a][b][c]" }
```
Would notice the value `20`
`lest(Any $arg, Callable[0,0] &$block)`
## `lookup`
Uses the Puppet lookup system to retrieve a value for a given key. By default,
this returns the first value found (and fails compilation if no values are
available), but you can configure it to merge multiple values into one, fail
gracefully, and more.
When looking up a key, Puppet will search up to three tiers of data, in the
following order:
1. Hiera.
2. The current environment's data provider.
3. The indicated module's data provider, if the key is of the form
`<MODULE NAME>::<SOMETHING>`.
### Arguments
You must provide the name of a key to look up, and can optionally provide other
arguments. You can combine these arguments in the following ways:
* `lookup( <NAME>, [<VALUE TYPE>], [<MERGE BEHAVIOR>], [<DEFAULT VALUE>] )`
* `lookup( [<NAME>], <OPTIONS HASH> )`
* `lookup( as above ) |$key| { # lambda returns a default value }`
Arguments in `[square brackets]` are optional.
The arguments accepted by `lookup` are as follows:
1. `<NAME>` (string or array) --- The name of the key to look up.
* This can also be an array of keys. If Puppet doesn't find anything for the
first key, it will try again with the subsequent ones, only resorting to a
default value if none of them succeed.
2. `<VALUE TYPE>` (data type) --- A
[data type](https://puppet.com/docs/puppet/latest/lang_data_type.html)
that must match the retrieved value; if not, the lookup (and catalog
compilation) will fail. Defaults to `Data` (accepts any normal value).
3. `<MERGE BEHAVIOR>` (string or hash; see **"Merge Behaviors"** below) ---
Whether (and how) to combine multiple values. If present, this overrides any
merge behavior specified in the data sources. Defaults to no value; Puppet will
use merge behavior from the data sources if present, and will otherwise do a
first-found lookup.
4. `<DEFAULT VALUE>` (any normal value) --- If present, `lookup` returns this
when it can't find a normal value. Default values are never merged with found
values. Like a normal value, the default must match the value type. Defaults to
no value; if Puppet can't find a normal value, the lookup (and compilation) will
fail.
5. `<OPTIONS HASH>` (hash) --- Alternate way to set the arguments above, plus
some less-common extra options. If you pass an options hash, you can't combine
it with any regular arguments (except `<NAME>`). An options hash can have the
following keys:
* `'name'` --- Same as `<NAME>` (argument 1). You can pass this as an
argument or in the hash, but not both.
* `'value_type'` --- Same as `<VALUE TYPE>` (argument 2).
* `'merge'` --- Same as `<MERGE BEHAVIOR>` (argument 3).
* `'default_value'` --- Same as `<DEFAULT VALUE>` (argument 4).
* `'default_values_hash'` (hash) --- A hash of lookup keys and default
values. If Puppet can't find a normal value, it will check this hash for the
requested key before giving up. You can combine this with `default_value` or
a lambda, which will be used if the key isn't present in this hash. Defaults
to an empty hash.
* `'override'` (hash) --- A hash of lookup keys and override values. Puppet
will check for the requested key in the overrides hash _first;_ if found, it
returns that value as the _final_ value, ignoring merge behavior. Defaults
to an empty hash.
Finally, `lookup` can take a lambda, which must accept a single parameter.
This is yet another way to set a default value for the lookup; if no results are
found, Puppet will pass the requested key to the lambda and use its result as
the default value.
### Merge Behaviors
Puppet lookup uses a hierarchy of data sources, and a given key might have
values in multiple sources. By default, Puppet returns the first value it finds,
but it can also continue searching and merge all the values together.
> **Note:** Data sources can use the special `lookup_options` metadata key to
request a specific merge behavior for a key. The `lookup` function will use that
requested behavior unless you explicitly specify one.
The valid merge behaviors are:
* `'first'` --- Returns the first value found, with no merging. Puppet lookup's
default behavior.
* `'unique'` (called "array merge" in classic Hiera) --- Combines any number of
arrays and scalar values to return a merged, flattened array with all duplicate
values removed. The lookup will fail if any hash values are found.
* `'hash'` --- Combines the keys and values of any number of hashes to return a
merged hash. If the same key exists in multiple source hashes, Puppet will use
the value from the highest-priority data source; it won't recursively merge the
values.
* `'deep'` --- Combines the keys and values of any number of hashes to return a
merged hash. If the same key exists in multiple source hashes, Puppet will
recursively merge hash or array values (with duplicate values removed from
arrays). For conflicting scalar values, the highest-priority value will win.
* `{'strategy' => 'first'}`, `{'strategy' => 'unique'}`,
or `{'strategy' => 'hash'}` --- Same as the string versions of these merge behaviors.
* `{'strategy' => 'deep', <DEEP OPTION> => <VALUE>, ...}` --- Same as `'deep'`,
but can adjust the merge with additional options. The available options are:
* `'knockout_prefix'` (string) --- A string prefix to indicate a
value should be _removed_ from the final result. If a value is exactly equal
to the prefix, it will knockout the entire element. Defaults to `undef`, which
disables this feature.
* `'sort_merged_arrays'` (boolean) --- Whether to sort all arrays that are
merged together. Defaults to `false`.
* `'merge_hash_arrays'` (boolean) --- Whether to merge hashes within arrays.
Defaults to `false`.
Signature 1
`lookup(NameType $name, Optional[ValueType] $value_type, Optional[MergeType] $merge)`
Signature 2
`lookup(NameType $name, Optional[ValueType] $value_type, Optional[MergeType] $merge, DefaultValueType $default_value)`
Signature 3
`lookup(NameType $name, Optional[ValueType] $value_type, Optional[MergeType] $merge, BlockType &$block)`
Signature 4
`lookup(OptionsWithName $options_hash, Optional[BlockType] &$block)`
Signature 5
`lookup(Variant[String,Array[String]] $name, OptionsWithoutName $options_hash, Optional[BlockType] &$block)`
## `lstrip`
Strips leading spaces from a String
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion removes all leading ASCII white space characters such as space, tab, newline, and return.
It does not remove other space-like characters like hard space (Unicode U+00A0). (Tip, `/^[[:space:]]/` regular expression
matches all space-like characters).
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
```puppet
"\n\thello ".lstrip()
lstrip("\n\thello ")
```
Would both result in `"hello"`
```puppet
["\n\thello ", "\n\thi "].lstrip()
lstrip(["\n\thello ", "\n\thi "])
```
Would both result in `['hello', 'hi']`
Signature 1
`lstrip(Numeric $arg)`
Signature 2
`lstrip(String $arg)`
Signature 3
`lstrip(Iterable[Variant[String, Numeric]] $arg)`
## `map`
Applies a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
to every value in a data structure and returns an array containing the results.
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It can
request one or two parameters.
`$transformed_data = $data.map |$parameter| { <PUPPET CODE BLOCK> }`
or
`$transformed_data = map($data) |$parameter| { <PUPPET CODE BLOCK> }`
When the first argument (`$data` in the above example) is an array, Puppet passes each
value in turn to the lambda.
```puppet
# For the array $data, return an array containing each value multiplied by 10
$data = [1,2,3]
$transformed_data = $data.map |$items| { $items * 10 }
# $transformed_data contains [10,20,30]
```
When the first argument is a hash, Puppet passes each key and value pair to the lambda
as an array in the form `[key, value]`.
```puppet
# For the hash $data, return an array containing the keys
$data = {'a'=>1,'b'=>2,'c'=>3}
$transformed_data = $data.map |$items| { $items[0] }
# $transformed_data contains ['a','b','c']
```
When the first argument is an array and the lambda has two parameters, Puppet passes the
array's indexes (enumerated from 0) in the first parameter and its values in the second
parameter.
```puppet
# For the array $data, return an array containing the indexes
$data = [1,2,3]
$transformed_data = $data.map |$index,$value| { $index }
# $transformed_data contains [0,1,2]
```
When the first argument is a hash, Puppet passes its keys to the first parameter and its
values to the second parameter.
```puppet
# For the hash $data, return an array containing each value
$data = {'a'=>1,'b'=>2,'c'=>3}
$transformed_data = $data.map |$key,$value| { $value }
# $transformed_data contains [1,2,3]
```
Signature 1
`map(Hash[Any, Any] $hash, Callable[2,2] &$block)`
Signature 2
`map(Hash[Any, Any] $hash, Callable[1,1] &$block)`
Signature 3
`map(Iterable $enumerable, Callable[2,2] &$block)`
Signature 4
`map(Iterable $enumerable, Callable[1,1] &$block)`
## `match`
Matches a regular expression against a string and returns an array containing the match
and any matched capturing groups.
The first argument is a string or array of strings. The second argument is either a
regular expression, regular expression represented as a string, or Regex or Pattern
data type that the function matches against the first argument.
The returned array contains the entire match at index 0, and each captured group at
subsequent index values. If the value or expression being matched is an array, the
function returns an array with mapped match results.
If the function doesn't find a match, it returns 'undef'.
```puppet
$matches = "abc123".match(/[a-z]+[1-9]+/)
# $matches contains [abc123]
```
```puppet
$matches = "abc123".match(/([a-z]+)([1-9]+)/)
# $matches contains [abc123, abc, 123]
```
```puppet
$matches = ["abc123","def456"].match(/([a-z]+)([1-9]+)/)
# $matches contains [[abc123, abc, 123], [def456, def, 456]]
```
Signature 1
`match(String $string, Variant[Any, Type] $pattern)`
Signature 2
`match(Array[String] $string, Variant[Any, Type] $pattern)`
## `max`
Returns the highest value among a variable number of arguments.
Takes at least one argument.
This function is (with one exception) compatible with the stdlib function
with the same name and performs deprecated type conversion before
comparison as follows:
* If a value converted to String is an optionally '-' prefixed,
string of digits, one optional decimal point, followed by optional
decimal digits - then the comparison is performed on the values
converted to floating point.
* If a value is not considered convertible to float, it is converted
to a `String` and the comparison is a lexical compare where min is
the lexicographical later value.
* A lexicographical compare is performed in a system locale - international
characters may therefore not appear in what a user thinks is the correct order.
* The conversion rules apply to values in pairs - the rule must hold for both
values - a value may therefore be compared using different rules depending
on the "other value".
* The returned result found to be the "highest" is the original unconverted value.
The above rules have been deprecated in Puppet 6.0.0 as they produce strange results when
given values of mixed data types. In general, either convert values to be
all `String` or all `Numeric` values before calling the function, or call the
function with a lambda that performs type conversion and comparison. This because one
simply cannot compare `Boolean` with `Regexp` and with any arbitrary `Array`, `Hash` or
`Object` and getting a meaningful result.
The one change in the function's behavior is when the function is given a single
array argument. The stdlib implementation would return that array as the result where
it now instead returns the max value from that array.
```puppet
notice(max(1)) # would notice 1
notice(max(1,2)) # would notice 2
notice(max("1", 2)) # would notice 2
notice(max("0777", 512)) # would notice "0777", since "0777" is not converted from octal form
notice(max(0777, 512)) # would notice 512, since 0777 is decimal 511
notice(max('aa', 'ab')) # would notice 'ab'
notice(max(['a'], ['b'])) # would notice ['b'], since "['b']" is after "['a']"
```
```puppet
$x = [1,2,3,4]
notice(max(*$x)) # would notice 4
```
```puppet
$x = [1,2,3,4]
notice(max($x)) # would notice 4
notice($x.max) # would notice 4
```
This example shows that a single array argument is used as the set of values
as opposed to being a single returned value.
When calling with a lambda, it must accept two variables and it must return
one of -1, 0, or 1 depending on if first argument is before/lower than, equal to,
or higher/after the second argument.
```puppet
notice(max("2", "10", "100") |$a, $b| { compare($a, $b) })
```
Would notice "2" as higher since it is lexicographically higher/after the other values. Without the
lambda the stdlib compatible (deprecated) behavior would have been to return "100" since number conversion
kicks in.
Signature 1
`max(Numeric *$values)`
Signature 2
`max(String *$values)`
Signature 3
`max(Semver *$values)`
Signature 4
`max(Timespan *$values)`
Signature 5
`max(Timestamp *$values)`
Signature 6
`max(Array[Numeric] $values, Optional[Callable[2,2]] &$block)`
Signature 7
`max(Array[String] $values, Optional[Callable[2,2]] &$block)`
Signature 8
`max(Array[Semver] $values, Optional[Callable[2,2]] &$block)`
Signature 9
`max(Array[Timespan] $values, Optional[Callable[2,2]] &$block)`
Signature 10
`max(Array[Timestamp] $values, Optional[Callable[2,2]] &$block)`
Signature 11
`max(Array $values, Optional[Callable[2,2]] &$block)`
Signature 12
`max(Any *$values, Callable[2,2] &$block)`
Signature 13
`max(Any *$values)`
## `md5`
Returns a MD5 hash value from a provided string.
`md5()`
## `min`
Returns the lowest value among a variable number of arguments.
Takes at least one argument.
This function is (with one exception) compatible with the stdlib function
with the same name and performs deprecated type conversion before
comparison as follows:
* If a value converted to String is an optionally '-' prefixed,
string of digits, one optional decimal point, followed by optional
decimal digits - then the comparison is performed on the values
converted to floating point.
* If a value is not considered convertible to float, it is converted
to a `String` and the comparison is a lexical compare where min is
the lexicographical earlier value.
* A lexicographical compare is performed in a system locale - international
characters may therefore not appear in what a user thinks is the correct order.
* The conversion rules apply to values in pairs - the rule must hold for both
values - a value may therefore be compared using different rules depending
on the "other value".
* The returned result found to be the "lowest" is the original unconverted value.
The above rules have been deprecated in Puppet 6.0.0 as they produce strange results when
given values of mixed data types. In general, either convert values to be
all `String` or all `Numeric` values before calling the function, or call the
function with a lambda that performs type conversion and comparison. This because one
simply cannot compare `Boolean` with `Regexp` and with any arbitrary `Array`, `Hash` or
`Object` and getting a meaningful result.
The one change in the function's behavior is when the function is given a single
array argument. The stdlib implementation would return that array as the result where
it now instead returns the max value from that array.
```puppet
notice(min(1)) # would notice 1
notice(min(1,2)) # would notice 1
notice(min("1", 2)) # would notice 1
notice(min("0777", 512)) # would notice 512, since "0777" is not converted from octal form
notice(min(0777, 512)) # would notice 511, since 0777 is decimal 511
notice(min('aa', 'ab')) # would notice 'aa'
notice(min(['a'], ['b'])) # would notice ['a'], since "['a']" is before "['b']"
```
```puppet
$x = [1,2,3,4]
notice(min(*$x)) # would notice 1
```
```puppet
$x = [1,2,3,4]
notice(min($x)) # would notice 1
notice($x.min) # would notice 1
```
This example shows that a single array argument is used as the set of values
as opposed to being a single returned value.
When calling with a lambda, it must accept two variables and it must return
one of -1, 0, or 1 depending on if first argument is before/lower than, equal to,
or higher/after the second argument.
```puppet
notice(min("2", "10", "100") |$a, $b| { compare($a, $b) })
```
Would notice "10" as lower since it is lexicographically lower/before the other values. Without the
lambda the stdlib compatible (deprecated) behavior would have been to return "2" since number conversion kicks in.
Signature 1
`min(Numeric *$values)`
Signature 2
`min(String *$values)`
Signature 3
`min(Semver *$values)`
Signature 4
`min(Timespan *$values)`
Signature 5
`min(Timestamp *$values)`
Signature 6
`min(Array[Numeric] $values, Optional[Callable[2,2]] &$block)`
Signature 7
`min(Array[Semver] $values, Optional[Callable[2,2]] &$block)`
Signature 8
`min(Array[Timespan] $values, Optional[Callable[2,2]] &$block)`
Signature 9
`min(Array[Timestamp] $values, Optional[Callable[2,2]] &$block)`
Signature 10
`min(Array[String] $values, Optional[Callable[2,2]] &$block)`
Signature 11
`min(Array $values, Optional[Callable[2,2]] &$block)`
Signature 12
`min(Any *$values, Callable[2,2] &$block)`
Signature 13
`min(Any *$values)`
## `module_directory`
Finds an existing module and returns the path to its root directory.
The argument to this function should be a module name String
For example, the reference `mysql` will search for the
directory `<MODULES DIRECTORY>/mysql` and return the first
found on the modulepath.
This function can also accept:
* Multiple String arguments, which will return the path of the **first** module
found, skipping non existing modules.
* An array of module names, which will return the path of the **first** module
found from the given names in the array, skipping non existing modules.
The function returns `undef` if none of the given modules were found
Signature 1
`module_directory(String *$names)`
Signature 2
`module_directory(Array[String] *$names)`
## `new`
Creates a new instance/object of a given data type.
This function makes it possible to create new instances of
concrete data types. If a block is given it is called with the
just created instance as an argument.
Calling this function is equivalent to directly
calling the data type:
```puppet
$a = Integer.new("42")
$b = Integer("42")
```
These would both convert the string `"42"` to the decimal value `42`.
```puppet
$a = Integer.new("42", 8)
$b = Integer({from => "42", radix => 8})
```
This would convert the octal (radix 8) number `"42"` in string form
to the decimal value `34`.
The new function supports two ways of giving the arguments:
* by name (using a hash with property to value mapping)
* by position (as regular arguments)
Note that it is not possible to create new instances of
some abstract data types (for example `Variant`). The data type `Optional[T]` is an
exception as it will create an instance of `T` or `undef` if the
value to convert is `undef`.
The arguments that can be given is determined by the data type.
> An assertion is always made that the produced value complies with the given type constraints.
```puppet
Integer[0].new("-100")
```
Would fail with an assertion error (since value is less than 0).
The following sections show the arguments and conversion rules
per data type built into the Puppet Type System.
### Conversion to `Optional[T]` and `NotUndef[T]`
Conversion to these data types is the same as a conversion to the type argument `T`.
In the case of `Optional[T]` it is accepted that the argument to convert may be `undef`.
It is however not acceptable to give other arguments (than `undef`) that cannot be
converted to `T`.
### Conversion to Integer
A new `Integer` can be created from `Integer`, `Float`, `Boolean`, and `String` values.
For conversion from `String` it is possible to specify the radix (base).
```puppet
type Radix = Variant[Default, Integer[2,2], Integer[8,8], Integer[10,10], Integer[16,16]]
function Integer.new(
String $value,
Radix $radix = 10,
Boolean $abs = false
)
function Integer.new(
Variant[Numeric, Boolean] $value,
Boolean $abs = false
)
```
* When converting from `String` the default radix is 10.
* If radix is not specified an attempt is made to detect the radix from the start of the string:
* `0b` or `0B` is taken as radix 2.
* `0x` or `0X` is taken as radix 16.
* `0` as radix 8.
* All others are decimal.
* Conversion from `String` accepts an optional sign in the string.
* For hexadecimal (radix 16) conversion an optional leading `"0x"`, or `"0X"` is accepted.
* For octal (radix 8) an optional leading `"0"` is accepted.
* For binary (radix 2) an optional leading `"0b"` or `"0B"` is accepted.
* When `radix` is set to `default`, the conversion is based on the leading.
characters in the string. A leading `"0"` for radix 8, a leading `"0x"`, or `"0X"` for
radix 16, and leading `"0b"` or `"0B"` for binary.
* Conversion from `Boolean` results in `0` for `false` and `1` for `true`.
* Conversion from `Integer`, `Float`, and `Boolean` ignores the radix.
* `Float` value fractions are truncated (no rounding).
* When `abs` is set to `true`, the result will be an absolute integer.
```puppet
$a_number = Integer("0xFF", 16) # results in 255
$a_number = Integer("010") # results in 8
$a_number = Integer("010", 10) # results in 10
$a_number = Integer(true) # results in 1
$a_number = Integer(-38, 10, true) # results in 38
```
### Conversion to Float
A new `Float` can be created from `Integer`, `Float`, `Boolean`, and `String` values.
For conversion from `String` both float and integer formats are supported.
```puppet
function Float.new(
Variant[Numeric, Boolean, String] $value,
Boolean $abs = true
)
```
* For an integer, the floating point fraction of `.0` is added to the value.
* A `Boolean` `true` is converted to `1.0`, and a `false` to `0.0`.
* In `String` format, integer prefixes for hex and binary are understood (but not octal since
floating point in string format may start with a `'0'`).
* When `abs` is set to `true`, the result will be an absolute floating point value.
### Conversion to Numeric
A new `Integer` or `Float` can be created from `Integer`, `Float`, `Boolean` and
`String` values.
```puppet
function Numeric.new(
Variant[Numeric, Boolean, String] $value,
Boolean $abs = true
)
```
* If the value has a decimal period, or if given in scientific notation
(e/E), the result is a `Float`, otherwise the value is an `Integer`. The
conversion from `String` always uses a radix based on the prefix of the string.
* Conversion from `Boolean` results in `0` for `false` and `1` for `true`.
* When `abs` is set to `true`, the result will be an absolute `Float`or `Integer` value.
```puppet
$a_number = Numeric(true) # results in 1
$a_number = Numeric("0xFF") # results in 255
$a_number = Numeric("010") # results in 8
$a_number = Numeric("3.14") # results in 3.14 (a float)
$a_number = Numeric(-42.3, true) # results in 42.3
$a_number = Numeric(-42, true) # results in 42
```
### Conversion to Timespan
A new `Timespan` can be created from `Integer`, `Float`, `String`, and `Hash` values. Several variants of the constructor are provided.
**Timespan from seconds**
When a Float is used, the decimal part represents fractions of a second.
```puppet
function Timespan.new(
Variant[Float, Integer] $value
)
```
**Timespan from days, hours, minutes, seconds, and fractions of a second**
The arguments can be passed separately in which case the first four, days, hours, minutes, and seconds are mandatory and the rest are optional.
All values may overflow and/or be negative. The internal 128-bit nano-second integer is calculated as:
```
(((((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000 + milliseconds) * 1000 + microseconds) * 1000 + nanoseconds
```
```puppet
function Timespan.new(
Integer $days, Integer $hours, Integer $minutes, Integer $seconds,
Integer $milliseconds = 0, Integer $microseconds = 0, Integer $nanoseconds = 0
)
```
or, all arguments can be passed as a `Hash`, in which case all entries are optional:
```puppet
function Timespan.new(
Struct[{
Optional[negative] => Boolean,
Optional[days] => Integer,
Optional[hours] => Integer,
Optional[minutes] => Integer,
Optional[seconds] => Integer,
Optional[milliseconds] => Integer,
Optional[microseconds] => Integer,
Optional[nanoseconds] => Integer
}] $hash
)
```
**Timespan from String and format directive patterns**
The first argument is parsed using the format optionally passed as a string or array of strings. When an array is used, an attempt
will be made to parse the string using the first entry and then with each entry in succession until parsing succeeds. If the second
argument is omitted, an array of default formats will be used.
An exception is raised when no format was able to parse the given string.
```puppet
function Timespan.new(
String $string, Variant[String[2],Array[String[2], 1]] $format = <default format>)
)
```
the arguments may also be passed as a `Hash`:
```puppet
function Timespan.new(
Struct[{
string => String[1],
Optional[format] => Variant[String[2],Array[String[2], 1]]
}] $hash
)
```
The directive consists of a percent (`%`) character, zero or more flags, optional minimum field width and
a conversion specifier as follows:
```
%[Flags][Width]Conversion
```
**Flags:**
| Flag | Meaning
| ---- | ---------------
| - | Don't pad numerical output
| _ | Use spaces for padding
| 0 | Use zeros for padding
**Format directives:**
| Format | Meaning |
| ------ | ------- |
| D | Number of Days |
| H | Hour of the day, 24-hour clock |
| M | Minute of the hour (00..59) |
| S | Second of the minute (00..59) |
| L | Millisecond of the second (000..999) |
| N | Fractional seconds digits |
The format directive that represents the highest magnitude in the format will be allowed to
overflow. I.e. if no "%D" is used but a "%H" is present, then the hours may be more than 23.
The default array contains the following patterns:
```
['%D-%H:%M:%S', '%D-%H:%M', '%H:%M:%S', '%H:%M']
```
Examples - Converting to Timespan
```puppet
$duration = Timespan(13.5) # 13 seconds and 500 milliseconds
$duration = Timespan({days=>4}) # 4 days
$duration = Timespan(4, 0, 0, 2) # 4 days and 2 seconds
$duration = Timespan('13:20') # 13 hours and 20 minutes (using default pattern)
$duration = Timespan('10:03.5', '%M:%S.%L') # 10 minutes, 3 seconds, and 5 milli-seconds
$duration = Timespan('10:03.5', '%M:%S.%N') # 10 minutes, 3 seconds, and 5 nano-seconds
```
### Conversion to Timestamp
A new `Timestamp` can be created from `Integer`, `Float`, `String`, and `Hash` values. Several variants of the constructor are provided.
**Timestamp from seconds since epoch (1970-01-01 00:00:00 UTC)**
When a Float is used, the decimal part represents fractions of a second.
```puppet
function Timestamp.new(
Variant[Float, Integer] $value
)
```
**Timestamp from String and patterns consisting of format directives**
The first argument is parsed using the format optionally passed as a string or array of strings. When an array is used, an attempt
will be made to parse the string using the first entry and then with each entry in succession until parsing succeeds. If the second
argument is omitted, an array of default formats will be used.
A third optional timezone argument can be provided. The first argument will then be parsed as if it represents a local time in that
timezone. The timezone can be any timezone that is recognized when using the `'%z'` or `'%Z'` formats, or the word `'current'`, in which
case the current timezone of the evaluating process will be used. The timezone argument is case insensitive.
The default timezone, when no argument is provided, or when using the keyword `default`, is 'UTC'.
It is illegal to provide a timezone argument other than `default` in combination with a format that contains '%z' or '%Z' since that
would introduce an ambiguity as to which timezone to use. The one extracted from the string, or the one provided as an argument.
An exception is raised when no format was able to parse the given string.
```puppet
function Timestamp.new(
String $string,
Variant[String[2],Array[String[2], 1]] $format = <default format>,
String $timezone = default)
)
```
the arguments may also be passed as a `Hash`:
```puppet
function Timestamp.new(
Struct[{
string => String[1],
Optional[format] => Variant[String[2],Array[String[2], 1]],
Optional[timezone] => String[1]
}] $hash
)
```
The directive consists of a percent (%) character, zero or more flags, optional minimum field width and
a conversion specifier as follows:
```
%[Flags][Width]Conversion
```
**Flags:**
| Flag | Meaning
| ---- | ---------------
| - | Don't pad numerical output
| _ | Use spaces for padding
| 0 | Use zeros for padding
| # | Change names to upper-case or change case of am/pm
| ^ | Use uppercase
| : | Use colons for `%z`
**Format directives (names and padding can be altered using flags):**
**Date (Year, Month, Day):**
| Format | Meaning |
| ------ | ------- |
| Y | Year with century, zero-padded to at least 4 digits |
| C | year / 100 (rounded down such as `20` in `2009`) |
| y | year % 100 (`00..99`) |
| m | Month of the year, zero-padded (`01..12`) |
| B | The full month name (`"January"`) |
| b | The abbreviated month name (`"Jan"`) |
| h | Equivalent to `%b` |
| d | Day of the month, zero-padded (`01..31`) |
| e | Day of the month, blank-padded (`1..31`) |
| j | Day of the year (`001..366`) |
**Time (Hour, Minute, Second, Subsecond):**
| Format | Meaning |
| ------ | ------- |
| H | Hour of the day, 24-hour clock, zero-padded (`00..23`) |
| k | Hour of the day, 24-hour clock, blank-padded (`0..23`) |
| I | Hour of the day, 12-hour clock, zero-padded (`01..12`) |
| l | Hour of the day, 12-hour clock, blank-padded (`1..12`) |
| P | Meridian indicator, lowercase (`"am"` or `"pm"`) |
| p | Meridian indicator, uppercase (`"AM"` or `"PM"`) |
| M | Minute of the hour (`00..59`) |
| S | Second of the minute (`00..60`) |
| L | Millisecond of the second (`000..999`). Digits under millisecond are truncated to not produce 1000 |
| N | Fractional seconds digits, default is 9 digits (nanosecond). Digits under a specified width are truncated to avoid carry up |
**Time (Hour, Minute, Second, Subsecond):**
| Format | Meaning |
| ------ | ------- |
| z | Time zone as hour and minute offset from UTC (e.g. `+0900`) |
| :z | hour and minute offset from UTC with a colon (e.g. `+09:00`) |
| ::z | hour, minute and second offset from UTC (e.g. `+09:00:00`) |
| Z | Abbreviated time zone name or similar information. (OS dependent) |
**Weekday:**
| Format | Meaning |
| ------ | ------- |
| A | The full weekday name (`"Sunday"`) |
| a | The abbreviated name (`"Sun"`) |
| u | Day of the week (Monday is `1`, `1..7`) |
| w | Day of the week (Sunday is `0`, `0..6`) |
**ISO 8601 week-based year and week number:**
The first week of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
| Format | Meaning |
| ------ | ------- |
| G | The week-based year |
| g | The last 2 digits of the week-based year (`00..99`) |
| V | Week number of the week-based year (`01..53`) |
**Week number:**
The first week of YYYY that starts with a Sunday or Monday (according to %U
or %W). The days in the year before the first week are in week 0.
| Format | Meaning |
| ------ | ------- |
| U | Week number of the year. The week starts with Sunday. (`00..53`) |
| W | Week number of the year. The week starts with Monday. (`00..53`) |
**Seconds since the Epoch:**
| Format | Meaning |
| s | Number of seconds since 1970-01-01 00:00:00 UTC. |
**Literal string:**
| Format | Meaning |
| ------ | ------- |
| n | Newline character (`\n`) |
| t | Tab character (`\t`) |
| % | Literal `%` character |
**Combination:**
| Format | Meaning |
| ------ | ------- |
| c | date and time (`%a %b %e %T %Y`) |
| D | Date (`%m/%d/%y`) |
| F | The ISO 8601 date format (`%Y-%m-%d`) |
| v | VMS date (`%e-%^b-%4Y`) |
| x | Same as `%D` |
| X | Same as `%T` |
| r | 12-hour time (`%I:%M:%S %p`) |
| R | 24-hour time (`%H:%M`) |
| T | 24-hour time (`%H:%M:%S`) |
The default array contains the following patterns:
When a timezone argument (other than `default`) is explicitly provided:
```
['%FT%T.L', '%FT%T', '%F']
```
otherwise:
```
['%FT%T.%L %Z', '%FT%T %Z', '%F %Z', '%FT%T.L', '%FT%T', '%F']
```
Examples - Converting to Timestamp
```puppet
$ts = Timestamp(1473150899) # 2016-09-06 08:34:59 UTC
$ts = Timestamp({string=>'2015', format=>'%Y'}) # 2015-01-01 00:00:00.000 UTC
$ts = Timestamp('Wed Aug 24 12:13:14 2016', '%c') # 2016-08-24 12:13:14 UTC
$ts = Timestamp('Wed Aug 24 12:13:14 2016 PDT', '%c %Z') # 2016-08-24 19:13:14.000 UTC
$ts = Timestamp('2016-08-24 12:13:14', '%F %T', 'PST') # 2016-08-24 20:13:14.000 UTC
$ts = Timestamp('2016-08-24T12:13:14', default, 'PST') # 2016-08-24 20:13:14.000 UTC
```
### Conversion to Type
A new `Type` can be created from its `String` representation.
```puppet
$t = Type.new('Integer[10]')
```
### Conversion to String
Conversion to `String` is the most comprehensive conversion as there are many
use cases where a string representation is wanted. The defaults for the many options
have been chosen with care to be the most basic "value in textual form" representation.
The more advanced forms of formatting are intended to enable writing special purposes formatting
functions in the Puppet language.
A new string can be created from all other data types. The process is performed in
several steps - first the data type of the given value is inferred, then the resulting data type
is used to find the most significant format specified for that data type. And finally,
the found format is used to convert the given value.
The mapping from data type to format is referred to as the *format map*. This map
allows different formatting depending on type.
```puppet
$format_map = {
Integer[default, 0] => "%d",
Integer[1, default] => "%#x"
}
String("-1", $format_map) # produces '-1'
String("10", $format_map) # produces '0xa'
```
A format is specified on the form:
```
%[Flags][Width][.Precision]Format
```
`Width` is the number of characters into which the value should be fitted. This allocated space is
padded if value is shorter. By default it is space padded, and the flag `0` will cause padding with `0`
for numerical formats.
`Precision` is the number of fractional digits to show for floating point, and the maximum characters
included in a string format.
Note that all data type supports the formats `s` and `p` with the meaning "default string representation" and
"default programmatic string representation" (which for example means that a String is quoted in 'p' format).
**Signatures of String conversion**
```puppet
type Format = Pattern[/^%([\s\+\-#0\[\{<\(\|]*)([1-9][0-9]*)?(?:\.([0-9]+))?([a-zA-Z])/]
type ContainerFormat = Struct[{
format => Optional[String],
separator => Optional[String],
separator2 => Optional[String],
string_formats => Hash[Type, Format]
}]
type TypeMap = Hash[Type, Variant[Format, ContainerFormat]]
type Formats = Variant[Default, String[1], TypeMap]
function String.new(
Any $value,
Formats $string_formats
)
```
Where:
* `separator` is the string used to separate entries in an array, or hash (extra space should not be included at
the end), defaults to `","`
* `separator2` is the separator between key and value in a hash entry (space padding should be included as
wanted), defaults to `" => "`.
* `string_formats` is a data type to format map for values contained in arrays and hashes - defaults to `{Any => "%p"}`. Note that
these nested formats are not applicable to data types that are containers; they are always formatted as per the top level
format specification.
```puppet
$str = String(10) # produces '10'
$str = String([10]) # produces '["10"]'
```
```puppet
$str = String(10, "%#x") # produces '0xa'
$str = String([10], "%(a") # produces '("10")'
```
```puppet
$formats = {
Array => {
format => '%(a',
string_formats => { Integer => '%#x' }
}
}
$str = String([1,2,3], $formats) # produces '(0x1, 0x2, 0x3)'
```
The given formats are merged with the default formats, and matching of values to convert against format is based on
the specificity of the mapped type; for example, different formats can be used for short and long arrays.
**Integer to String**
| Format | Integer Formats
| ------ | ---------------
| d | Decimal, negative values produces leading `-`.
| x X | Hexadecimal in lower or upper case. Uses `..f/..F` for negative values unless `+` is also used. A `#` adds prefix `0x/0X`.
| o | Octal. Uses `..0` for negative values unless `+` is also used. A `#` adds prefix `0`.
| b B | Binary with prefix `b` or `B`. Uses `..1/..1` for negative values unless `+` is also used.
| c | Numeric value representing a Unicode value, result is a one unicode character string, quoted if alternative flag `#` is used
| s | Same as `d`, or `d` in quotes if alternative flag `#` is used.
| p | Same as `d`.
| eEfgGaA | Converts integer to float and formats using the floating point rules.
Defaults to `d`.
**Float to String**
| Format | Float formats
| ------ | -------------
| f | Floating point in non exponential notation.
| e E | Exponential notation with `e` or `E`.
| g G | Conditional exponential with `e` or `E` if exponent `< -4` or `>=` the precision.
| a A | Hexadecimal exponential form, using `x`/`X` as prefix and `p`/`P` before exponent.
| s | Converted to string using format `p`, then applying string formatting rule, alternate form `#`` quotes result.
| p | Same as `f` format with minimum significant number of fractional digits, prec has no effect.
| dxXobBc | Converts float to integer and formats using the integer rules.
Defaults to `p`.
**String to String**
| Format | String
| ------ | ------
| s | Unquoted string, verbatim output of control chars.
| p | Programmatic representation - strings are quoted, interior quotes and control chars are escaped. Selects single or double quotes based on content, or uses double quotes if alternative flag `#` is used.
| C | Each `::` name segment capitalized, quoted if alternative flag `#` is used.
| c | Capitalized string, quoted if alternative flag `#` is used.
| d | Downcased string, quoted if alternative flag `#` is used.
| u | Upcased string, quoted if alternative flag `#` is used.
| t | Trims leading and trailing whitespace from the string, quoted if alternative flag `#` is used.
Defaults to `s` at top level and `p` inside array or hash.
**Boolean to String**
| Format | Boolean Formats
| ---- | -------------------
| t T | String `'true'/'false'` or `'True'/'False'`, first char if alternate form is used (i.e. `'t'/'f'` or `'T'/'F'`).
| y Y | String `'yes'/'no'`, `'Yes'/'No'`, `'y'/'n'` or `'Y'/'N'` if alternative flag `#` is used.
| dxXobB | Numeric value `0/1` in accordance with the given format which must be valid integer format.
| eEfgGaA | Numeric value `0.0/1.0` in accordance with the given float format and flags.
| s | String `'true'` / `'false'`.
| p | String `'true'` / `'false'`.
**Regexp to String**
| Format | Regexp Formats
| ---- | --------------
| s | No delimiters, quoted if alternative flag `#` is used.
| p | Delimiters `/ /`.
**Undef to String**
| Format | Undef formats
| ------ | -------------
| s | Empty string, or quoted empty string if alternative flag `#` is used.
| p | String `'undef'`, or quoted `'"undef"'` if alternative flag `#` is used.
| n | String `'nil'`, or `'null'` if alternative flag `#` is used.
| dxXobB | String `'NaN'`.
| eEfgGaA | String `'NaN'`.
| v | String `'n/a'`.
| V | String `'N/A'`.
| u | String `'undef'`, or `'undefined'` if alternative `#` flag is used.
**Default value to String**
| Format | Default formats
| ------ | ---------------
| d D | String `'default'` or `'Default'`, alternative form `#` causes value to be quoted.
| s | Same as `d`.
| p | Same as `d`.
**Binary value to String**
| Format | Default formats
| ------ | ---------------
| s | binary as unquoted UTF-8 characters (errors if byte sequence is invalid UTF-8). Alternate form escapes non ascii bytes.
| p | `'Binary("<base64strict>")'`
| b | `'<base64>'` - base64 string with newlines inserted
| B | `'<base64strict>'` - base64 strict string (without newlines inserted)
| u | `'<base64urlsafe>'` - base64 urlsafe string
| t | `'Binary'` - outputs the name of the type only
| T | `'BINARY'` - output the name of the type in all caps only
* The alternate form flag `#` will quote the binary or base64 text output.
* The format `%#s` allows invalid UTF-8 characters and outputs all non ascii bytes
as hex escaped characters on the form `\\xHH` where `H` is a hex digit.
* The width and precision values are applied to the text part only in `%p` format.
**Array & Tuple to String**
| Format | Array/Tuple Formats
| ------ | -------------
| a | Formats with `[ ]` delimiters and `,`, alternate form `#` indents nested arrays/hashes.
| s | Same as `a`.
| p | Same as `a`.
See "Flags" `<[({\|` for formatting of delimiters, and "Additional parameters for containers; Array and Hash" for
more information about options.
The alternate form flag `#` will cause indentation of nested array or hash containers. If width is also set
it is taken as the maximum allowed length of a sequence of elements (not including delimiters). If this max length
is exceeded, each element will be indented.
**Hash & Struct to String**
| Format | Hash/Struct Formats
| ------ | -------------
| h | Formats with `{ }` delimiters, `,` element separator and ` => ` inner element separator unless overridden by flags.
| s | Same as h.
| p | Same as h.
| a | Converts the hash to an array of `[k,v]` tuples and formats it using array rule(s).
See "Flags" `<[({\|` for formatting of delimiters, and "Additional parameters for containers; Array and Hash" for
more information about options.
The alternate form flag `#` will format each hash key/value entry indented on a separate line.
**Type to String**
| Format | Array/Tuple Formats
| ------ | -------------
| s | The same as `p`, quoted if alternative flag `#` is used.
| p | Outputs the type in string form as specified by the Puppet Language.
**Flags**
| Flag | Effect
| ------ | ------
| (space) | A space instead of `+` for numeric output (`-` is shown), for containers skips delimiters.
| # | Alternate format; prefix `0x/0x`, `0` (octal) and `0b/0B` for binary, Floats force decimal '.'. For g/G keep trailing `0`.
| + | Show sign `+/-` depending on value's sign, changes `x`, `X`, `o`, `b`, `B` format to not use 2's complement form.
| - | Left justify the value in the given width.
| 0 | Pad with `0` instead of space for widths larger than value.
| <[({\| | Defines an enclosing pair `<> [] () {} or \| \|` when used with a container type.
### Conversion to Boolean
Accepts a single value as argument:
* Float `0.0` is `false`, all other float values are `true`
* Integer `0` is `false`, all other integer values are `true`
* Strings
* `true` if 'true', 'yes', 'y' (case independent compare)
* `false` if 'false', 'no', 'n' (case independent compare)
* Boolean is already boolean and is simply returned
### Conversion to Array and Tuple
When given a single value as argument:
* A non empty `Hash` is converted to an array matching `Array[Tuple[Any,Any], 1]`.
* An empty `Hash` becomes an empty array.
* An `Array` is simply returned.
* An `Iterable[T]` is turned into an array of `T` instances.
* A `Binary` is converted to an `Array[Integer[0,255]]` of byte values
When given a second Boolean argument:
* if `true`, a value that is not already an array is returned as a one element array.
* if `false`, (the default), converts the first argument as shown above.
```puppet
$arr = Array($value, true)
```
Conversion to a `Tuple` works exactly as conversion to an `Array`, only that the constructed array is
asserted against the given tuple type.
### Conversion to Hash and Struct
Accepts a single value as argument:
* An empty `Array` becomes an empty `Hash`
* An `Array` matching `Array[Tuple[Any,Any], 1]` is converted to a hash where each tuple describes a key/value entry
* An `Array` with an even number of entries is interpreted as `[key1, val1, key2, val2, ...]`
* An `Iterable` is turned into an `Array` and then converted to hash as per the array rules
* A `Hash` is simply returned
Alternatively, a tree can be constructed by giving two values; an array of tuples on the form `[path, value]`
(where the `path` is the path from the root of a tree, and `value` the value at that position in the tree), and
either the option `'tree'` (do not convert arrays to hashes except the top level), or
`'hash_tree'` (convert all arrays to hashes).
The tree/hash_tree forms of Hash creation are suited for transforming the result of an iteration
using `tree_each` and subsequent filtering or mapping.
Mapping an arbitrary structure in a way that keeps the structure, but where some values are replaced
can be done by using the `tree_each` function, mapping, and then constructing a new Hash from the result:
```puppet
# A hash tree with 'water' at different locations
$h = { a => { b => { x => 'water'}}, b => { y => 'water'} }
# a helper function that turns water into wine
function make_wine($x) { if $x == 'water' { 'wine' } else { $x } }
# create a flattened tree with water turned into wine
$flat_tree = $h.tree_each.map |$entry| { [$entry[0], make_wine($entry[1])] }
# create a new Hash and log it
notice Hash($flat_tree, 'hash_tree')
```
Would notice the hash `{a => {b => {x => wine}}, b => {y => wine}}`
Conversion to a `Struct` works exactly as conversion to a `Hash`, only that the constructed hash is
asserted against the given struct type.
### Conversion to a Regexp
A `String` can be converted into a `Regexp`
**Example**: Converting a String into a Regexp
```puppet
$s = '[a-z]+\.com'
$r = Regexp($s)
if('foo.com' =~ $r) {
...
}
```
### Creating a SemVer
A SemVer object represents a single [Semantic Version](http://semver.org/).
It can be created from a String, individual values for its parts, or a hash specifying the value per part.
See the specification at [semver.org](http://semver.org/) for the meaning of the SemVer's parts.
The signatures are:
```puppet
type PositiveInteger = Integer[0,default]
type SemVerQualifier = Pattern[/\A(?<part>[0-9A-Za-z-]+)(?:\.\g<part>)*\Z/]
type SemVerString = String[1]
type SemVerHash =Struct[{
major => PositiveInteger,
minor => PositiveInteger,
patch => PositiveInteger,
Optional[prerelease] => SemVerQualifier,
Optional[build] => SemVerQualifier
}]
function SemVer.new(SemVerString $str)
function SemVer.new(
PositiveInteger $major
PositiveInteger $minor
PositiveInteger $patch
Optional[SemVerQualifier] $prerelease = undef
Optional[SemVerQualifier] $build = undef
)
function SemVer.new(SemVerHash $hash_args)
```
```puppet
# As a type, SemVer can describe disjunct ranges which versions can be
# matched against - here the type is constructed with two
# SemVerRange objects.
#
$t = SemVer[
SemVerRange('>=1.0.0 <2.0.0'),
SemVerRange('>=3.0.0 <4.0.0')
]
notice(SemVer('1.2.3') =~ $t) # true
notice(SemVer('2.3.4') =~ $t) # false
notice(SemVer('3.4.5') =~ $t) # true
```
### Creating a `SemVerRange`
A `SemVerRange` object represents a range of `SemVer`. It can be created from
a `String`, or from two `SemVer` instances, where either end can be given as
a literal `default` to indicate infinity. The string format of a `SemVerRange` is specified by
the [Semantic Version Range Grammar](https://github.com/npm/node-semver#ranges).
> Use of the comparator sets described in the grammar (joining with `||`) is not supported.
The signatures are:
```puppet
type SemVerRangeString = String[1]
type SemVerRangeHash = Struct[{
min => Variant[Default, SemVer],
Optional[max] => Variant[Default, SemVer],
Optional[exclude_max] => Boolean
}]
function SemVerRange.new(
SemVerRangeString $semver_range_string
)
function SemVerRange.new(
Variant[Default,SemVer] $min
Variant[Default,SemVer] $max
Optional[Boolean] $exclude_max = undef
)
function SemVerRange.new(
SemVerRangeHash $semver_range_hash
)
```
For examples of `SemVerRange` use see "Creating a SemVer"
### Creating a Binary
A `Binary` object represents a sequence of bytes and it can be created from a String in Base64 format,
an Array containing byte values. A Binary can also be created from a Hash containing the value to convert to
a `Binary`.
The signatures are:
```puppet
type ByteInteger = Integer[0,255]
type Base64Format = Enum["%b", "%u", "%B", "%s"]
type StringHash = Struct[{value => String, "format" => Optional[Base64Format]}]
type ArrayHash = Struct[{value => Array[ByteInteger]}]
type BinaryArgsHash = Variant[StringHash, ArrayHash]
function Binary.new(
String $base64_str,
Optional[Base64Format] $format
)
function Binary.new(
Array[ByteInteger] $byte_array
}
# Same as for String, or for Array, but where arguments are given in a Hash.
function Binary.new(BinaryArgsHash $hash_args)
```
The formats have the following meaning:
| format | explanation |
| ---- | ---- |
| B | The data is in base64 strict encoding
| u | The data is in URL safe base64 encoding
| b | The data is in base64 encoding, padding as required by base64 strict, is added by default
| s | The data is a puppet string. The string must be valid UTF-8, or convertible to UTF-8 or an error is raised.
| r | (Ruby Raw) the byte sequence in the given string is used verbatim irrespective of possible encoding errors
* The default format is `%B`.
* Note that the format `%r` should be used sparingly, or not at all. It exists for backwards compatibility reasons when someone receiving
a string from some function and that string should be treated as Binary. Such code should be changed to return a Binary instead of a String.
```puppet
# create the binary content "abc"
$a = Binary('YWJj')
# create the binary content from content in a module's file
$b = binary_file('mymodule/mypicture.jpg')
```
* Since 4.5.0
* Binary type since 4.8.0
### Creating an instance of a `Type` using the `Init` type
The type `Init[T]` describes a value that can be used when instantiating a type. When used as the first argument in a call to `new`, it
will dispatch the call to its contained type and optionally augment the parameter list with additional arguments.
```puppet
# The following declaration
$x = Init[Integer].new('128')
# is exactly the same as
$x = Integer.new('128')
```
or, with base 16 and using implicit new
```puppet
# The following declaration
$x = Init[Integer,16]('80')
# is exactly the same as
$x = Integer('80', 16)
```
```puppet
$fmt = Init[String,'%#x']
notice($fmt(256)) # will notice '0x100'
```
`new(Type $type, Any *$args, Optional[Callable] &$block)`
## `next`
Makes iteration continue with the next value, optionally with a given value for this iteration.
If a value is not given it defaults to `undef`
```puppet
$data = ['a','b','c']
$data.each |Integer $index, String $value| {
if $index == 1 {
next()
}
notice ("${index} = ${value}")
}
```
Would notice:
```
Notice: Scope(Class[main]): 0 = a
Notice: Scope(Class[main]): 2 = c
```
`next(Optional[Any] $value)`
## `notice`
Logs a message on the server at level `notice`.
`notice(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `partition`
Returns two arrays, the first containing the elements of enum for which the block evaluates to true,
the second containing the rest.
Signature 1
`partition(Collection $collection, Callable[1,1] &$block)`
### Parameters
* `collection` --- A collection of things to partition.
Return type(s): `Tuple[Array, Array]`.
### Examples
Partition array of empty strings, results in e.g. `[[''], [b, c]]`
```puppet
['', b, c].partition |$s| { $s.empty }
```
Partition array of strings using index, results in e.g. `[['', 'ab'], ['b']]`
```puppet
['', b, ab].partition |$i, $s| { $i == 2 or $s.empty }
```
Partition hash of strings by key-value pair, results in e.g. `[[['b', []]], [['a', [1, 2]]]]`
```puppet
{ a => [1, 2], b => [] }.partition |$kv| { $kv[1].empty }
```
Partition hash of strings by key and value, results in e.g. `[[['b', []]], [['a', [1, 2]]]]`
```puppet
{ a => [1, 2], b => [] }.partition |$k, $v| { $v.empty }
```
Signature 2
`partition(Array $array, Callable[2,2] &$block)`
Signature 3
`partition(Collection $collection, Callable[2,2] &$block)`
## `realize`
Make a virtual object real. This is useful
when you want to know the name of the virtual object and don't want to
bother with a full collection. It is slightly faster than a collection,
and, of course, is a bit shorter. You must pass the object using a
reference; e.g.: `realize User[luke]`.
`realize()`
## `reduce`
Applies a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
to every value in a data structure from the first argument, carrying over the returned
value of each iteration, and returns the result of the lambda's final iteration. This
lets you create a new value or data structure by combining values from the first
argument's data structure.
This function takes two mandatory arguments, in this order:
1. An array, hash, or other iterable object that the function will iterate over.
2. A lambda, which the function calls for each element in the first argument. It takes
two mandatory parameters:
1. A memo value that is overwritten after each iteration with the iteration's result.
2. A second value that is overwritten after each iteration with the next value in the
function's first argument.
`$data.reduce |$memo, $value| { ... }`
or
`reduce($data) |$memo, $value| { ... }`
You can also pass an optional "start memo" value as an argument, such as `start` below:
`$data.reduce(start) |$memo, $value| { ... }`
or
`reduce($data, start) |$memo, $value| { ... }`
When the first argument (`$data` in the above example) is an array, Puppet passes each
of the data structure's values in turn to the lambda's parameters. When the first
argument is a hash, Puppet converts each of the hash's values to an array in the form
`[key, value]`.
If you pass a start memo value, Puppet executes the lambda with the provided memo value
and the data structure's first value. Otherwise, Puppet passes the structure's first two
values to the lambda.
Puppet calls the lambda for each of the data structure's remaining values. For each
call, it passes the result of the previous call as the first parameter (`$memo` in the
above examples) and the next value from the data structure as the second parameter
(`$value`).
```puppet
# Reduce the array $data, returning the sum of all values in the array.
$data = [1, 2, 3]
$sum = $data.reduce |$memo, $value| { $memo + $value }
# $sum contains 6
# Reduce the array $data, returning the sum of a start memo value and all values in the
# array.
$data = [1, 2, 3]
$sum = $data.reduce(4) |$memo, $value| { $memo + $value }
# $sum contains 10
# Reduce the hash $data, returning the sum of all values and concatenated string of all
# keys.
$data = {a => 1, b => 2, c => 3}
$combine = $data.reduce |$memo, $value| {
$string = "${memo[0]}${value[0]}"
$number = $memo[1] + $value[1]
[$string, $number]
}
# $combine contains [abc, 6]
```
```puppet
# Reduce the array $data, returning the sum of all values in the array and starting
# with $memo set to an arbitrary value instead of $data's first value.
$data = [1, 2, 3]
$sum = $data.reduce(4) |$memo, $value| { $memo + $value }
# At the start of the lambda's first iteration, $memo contains 4 and $value contains 1.
# After all iterations, $sum contains 10.
# Reduce the hash $data, returning the sum of all values and concatenated string of
# all keys, and starting with $memo set to an arbitrary array instead of $data's first
# key-value pair.
$data = {a => 1, b => 2, c => 3}
$combine = $data.reduce( [d, 4] ) |$memo, $value| {
$string = "${memo[0]}${value[0]}"
$number = $memo[1] + $value[1]
[$string, $number]
}
# At the start of the lambda's first iteration, $memo contains [d, 4] and $value
# contains [a, 1].
# $combine contains [dabc, 10]
```
```puppet
# Reduce a hash of hashes $data, merging defaults into the inner hashes.
$data = {
'connection1' => {
'username' => 'user1',
'password' => 'pass1',
},
'connection_name2' => {
'username' => 'user2',
'password' => 'pass2',
},
}
$defaults = {
'maxActive' => '20',
'maxWait' => '10000',
'username' => 'defaultuser',
'password' => 'defaultpass',
}
$merged = $data.reduce( {} ) |$memo, $x| {
$memo + { $x[0] => $defaults + $data[$x[0]] }
}
# At the start of the lambda's first iteration, $memo is set to {}, and $x is set to
# the first [key, value] tuple. The key in $data is, therefore, given by $x[0]. In
# subsequent rounds, $memo retains the value returned by the expression, i.e.
# $memo + { $x[0] => $defaults + $data[$x[0]] }.
```
Signature 1
`reduce(Iterable $enumerable, Callable[2,2] &$block)`
Signature 2
`reduce(Iterable $enumerable, Any $memo, Callable[2,2] &$block)`
## `regsubst`
Performs regexp replacement on a string or array of strings.
Signature 1
`regsubst(Variant[Array[String],String] $target, String $pattern, Variant[String,Hash[String,String]] $replacement, Optional[Optional[Pattern[/^[GEIM]*$/]]] $flags, Optional[Enum['N','E','S','U']] $encoding)`
### Parameters
* `target` --- The string or array of strings to operate on. If an array, the replacement will be
performed on each of the elements in the array, and the return value will be an array.
* `pattern` --- The regular expression matching the target string. If you want it anchored at the start
and or end of the string, you must do that with ^ and $ yourself.
* `replacement` --- Replacement string. Can contain backreferences to what was matched using \\0 (whole match),
\\1 (first set of parentheses), and so on.
If the second argument is a Hash, and the matched text is one of its keys, the corresponding value is the replacement string.
* `flags` --- Optional. String of single letter flags for how the regexp is interpreted (E, I, and M cannot be used
if pattern is a precompiled regexp):
- *E* Extended regexps
- *I* Ignore case in regexps
- *M* Multiline regexps
- *G* Global replacement; all occurrences of the regexp in each target string will be replaced. Without this, only the first occurrence will be replaced.
* `encoding` --- Deprecated and ignored parameter, included only for compatibility.
Return type(s): `Array[String]`, `String`. The result of the substitution. Result type is the same as for the target parameter.
### Examples
Get the third octet from the node's IP address:
```puppet
$i3 = regsubst($ipaddress,'^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$','\\3')
```
Signature 2
`regsubst(Variant[Array[String],String] $target, Variant[Regexp,Type[Regexp]] $pattern, Variant[String,Hash[String,String]] $replacement, Optional[Pattern[/^G?$/]] $flags)`
### Parameters
* `target` --- The string or array of strings to operate on. If an array, the replacement will be
performed on each of the elements in the array, and the return value will be an array.
* `pattern` --- The regular expression matching the target string. If you want it anchored at the start
and or end of the string, you must do that with ^ and $ yourself.
* `replacement` --- Replacement string. Can contain backreferences to what was matched using \\0 (whole match),
\\1 (first set of parentheses), and so on.
If the second argument is a Hash, and the matched text is one of its keys, the corresponding value is the replacement string.
* `flags` --- Optional. String of single letter flags for how the regexp is interpreted (E, I, and M cannot be used
if pattern is a precompiled regexp):
- *E* Extended regexps
- *I* Ignore case in regexps
- *M* Multiline regexps
- *G* Global replacement; all occurrences of the regexp in each target string will be replaced. Without this, only the first occurrence will be replaced.
Return type(s): `Array[String]`, `String`. The result of the substitution. Result type is the same as for the target parameter.
### Examples
Put angle brackets around each octet in the node's IP address:
```puppet
$x = regsubst($ipaddress, /([0-9]+)/, '<\\1>', 'G')
```
## `require`
Requires the specified classes.
Evaluate one or more classes, adding the required class as a dependency.
The relationship metaparameters work well for specifying relationships
between individual resources, but they can be clumsy for specifying
relationships between classes. This function is a superset of the
`include` function, adding a class relationship so that the requiring
class depends on the required class.
Warning: using `require` in place of `include` can lead to unwanted dependency cycles.
For instance, the following manifest, with `require` instead of `include`, would produce a nasty
dependence cycle, because `notify` imposes a `before` between `File[/foo]` and `Service[foo]`:
```puppet
class myservice {
service { foo: ensure => running }
}
class otherstuff {
include myservice
file { '/foo': notify => Service[foo] }
}
```
Note that this function only works with clients 0.25 and later, and it will
fail if used with earlier clients.
You must use the class's full name;
relative names are not allowed. In addition to names in string form,
you may also directly use Class and Resource Type values that are produced when evaluating
resource and relationship expressions.
- Since 4.0.0 Class and Resource types, absolute names
- Since 4.7.0 Returns an `Array[Type[Class]]` with references to the required classes
`require(Any *$names)`
## `return`
Makes iteration continue with the next value, optionally with a given value for this iteration.
If a value is not given it defaults to `undef`
`return(Optional[Any] $value)`
## `reverse_each`
Reverses the order of the elements of something that is iterable and optionally runs a
[lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html) for each
element.
This function takes one to two arguments:
1. An `Iterable` that the function will iterate over.
2. An optional lambda, which the function calls for each element in the first argument. It must
request one parameter.
```puppet
$data.reverse_each |$parameter| { <PUPPET CODE BLOCK> }
```
or
```puppet
$reverse_data = $data.reverse_each
```
or
```puppet
reverse_each($data) |$parameter| { <PUPPET CODE BLOCK> }
```
or
```puppet
$reverse_data = reverse_each($data)
```
When no second argument is present, Puppet returns an `Iterable` that represents the reverse
order of its first argument. This allows methods on `Iterable` to be chained.
When a lambda is given as the second argument, Puppet iterates the first argument in reverse
order and passes each value in turn to the lambda, then returns `undef`.
```puppet
# Puppet will log a notice for each of the three items
# in $data in reverse order.
$data = [1,2,3]
$data.reverse_each |$item| { notice($item) }
```
When no second argument is present, Puppet returns a new `Iterable` which allows it to
be directly chained into another function that takes an `Iterable` as an argument.
```puppet
# For the array $data, return an array containing each
# value multiplied by 10 in reverse order
$data = [1,2,3]
$transformed_data = $data.reverse_each.map |$item| { $item * 10 }
# $transformed_data is set to [30,20,10]
```
```puppet
# For the array $data, return an array containing each
# value multiplied by 10 in reverse order
$data = [1,2,3]
$transformed_data = map(reverse_each($data)) |$item| { $item * 10 }
# $transformed_data is set to [30,20,10]
```
Signature 1
`reverse_each(Iterable $iterable)`
Signature 2
`reverse_each(Iterable $iterable, Callable[1,1] &$block)`
## `round`
Returns an `Integer` value rounded to the nearest value.
Takes a single `Numeric` value as an argument.
```puppet
notice(round(2.9)) # would notice 3
notice(round(2.1)) # would notice 2
notice(round(-2.9)) # would notice -3
```
`round(Numeric $val)`
## `rstrip`
Strips trailing spaces from a String
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion removes all trailing ASCII white space characters such as space, tab, newline, and return.
It does not remove other space-like characters like hard space (Unicode U+00A0). (Tip, `/^[[:space:]]/` regular expression
matches all space-like characters).
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
```puppet
" hello\n\t".rstrip()
rstrip(" hello\n\t")
```
Would both result in `"hello"`
```puppet
[" hello\n\t", " hi\n\t"].rstrip()
rstrip([" hello\n\t", " hi\n\t"])
```
Would both result in `['hello', 'hi']`
Signature 1
`rstrip(Numeric $arg)`
Signature 2
`rstrip(String $arg)`
Signature 3
`rstrip(Iterable[Variant[String, Numeric]] $arg)`
## `scanf`
Scans a string and returns an array of one or more converted values based on the given format string.
See the documentation of Ruby's String#scanf method for details about the supported formats (which
are similar but not identical to the formats used in Puppet's `sprintf` function.)
This function takes two mandatory arguments: the first is the string to convert, and the second is
the format string. The result of the scan is an array, with each successfully scanned and transformed value.
The scanning stops if a scan is unsuccessful, and the scanned result up to that point is returned. If there
was no successful scan, the result is an empty array.
"42".scanf("%i")
You can also optionally pass a lambda to scanf, to do additional validation or processing.
"42".scanf("%i") |$x| {
unless $x[0] =~ Integer {
fail "Expected a well formed integer value, got '$x[0]'"
}
$x[0]
}
`scanf(String $data, String $format, Optional[Callable] &$block)`
## `sha1`
Returns a SHA1 hash value from a provided string.
`sha1()`
## `sha256`
Returns a SHA256 hash value from a provided string.
`sha256()`
## `shellquote`
\
Quote and concatenate arguments for use in Bourne shell.
Each argument is quoted separately, and then all are concatenated
with spaces. If an argument is an array, the elements of that
array is interpolated within the rest of the arguments; this makes
it possible to have an array of arguments and pass that array to
shellquote instead of having to specify each argument
individually in the call.
`shellquote()`
## `size`
The same as length() - returns the size of an Array, Hash, String, or Binary value.
`size(Variant[Collection, String, Binary] $arg)`
## `slice`
Slices an array or hash into pieces of a given size.
This function takes two mandatory arguments: the first should be an array or hash, and the second specifies
the number of elements to include in each slice.
When the first argument is a hash, each key value pair is counted as one. For example, a slice size of 2 will produce
an array of two arrays with key, and value.
```puppet
$a.slice(2) |$entry| { notice "first ${$entry[0]}, second ${$entry[1]}" }
$a.slice(2) |$first, $second| { notice "first ${first}, second ${second}" }
```
The function produces a concatenated result of the slices.
```puppet
slice([1,2,3,4,5,6], 2) # produces [[1,2], [3,4], [5,6]]
slice(Integer[1,6], 2) # produces [[1,2], [3,4], [5,6]]
slice(4,2) # produces [[0,1], [2,3]]
slice('hello',2) # produces [[h, e], [l, l], [o]]
```
```puppet
$a.slice($n) |$x| { ... }
slice($a) |$x| { ... }
```
The lambda should have either one parameter (receiving an array with the slice), or the same number
of parameters as specified by the slice size (each parameter receiving its part of the slice).
If there are fewer remaining elements than the slice size for the last slice, it will contain the remaining
elements. If the lambda has multiple parameters, excess parameters are set to undef for an array, or
to empty arrays for a hash.
```puppet
$a.slice(2) |$first, $second| { ... }
```
Signature 1
`slice(Hash[Any, Any] $hash, Integer[1, default] $slice_size, Optional[Callable] &$block)`
Signature 2
`slice(Iterable $enumerable, Integer[1, default] $slice_size, Optional[Callable] &$block)`
## `sort`
Sorts an Array numerically or lexicographically or the characters of a String lexicographically.
Please note: This function is based on Ruby String comparison and as such may not be entirely UTF8 compatible.
To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
This function is compatible with the function `sort()` in `stdlib`.
* Comparison of characters in a string always uses a system locale and may not be what is expected for a particular locale
* Sorting is based on Ruby's `<=>` operator unless a lambda is given that performs the comparison.
* comparison of strings is case dependent (use lambda with `compare($a,$b)` to ignore case)
* comparison of mixed data types raises an error (if there is the need to sort mixed data types use a lambda)
Also see the `compare()` function for information about comparable data types in general.
```puppet
notice(sort("xadb")) # notices 'abdx'
```
```puppet
notice(sort([3,6,2])) # notices [2, 3, 6]
```
```puppet
notice(sort([3,6,2]) |$a,$b| { compare($a, $b) }) # notices [2, 3, 6]
notice(sort([3,6,2]) |$a,$b| { compare($b, $a) }) # notices [6, 3, 2]
```
```puppet
notice(sort(['A','b','C'])) # notices ['A', 'C', 'b']
notice(sort(['A','b','C']) |$a,$b| { compare($a, $b) }) # notices ['A', 'b', 'C']
notice(sort(['A','b','C']) |$a,$b| { compare($a, $b, true) }) # notices ['A', 'b', 'C']
notice(sort(['A','b','C']) |$a,$b| { compare($a, $b, false) }) # notices ['A','C', 'b']
```
```puppet
notice(sort(['b', 3, 'a', 2]) |$a, $b| {
case [$a, $b] {
[String, Numeric] : { 1 }
[Numeric, String] : { -1 }
default: { compare($a, $b) }
}
})
```
Would notice `[2,3,'a','b']`
Signature 1
`sort(String $string_value, Optional[Callable[2,2]] &$block)`
Signature 2
`sort(Array $array_value, Optional[Callable[2,2]] &$block)`
## `split`
Splits a string into an array using a given pattern.
The pattern can be a string, regexp or regexp type.
```puppet
$string = 'v1.v2:v3.v4'
$array_var1 = split($string, /:/)
$array_var2 = split($string, '[.]')
$array_var3 = split($string, Regexp['[.:]'])
#`$array_var1` now holds the result `['v1.v2', 'v3.v4']`,
# while `$array_var2` holds `['v1', 'v2:v3', 'v4']`, and
# `$array_var3` holds `['v1', 'v2', 'v3', 'v4']`.
```
Note that in the second example, we split on a literal string that contains
a regexp meta-character (`.`), which must be escaped. A simple
way to do that for a single character is to enclose it in square
brackets; a backslash will also escape a single character.
Signature 1
`split(String $str, String $pattern)`
Signature 2
`split(String $str, Regexp $pattern)`
Signature 3
`split(String $str, Type[Regexp] $pattern)`
Signature 4
`split(Sensitive[String] $sensitive, String $pattern)`
Signature 5
`split(Sensitive[String] $sensitive, Regexp $pattern)`
Signature 6
`split(Sensitive[String] $sensitive, Type[Regexp] $pattern)`
## `sprintf`
Perform printf-style formatting of text.
The first parameter is format string describing how the rest of the parameters should be formatted.
See the documentation for the [`Kernel::sprintf` function](https://ruby-doc.org/core/Kernel.html)
in Ruby for details.
To use [named format](https://idiosyncratic-ruby.com/49-what-the-format.html) arguments, provide a
hash containing the target string values as the argument to be formatted. For example:
```puppet
notice sprintf(\"%<x>s : %<y>d\", { 'x' => 'value is', 'y' => 42 })
```
This statement produces a notice of `value is : 42`.
`sprintf()`
## `step`
When no block is given, Puppet returns a new `Iterable` which allows it to be directly chained into
another function that takes an `Iterable` as an argument.
```puppet
# For the array $data, return an array, set to the first element and each 5th successor element, in reverse
# order multiplied by 10
$data = Integer[0,20]
$transformed_data = $data.step(5).map |$item| { $item * 10 }
$transformed_data contains [0,50,100,150,200]
```
```puppet
# For the array $data, return an array, set to the first and each 5th
# successor, in reverse order, multiplied by 10
$data = Integer[0,20]
$transformed_data = map(step($data, 5)) |$item| { $item * 10 }
$transformed_data contains [0,50,100,150,200]
```
Signature 1
`step(Iterable $iterable, Integer[1] $step)`
Signature 2
`step(Iterable $iterable, Integer[1] $step, Callable[1,1] &$block)`
## `strftime`
Formats timestamp or timespan according to the directives in the given format string. The directives begins with a percent (%) character.
Any text not listed as a directive will be passed through to the output string.
A third optional timezone argument can be provided. The first argument will then be formatted to represent a local time in that
timezone. The timezone can be any timezone that is recognized when using the '%z' or '%Z' formats, or the word 'current', in which
case the current timezone of the evaluating process will be used. The timezone argument is case insensitive.
The default timezone, when no argument is provided, or when using the keyword `default`, is 'UTC'.
The directive consists of a percent (%) character, zero or more flags, optional minimum field width and
a conversion specifier as follows:
```
%[Flags][Width]Conversion
```
### Flags that controls padding
| Flag | Meaning
| ---- | ---------------
| - | Don't pad numerical output
| _ | Use spaces for padding
| 0 | Use zeros for padding
### `Timestamp` specific flags
| Flag | Meaning
| ---- | ---------------
| # | Change case
| ^ | Use uppercase
| : | Use colons for %z
### Format directives applicable to `Timestamp` (names and padding can be altered using flags):
**Date (Year, Month, Day):**
| Format | Meaning |
| ------ | ------- |
| Y | Year with century, zero-padded to at least 4 digits |
| C | year / 100 (rounded down such as 20 in 2009) |
| y | year % 100 (00..99) |
| m | Month of the year, zero-padded (01..12) |
| B | The full month name ("January") |
| b | The abbreviated month name ("Jan") |
| h | Equivalent to %b |
| d | Day of the month, zero-padded (01..31) |
| e | Day of the month, blank-padded ( 1..31) |
| j | Day of the year (001..366) |
**Time (Hour, Minute, Second, Subsecond):**
| Format | Meaning |
| ------ | ------- |
| H | Hour of the day, 24-hour clock, zero-padded (00..23) |
| k | Hour of the day, 24-hour clock, blank-padded ( 0..23) |
| I | Hour of the day, 12-hour clock, zero-padded (01..12) |
| l | Hour of the day, 12-hour clock, blank-padded ( 1..12) |
| P | Meridian indicator, lowercase ("am" or "pm") |
| p | Meridian indicator, uppercase ("AM" or "PM") |
| M | Minute of the hour (00..59) |
| S | Second of the minute (00..60) |
| L | Millisecond of the second (000..999). Digits under millisecond are truncated to not produce 1000 |
| N | Fractional seconds digits, default is 9 digits (nanosecond). Digits under a specified width are truncated to avoid carry up |
**Time (Hour, Minute, Second, Subsecond):**
| Format | Meaning |
| ------ | ------- |
| z | Time zone as hour and minute offset from UTC (e.g. +0900) |
| :z | hour and minute offset from UTC with a colon (e.g. +09:00) |
| ::z | hour, minute and second offset from UTC (e.g. +09:00:00) |
| Z | Abbreviated time zone name or similar information. (OS dependent) |
**Weekday:**
| Format | Meaning |
| ------ | ------- |
| A | The full weekday name ("Sunday") |
| a | The abbreviated name ("Sun") |
| u | Day of the week (Monday is 1, 1..7) |
| w | Day of the week (Sunday is 0, 0..6) |
**ISO 8601 week-based year and week number:**
The first week of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
| Format | Meaning |
| ------ | ------- |
| G | The week-based year |
| g | The last 2 digits of the week-based year (00..99) |
| V | Week number of the week-based year (01..53) |
**Week number:**
The first week of YYYY that starts with a Sunday or Monday (according to %U
or %W). The days in the year before the first week are in week 0.
| Format | Meaning |
| ------ | ------- |
| U | Week number of the year. The week starts with Sunday. (00..53) |
| W | Week number of the year. The week starts with Monday. (00..53) |
**Seconds since the Epoch:**
| Format | Meaning |
| ------ | ------- |
| s | Number of seconds since 1970-01-01 00:00:00 UTC. |
**Literal string:**
| Format | Meaning |
| ------ | ------- |
| n | Newline character (\n) |
| t | Tab character (\t) |
| % | Literal "%" character |
**Combination:**
| Format | Meaning |
| ------ | ------- |
| c | date and time (%a %b %e %T %Y) |
| D | Date (%m/%d/%y) |
| F | The ISO 8601 date format (%Y-%m-%d) |
| v | VMS date (%e-%^b-%4Y) |
| x | Same as %D |
| X | Same as %T |
| r | 12-hour time (%I:%M:%S %p) |
| R | 24-hour time (%H:%M) |
| T | 24-hour time (%H:%M:%S) |
```puppet
$timestamp = Timestamp('2016-08-24T12:13:14')
# Notice the timestamp using a format that notices the ISO 8601 date format
notice($timestamp.strftime('%F')) # outputs '2016-08-24'
# Notice the timestamp using a format that notices weekday, month, day, time (as UTC), and year
notice($timestamp.strftime('%c')) # outputs 'Wed Aug 24 12:13:14 2016'
# Notice the timestamp using a specific timezone
notice($timestamp.strftime('%F %T %z', 'PST')) # outputs '2016-08-24 04:13:14 -0800'
# Notice the timestamp using timezone that is current for the evaluating process
notice($timestamp.strftime('%F %T', 'current')) # outputs the timestamp using the timezone for the current process
```
### Format directives applicable to `Timespan`:
| Format | Meaning |
| ------ | ------- |
| D | Number of Days |
| H | Hour of the day, 24-hour clock |
| M | Minute of the hour (00..59) |
| S | Second of the minute (00..59) |
| L | Millisecond of the second (000..999). Digits under millisecond are truncated to not produce 1000. |
| N | Fractional seconds digits, default is 9 digits (nanosecond). Digits under a specified length are truncated to avoid carry up |
The format directive that represents the highest magnitude in the format will be allowed to overflow.
I.e. if no "%D" is used but a "%H" is present, then the hours will be more than 23 in case the
timespan reflects more than a day.
```puppet
$duration = Timespan({ hours => 3, minutes => 20, seconds => 30 })
# Notice the duration using a format that outputs <hours>:<minutes>:<seconds>
notice($duration.strftime('%H:%M:%S')) # outputs '03:20:30'
# Notice the duration using a format that outputs <minutes>:<seconds>
notice($duration.strftime('%M:%S')) # outputs '200:30'
```
- Since 4.8.0
Signature 1
`strftime(Timespan $time_object, String $format)`
Signature 2
`strftime(Timestamp $time_object, String $format, Optional[String] $timezone)`
Signature 3
`strftime(String $format, Optional[String] $timezone)`
## `strip`
Strips leading and trailing spaces from a String
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String` the conversion removes all leading and trailing ASCII white space characters such as space, tab, newline, and return.
It does not remove other space-like characters like hard space (Unicode U+00A0). (Tip, `/^[[:space:]]/` regular expression
matches all space-like characters).
* For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
```puppet
" hello\n\t".strip()
strip(" hello\n\t")
```
Would both result in `"hello"`
```puppet
[" hello\n\t", " hi\n\t"].strip()
strip([" hello\n\t", " hi\n\t"])
```
Would both result in `['hello', 'hi']`
Signature 1
`strip(Numeric $arg)`
Signature 2
`strip(String $arg)`
Signature 3
`strip(Iterable[Variant[String, Numeric]] $arg)`
## `tag`
Add the specified tags to the containing class
or definition. All contained objects will then acquire that tag, also.
`tag()`
## `tagged`
A boolean function that
tells you whether the current container is tagged with the specified tags.
The tags are ANDed, so that all of the specified tags must be included for
the function to return true.
`tagged()`
## `template`
Loads an ERB template from a module, evaluates it, and returns the resulting
value as a string.
The argument to this function should be a `<MODULE NAME>/<TEMPLATE FILE>`
reference, which will load `<TEMPLATE FILE>` from a module's `templates`
directory. (For example, the reference `apache/vhost.conf.erb` will load the
file `<MODULES DIRECTORY>/apache/templates/vhost.conf.erb`.)
This function can also accept:
* An absolute path, which can load a template file from anywhere on disk.
* Multiple arguments, which will evaluate all of the specified templates and
return their outputs concatenated into a single string.
`template()`
## `then`
Calls a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
with the given argument unless the argument is `undef`.
Returns `undef` if the argument is `undef`, and otherwise the result of giving the
argument to the lambda.
This is useful to process a sequence of operations where an intermediate
result may be `undef` (which makes the entire sequence `undef`).
The `then` function is especially useful with the function `dig` which
performs in a similar way "digging out" a value in a complex structure.
```puppet
$data = {a => { b => [{x => 10, y => 20}, {x => 100, y => 200}]}}
notice $data.dig(a, b, 1, x).then |$x| { $x * 2 }
```
Would notice the value 200
Contrast this with:
```puppet
$data = {a => { b => [{x => 10, y => 20}, {not_x => 100, why => 200}]}}
notice $data.dig(a, b, 1, x).then |$x| { $x * 2 }
```
Which would notice `undef` since the last lookup of 'x' results in `undef` which
is returned (without calling the lambda given to the `then` function).
As a result there is no need for conditional logic or a temporary (non local)
variable as the result is now either the wanted value (`x`) multiplied
by 2 or `undef`.
Calls to `then` can be chained. In the next example, a structure is using an offset based on
using 1 as the index to the first element (instead of 0 which is used in the language).
We are not sure if user input actually contains an index at all, or if it is
outside the range of available names.args.
```puppet
# Names to choose from
$names = ['Ringo', 'Paul', 'George', 'John']
# Structure where 'beatle 2' is wanted (but where the number refers
# to 'Paul' because input comes from a source using 1 for the first
# element).
$data = ['singer', { beatle => 2 }]
$picked = assert_type(String,
# the data we are interested in is the second in the array,
# a hash, where we want the value of the key 'beatle'
$data.dig(1, 'beatle')
# and we want the index in $names before the given index
.then |$x| { $names[$x-1] }
# so we can construct a string with that beatle's name
.then |$x| { "Picked Beatle '${x}'" }
)
notice $picked
```
Would notice "Picked Beatle 'Paul'", and would raise an error if the result
was not a String.
* Since 4.5.0
`then(Any $arg, Callable[1,1] &$block)`
## `tree_each`
Runs a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
recursively and repeatedly using values from a data structure, then returns the unchanged data structure, or if
a lambda is not given, returns an `Iterator` for the tree.
This function takes one mandatory argument, one optional, and an optional block in this order:
1. An `Array`, `Hash`, `Iterator`, or `Object` that the function will iterate over.
2. An optional hash with the options:
* `include_containers` => `Optional[Boolean]` # default `true` - if containers should be given to the lambda
* `include_values` => `Optional[Boolean]` # default `true` - if non containers should be given to the lambda
* `include_root` => `Optional[Boolean]` # default `true` - if the root container should be given to the lambda
* `container_type` => `Optional[Type[Variant[Array, Hash, Object]]]` # a type that determines what a container is - can only
be set to a type that matches the default `Variant[Array, Hash, Object]`.
* `order` => `Enum[depth_first, breadth_first]` # default ´depth_first`, the order in which elements are visited
* `include_refs` => `Optional[Boolean]` # default `false`, if attributes in objects marked as bing of `reference` kind
should be included.
3. An optional lambda, which the function calls for each element in the first argument. It must
accept one or two arguments; either `$path`, and `$value`, or just `$value`.
`$data.tree_each |$path, $value| { <PUPPET CODE BLOCK> }`
`$data.tree_each |$value| { <PUPPET CODE BLOCK> }`
or
`tree_each($data) |$path, $value| { <PUPPET CODE BLOCK> }`
`tree_each($data) |$value| { <PUPPET CODE BLOCK> }`
The parameter `$path` is always given as an `Array` containing the path that when applied to
the tree as `$data.dig(*$path) yields the `$value`.
The `$value` is the value at that path.
For `Array` values, the path will contain `Integer` entries with the array index,
and for `Hash` values, the path will contain the hash key, which may be `Any` value.
For `Object` containers, the entry is the name of the attribute (a `String`).
The tree is walked in either depth-first order, or in breadth-first order under the control of the
`order` option, yielding each `Array`, `Hash`, `Object`, and each entry/attribute.
The default is `depth_first` which means that children are processed before siblings.
An order of `breadth_first` means that siblings are processed before children.
```puppet
[1, [2, 3], 4]
```
If containers are skipped, results in:
* `depth_first` order `1`, `2`, `3`, `4`
* `breadth_first` order `1`, `4`,`2`, `3`
If containers and root are included, results in:
* `depth_first` order `[1, [2, 3], 4]`, `1`, `[2, 3]`, `2`, `3`, `4`
* `breadth_first` order `[1, [2, 3], 4]`, `1`, `[2, 3]`, `4`, `2`, `3`
Typical use of the `tree_each` function include:
* a more efficient way to iterate over a tree than first using `flatten` on an array
as that requires a new (potentially very large) array to be created
* when a tree needs to be transformed and 'pretty printed' in a template
* avoiding having to write a special recursive function when tree contains hashes (flatten does
not work on hashes)
```puppet
$data = [1, 2, [3, [4, 5]]]
$data.tree_each({include_containers => false}) |$v| { notice "$v" }
```
This would call the lambda 5 times with with the following values in sequence: `1`, `2`, `3`, `4`, `5`
```puppet
$data = [1, 2, [3, [4, 5]]]
$data.tree_each |$v| { notice "$v" }
```
This would call the lambda 7 times with the following values in sequence:
`1`, `2`, `[3, [4, 5]]`, `3`, `[4, 5]`, `4`, `5`
```puppet
$data = [1, 2, [3, [4, 5]]]
$data.tree_each({include_values => false, include_root => false}) |$v| { notice "$v" }
```
This would call the lambda 2 times with the following values in sequence:
`[3, [4, 5]]`, `[4, 5]`
Any Puppet Type system data type can be used to filter what is
considered to be a container, but it must be a narrower type than one of
the default `Array`, `Hash`, `Object` types - for example it is not possible to make a
`String` be a container type.
```puppet
$data = [1, {a => 'hello', b => [100, 200]}, [3, [4, 5]]]
$data.tree_each({container_type => Array, include_containers => false} |$v| { notice "$v" }
```
Would call the lambda 5 times with `1`, `{a => 'hello', b => [100, 200]}`, `3`, `4`, `5`
**Chaining** When calling `tree_each` without a lambda the function produces an `Iterator`
that can be chained into another iteration. Thus it is easy to use one of:
* `reverse_each` - get "leaves before root"
* `filter` - prune the tree
* `map` - transform each element
Note than when chaining, the value passed on is a `Tuple` with `[path, value]`.
```puppet
# A tree of some complexity (here very simple for readability)
$tree = [
{ name => 'user1', status => 'inactive', id => '10'},
{ name => 'user2', status => 'active', id => '20'}
]
notice $tree.tree_each.filter |$v| {
$value = $v[1]
$value =~ Hash and $value[status] == active
}
```
Would notice `[[[1], {name => user2, status => active, id => 20}]]`, which can then be processed
further as each filtered result appears as a `Tuple` with `[path, value]`.
For general examples that demonstrates iteration see the Puppet
[iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
documentation.
Signature 1
`tree_each(Variant[Iterator, Array, Hash, Object] $tree, Optional[OptionsType] $options, Callable[2,2] &$block)`
Signature 2
`tree_each(Variant[Iterator, Array, Hash, Object] $tree, Optional[OptionsType] $options, Callable[1,1] &$block)`
Signature 3
`tree_each(Variant[Iterator, Array, Hash, Object] $tree, Optional[OptionsType] $options)`
## `type`
Returns the data type of a given value with a given degree of generality.
```puppet
type InferenceFidelity = Enum[generalized, reduced, detailed]
function type(Any $value, InferenceFidelity $fidelity = 'detailed') # returns Type
```
``` puppet
notice type(42) =~ Type[Integer]
```
Would notice `true`.
By default, the best possible inference is made where all details are retained.
This is good when the type is used for further type calculations but is overwhelmingly
rich in information if it is used in a error message.
The optional argument `$fidelity` may be given as (from lowest to highest fidelity):
* `generalized` - reduces to common type and drops size constraints
* `reduced` - reduces to common type in collections
* `detailed` - (default) all details about inferred types is retained
``` puppet
notice type([3.14, 42], 'generalized')
notice type([3.14, 42], 'reduced'')
notice type([3.14, 42], 'detailed')
notice type([3.14, 42])
```
Would notice the four values:
1. `Array[Numeric]`
2. `Array[Numeric, 2, 2]`
3. `Tuple[Float[3.14], Integer[42,42]]]`
4. `Tuple[Float[3.14], Integer[42,42]]]`
Signature 1
`type(Any $value, Optional[Enum[detailed]] $inference_method)`
Signature 2
`type(Any $value, Enum[reduced] $inference_method)`
Signature 3
`type(Any $value, Enum[generalized] $inference_method)`
## `unique`
Produces a unique set of values from an `Iterable` argument.
* If the argument is a `String`, the unique set of characters are returned as a new `String`.
* If the argument is a `Hash`, the resulting hash associates a set of keys with a set of unique values.
* For all other types of `Iterable` (`Array`, `Iterator`) the result is an `Array` with
a unique set of entries.
* Comparison of all `String` values are case sensitive.
* An optional code block can be given - if present it is given each candidate value and its return is used instead of the given value. This
enables transformation of the value before comparison. The result of the lambda is only used for comparison.
* The optional code block when used with a hash is given each value (not the keys).
```puppet
# will produce 'abc'
"abcaabb".unique
```
```puppet
# will produce ['a', 'b', 'c']
['a', 'b', 'c', 'a', 'a', 'b'].unique
```
```puppet
# will produce { ['a', 'b'] => [10], ['c'] => [20]}
{'a' => 10, 'b' => 10, 'c' => 20}.unique
# will produce { 'a' => 10, 'c' => 20 } (use first key with first value)
Hash.new({'a' => 10, 'b' => 10, 'c' => 20}.unique.map |$k, $v| { [ $k[0] , $v[0]] })
# will produce { 'b' => 10, 'c' => 20 } (use last key with first value)
Hash.new({'a' => 10, 'b' => 10, 'c' => 20}.unique.map |$k, $v| { [ $k[-1] , $v[0]] })
```
```
# will produce [3, 2, 1]
[1,2,2,3,3].reverse_each.unique
```
```puppet
# will produce [['sam', 'smith'], ['sue', 'smith']]
[['sam', 'smith'], ['sam', 'brown'], ['sue', 'smith']].unique |$x| { $x[0] }
# will produce [['sam', 'smith'], ['sam', 'brown']]
[['sam', 'smith'], ['sam', 'brown'], ['sue', 'smith']].unique |$x| { $x[1] }
# will produce ['aBc', 'bbb'] (using a lambda to make comparison using downcased (%d) strings)
['aBc', 'AbC', 'bbb'].unique |$x| { String($x,'%d') }
# will produce {[a] => [10], [b, c, d, e] => [11, 12, 100]}
{a => 10, b => 11, c => 12, d => 100, e => 11}.unique |$v| { if $v > 10 { big } else { $v } }
```
Note that for `Hash` the result is slightly different than for the other data types. For those the result contains the
*first-found* unique value, but for `Hash` it contains associations from a set of keys to the set of values clustered by the
equality lambda (or the default value equality if no lambda was given). This makes the `unique` function more versatile for hashes
in general, while requiring that the simple computation of "hash's unique set of values" is performed as `$hsh.map |$k, $v| { $v }.unique`.
(Generally, it's meaningless to compute the unique set of hash keys because they are unique by definition. However, the
situation can change if the hash keys are processed with a different lambda for equality. For this unique computation,
first map the hash to an array of its keys.)
If the more advanced clustering is wanted for one of the other data types, simply transform it into a `Hash` as shown in the
following example.
```puppet
# Array ['a', 'b', 'c'] to Hash with index results in
# {0 => 'a', 1 => 'b', 2 => 'c'}
Hash(['a', 'b', 'c'].map |$i, $v| { [$i, $v]})
# String "abc" to Hash with index results in
# {0 => 'a', 1 => 'b', 2 => 'c'}
Hash(Array("abc").map |$i,$v| { [$i, $v]})
"abc".to(Array).map |$i,$v| { [$i, $v]}.to(Hash)
```
Signature 1
`unique(String $string, Optional[Callable[String]] &$block)`
Signature 2
`unique(Hash $hash, Optional[Callable[Any]] &$block)`
Signature 3
`unique(Array $array, Optional[Callable[Any]] &$block)`
Signature 4
`unique(Iterable $iterable, Optional[Callable[Any]] &$block)`
## `unwrap`
Unwraps a Sensitive value and returns the wrapped object.
Returns the Value itself, if it is not Sensitive.
```puppet
$plaintext = 'hunter2'
$pw = Sensitive.new($plaintext)
notice("Wrapped object is $pw") #=> Prints "Wrapped object is Sensitive [value redacted]"
$unwrapped = $pw.unwrap
notice("Unwrapped object is $unwrapped") #=> Prints "Unwrapped object is hunter2"
```
You can optionally pass a block to unwrap in order to limit the scope where the
unwrapped value is visible.
```puppet
$pw = Sensitive.new('hunter2')
notice("Wrapped object is $pw") #=> Prints "Wrapped object is Sensitive [value redacted]"
$pw.unwrap |$unwrapped| {
$conf = inline_template("password: ${unwrapped}\n")
Sensitive.new($conf)
} #=> Returns a new Sensitive object containing an interpolated config file
# $unwrapped is now out of scope
```
Signature 1
`unwrap(Sensitive $arg, Optional[Callable] &$block)`
Signature 2
`unwrap(Any $arg, Optional[Callable] &$block)`
## `upcase`
Converts a String, Array or Hash (recursively) into upper case.
This function is compatible with the stdlib function with the same name.
The function does the following:
* For a `String`, its upper case version is returned. This is done using Ruby system locale which handles some, but not all
special international up-casing rules (for example German double-s ß is upcased to "SS", whereas upper case double-s
is downcased to ß).
* For `Array` and `Hash` the conversion to upper case is recursive and each key and value must be convertible by
this function.
* When a `Hash` is converted, some keys could result in the same key - in those cases, the
latest key-value wins. For example if keys "aBC", and "abC" where both present, after upcase there would only be one
key "ABC".
* If the value is `Numeric` it is simply returned (this is for backwards compatibility).
* An error is raised for all other data types.
Please note: This function relies directly on Ruby's String implementation and as such may not be entirely UTF8 compatible.
To ensure best compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
```puppet
'hello'.upcase()
upcase('hello')
```
Would both result in `"HELLO"`
```puppet
['a', 'b'].upcase()
upcase(['a', 'b'])
```
Would both result in `['A', 'B']`
```puppet
{'a' => 'hello', 'b' => 'goodbye'}.upcase()
```
Would result in `{'A' => 'HELLO', 'B' => 'GOODBYE'}`
```puppet
['a', 'b', ['c', ['d']], {'x' => 'y'}].upcase
```
Would result in `['A', 'B', ['C', ['D']], {'X' => 'Y'}]`
Signature 1
`upcase(Numeric $arg)`
Signature 2
`upcase(String $arg)`
Signature 3
`upcase(Array[StringData] $arg)`
Signature 4
`upcase(Hash[StringData, StringData] $arg)`
## `values`
Returns the values of a hash as an Array
```puppet
$hsh = {"apples" => 3, "oranges" => 4 }
$hsh.values()
values($hsh)
# both results in the array [3, 4]
```
* Note that a hash in the puppet language accepts any data value (including `undef`) unless
it is constrained with a `Hash` data type that narrows the allowed data types.
* For an empty hash, an empty array is returned.
* The order of the values is the same as the order in the hash (typically the order in which they were added).
`values(Hash $hsh)`
## `versioncmp`
Compares two version numbers.
Prototype:
$result = versioncmp(a, b)
Where a and b are arbitrary version strings.
Optional parameter ignore_trailing_zeroes is used to ignore unnecessary
trailing version numbers like .0 or .0.00
This function returns:
* `1` if version a is greater than version b
* `0` if the versions are equal
* `-1` if version a is less than version b
This function uses the same version comparison algorithm used by Puppet's
`package` type.
`versioncmp(String $a, String $b, Optional[Boolean] $ignore_trailing_zeroes)`
## `warning`
Logs a message on the server at level `warning`.
`warning(Any *$values)`
### Parameters
* `*values` --- The values to log.
Return type(s): `Undef`.
## `with`
Calls a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
with the given arguments and returns the result.
Since a lambda's scope is
[local](https://puppet.com/docs/puppet/latest/lang_lambdas.html#lambda-scope)
to the lambda, you can use the `with` function to create private blocks of code within a
class using variables whose values cannot be accessed outside of the lambda.
```puppet
# Concatenate three strings into a single string formatted as a list.
$fruit = with("apples", "oranges", "bananas") |$x, $y, $z| {
"${x}, ${y}, and ${z}"
}
$check_var = $x
# $fruit contains "apples, oranges, and bananas"
# $check_var is undefined, as the value of $x is local to the lambda.
```
`with(Any *$arg, Callable &$block)`
## `yaml_data`
The `yaml_data` is a hiera 5 `data_hash` data provider function.
See [the configuration guide documentation](https://puppet.com/docs/puppet/latest/hiera_config_yaml_5.html#configuring-a-hierarchy-level-built-in-backends) for
how to use this function.
`yaml_data(Struct[{path=>String[1]}] $options, Puppet::LookupContext $context)`
|