File size: 71,912 Bytes
08c8a6d |
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 |
HANDBOOK Bjarne Däcker Robert Virding Erlang Handbook by Bjarne Däcker and Robert Virding Revision: Wed Sep 17 22:30:30 2014 +0200 Latest version of this handbook can be found at: http://opensource.erlang-solutions.com/erlang-handbook ISBN: 978-1-938616-04-4 Editor Omer Kilic Contributors The list of contributors can be found on the project repository. Conventions Syntax specifications are set using this monotype font. Square brackets ([ ]) enclose optional parts. Terms beginning with an uppercase letter like Integer shall then be replaced by some suitable value. Terms beginning with a lowercase letter like end are reserved words in Erlang. A vertical bar (|) separates alternatives, like Integer | Float. Errata and Improvements This is a live document so please file corrections and suggestions for improvement about the content using the issue tracker at https://github.com/esl/erlang-handbook. You may also fork this repository and send a pull request with your suggested fixes and improvements. New revisions of this document will be published after major corrections. This text is made available under a Creative Commons Attribution-ShareAlike 3.0 License. You are free to copy, distribute and transmit it under the license terms defined at http://creativecommons.org/licenses/ by-sa/3.0 Contents 1 Background, or Why Erlang is that it is 2 Structure of an Erlang program 2.1 Module syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Module attributes 2.2.1 Pre-defined module attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Macro and record definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.3 File inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Reserved words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Data types (terms) 3.1 Unary data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Booleans 3.1.3 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.4 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.5 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.6 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.7 Pids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.8 Funs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Compound data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.1 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.2 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.4 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.5 Binaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Type conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Variables 4.2 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.1 Match operator (=) in patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String prefix in patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.2 3 4 4 4 4 5 5 6 6 7 8 8 8 8 8 9 9 9 9 9 9 9 10 10 11 11 12 12 14 14 15 15 15 2 4.2.3 Expressions in patterns 4.2.4 Matching binaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Functions 5.1 Function definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.1 Term comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.2 Arithmetic expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.3 Boolean expressions Short-circuit boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.5 Operator precedences 5.4 Compound expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4.1 5.4.2 Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4.3 List comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5 Guard sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.6 Tail recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.7 Funs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.8 BIFs — Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Processes 6.1 Process creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Registered processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3 Process communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3.1 Send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3.2 Receive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3.3 Receive with timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.4 Process termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.5 Process links 6.5.1 Error handling between processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.5.3 Receiving exit signals 6.6 Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.7 Process priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.8 Process dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sending exit signals 7 Error handling 7.1 Exception classes and error reasons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Catch and throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3 Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Distributed Erlang 8.1 Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Node connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Hidden nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.5 Distribution BIFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6 Distribution command line flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.7 Distribution modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Ports and Port Drivers 9.1 Port Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2 Port BIFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 16 16 17 17 18 18 19 19 20 20 20 21 21 21 22 22 23 24 24 26 26 26 27 27 27 28 29 29 29 29 29 30 30 30 31 31 32 33 34 34 34 35 35 35 36 36 37 37 37 10 Code loading 11 Macros 11.1 Defining and using macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Predefined macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Flow Control in Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4 Stringifying Macro Arguments 12 Further Reading and Resources 4 39 40 40 41 41 41 43 1 Background, or Why Erlang is that it is Erlang is the result of a project at Ericsson’s Computer Science Laboratory to improve the programming of telecommunication applications. A critical requirement was supporting the characteristics of such applica- tions, that include: • Massive concurrency • Fault-tolerance • Isolation • Dynamic code upgrading at runtime • Transactions Throughout the whole of Erlang’s history the development process has been extremely pragmatic. The char- acteristics and properties of the types of systems in which Ericsson was interested drove Erlang’s development. These properties were considered to be so fundamental that it was decided to build support for them into the language itself, rather than in libraries. Because of the pragmatic development process, rather than a result of prior planning, Erlang “became” a functional language — since the features of functional languages fitted well with the properties of the systems being developed. 5 2 Structure of an Erlang program 2.1 Module syntax An Erlang program is made up of modules where each module is a text file with the extension .erl. For small programs, all modules typically reside in one directory. A module consists of module attributes and function definitions. -module(demo). -export([double/1]). double(X) -> times(X, 2). times(X, N) -> X * N. The above module demo consists of the function times/2 which is local to the module and the function double/1 which is exported and can be called from outside the module. (the arrow ⇒ should be read as “resulting in”) demo:double(10) ⇒ 20 double/1 means the function “double” with one argument. A function double/2 taking two arguments is regarded as a different function. The number of arguments is called the arity of the function. 2.2 Module attributes A module attribute defines a certain property of a module and consists of a tag and a value: -Tag(Value). Tag must be an atom, while Value must be a literal term (see chapter 3). Any module attribute can be specified. The attributes are stored in the compiled code and can be retrieved by calling the function Module:module_info(attributes). 2.2.1 Pre-defined module attributes Pre-defined module attributes must be placed before any function declaration. • -module(Module). 6 CHAPTER 2. STRUCTURE OF AN ERLANG PROGRAM 7 This attribute is mandatory and must be specified first. It defines the name of the module. The name Module, an atom (see section 3.1.1), should be the same as the filename without the ‘.erl’ extension. • -export([Func1/Arity1, ..., FuncN/ArityN]). This attribute specifies which functions in the module that can be called from outside the module. Each function name FuncX is an atom and ArityX an integer. • -import(Module,[Func1/Arity1, ..., FuncN/ArityN]). This attribute indicates a Module from which a list of functions are imported. For example: -import(demo, [double/1]). This means that it is possible to write double(10) instead of the longer demo:double(10) which can be impractical if the function is used frequently. • -compile(Options). Compiler options. • -vsn(Vsn). Module version. If this attribute is not specified, the version defaults to the checksum of the module. • -behaviour(Behaviour). This attribute either specifies a user defined behaviour or one of the OTP standard behaviours gen_server, gen_fsm, gen_event or supervisor. The spelling “behavior” is also accepted. 2.2.2 Macro and record definitions Records and macros are defined in the same way as module attributes: -record(Record,Fields). -define(Macro,Replacement). Records and macro definitions are also allowed between functions, as long as the definition comes before its first use. (About records see section 3.2.2 and about macros see chapter 11.) 2.2.3 File inclusion File inclusion is specified in the same way as module attributes: -include(File). -include_lib(File). File is a string that represents a file name. Include files are typically used for record and macro definitions that are shared by several modules. By convention, the extension .hrl is used for include files. -include("my_records.hrl"). -include("incdir/my_records.hrl"). -include("/home/user/proj/my_records.hrl"). If File starts with a path component $Var, then the value of the environment variable Var (returned by os:getenv(Var)) is substituted for $Var. CHAPTER 2. STRUCTURE OF AN ERLANG PROGRAM 8 -include("$PROJ_ROOT/my_records.hrl"). include_lib is similar to include, but the first path component is assumed to be the name of an application. -include_lib("kernel/include/file.hrl"). The code server uses code:lib_dir(kernel) to find the directory of the current (latest) version of kernel, and then the subdirectory include is searched for the file file.hrl. 2.3 Comments Comments may appear anywhere in a module except within strings and quoted atoms. A comment begins with the percentage character (%) and covers the rest of the line but not the end-of-line. The terminating end-of-line has the effect of a blank. 2.4 Character Set Erlang handles the full Latin-1 (ISO-8859-1) character set. Thus all Latin-1 printable characters can be used and displayed without the escape backslash. Atoms and variables can use all Latin-1 characters. Character classes Octal 40 - 57 Decimal 32 - 47 60 - 71 72 - 100 101 - 132 133 - 140 48 - 57 58 - 64 65 - 90 91 - 96 ! " # $ % & ’ / 0 - 9 : ; < = > @ A - Z [ \ ] ^ _ ‘ 141 - 172 173 - 176 97 - 122 123 - 126 a - z { | } ~ 200 - 237 240 - 277 128 - 159 160 - 191 300 - 326 327 192 - 214 215 330 - 336 337 - 366 367 216 - 222 223 - 246 247 - ¿ À - Ö × Ø - Þ ß - ö ÷ 370 - 377 248 - 255 ø - ÿ Class Punctuation characters Decimal digits Punctuation characters Uppercase letters Punctuation characters Lowercase letters Punctuation characters Control characters Punctuation characters Uppercase letters Punctuation character Uppercase letters Lowercase letters Punctuation character Lowercase letters CHAPTER 2. STRUCTURE OF AN ERLANG PROGRAM 9 2.5 Reserved words The following are reserved words in Erlang: after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse receive rem try when xor 3 Data types (terms) 3.1 Unary data types 3.1.1 Atoms An atom is a symbolic name, also known as a literal. Atoms begin with a lower-case letter, and may contain alphanumeric characters, underscores (_) or at-signs (@). Alternatively atoms can be specified by enclosing them in single quotes (’), necessary when they start with an uppercase character or contain characters other than underscores and at-signs. For example: hello phone_number ’Monday’ ’phone number’ ’Anything inside quotes \n\012’ (see section 3.3) 3.1.2 Booleans There is no boolean data type in Erlang. The atoms true and false are used instead. 2 =< 3 ⇒ true true or false ⇒ true 3.1.3 Integers In addition to the normal way of writing integers Erlang provides further notations. $Char is the Latin-1 numeric value of the character ‘Char’ (that may be an escape sequence) and Base#Value is an integer in base Base, which must be an integer in the range 2..36. 42 ⇒ 42 $A ⇒ 65 $\n ⇒ 10 2#101 ⇒ 5 16#1f ⇒ 31 (see section 3.3) 10 CHAPTER 3. DATA TYPES (TERMS) 11 3.1.4 Floats A float is a real number written Num[eExp] where Num is a decimal number between 0.01 and 10000 and Exp (optional) is a signed integer specifying the power-of-10 exponent. For example: 2.3e-3 ⇒ 2.30000e-3 3.1.5 References (corresponding to 2.3*10-3) A reference is a term which is unique in an Erlang runtime system, created by the built-in function make_ref/0. (For more information on built-in functions, or BIF s, see section 5.8.) 3.1.6 Ports A port identifier identifies a port (see chapter 9). 3.1.7 Pids A process identifier, pid, identifies a process (see chapter 6). 3.1.8 Funs A fun identifies a functional object (see section 5.7). 3.2 Compound data types 3.2.1 Tuples A tuple is a compound data type that holds a fixed number of terms enclosed within curly braces. {Term1,...,TermN} Each TermX in the tuple is called an element. The number of elements is called the size of the tuple. BIFs to manipulate tuples size(Tuple) element(N,Tuple) setelement(N,Tuple,Expr) Returns the size of Tuple Returns the Nth element in Tuple Returns a new tuple copied from Tuple except that the Nth element is replaced by Expr P = {adam, 24, {july, 29}} ⇒ P is bound to {adam, 24, {july, 29}} element(1, P) ⇒ adam element(3, P) ⇒ {july,29} P2 = setelement(2, P, 25) ⇒ P2 is bound to {adam, 25, {july, 29}} size(P) ⇒ 3 size({}) ⇒ 0 CHAPTER 3. DATA TYPES (TERMS) 12 3.2.2 Records A record is a named tuple with named elements called fields. A record type is defined as a module attribute, for example: -record(Rec, {Field1 [= Value1], ... FieldN [= ValueN]}). Rec and Fields are atoms and each FieldX can be given an optional default ValueX. This definition may be placed amongst the functions of a module, but only before it is used. If a record type is used by several modules it is advisable to put it in a separate file for inclusion. A new record of type Rec is created using an expression like this: # Rec{Field1=Expr1, ..., FieldK=ExprK [, _=ExprL]} The fields need not be in the same order as in the record definition. Fields omitted will get their respective default values. If the final clause is used, omitted fields will get the value ExprL. Fields without default values and that are omitted will have their value set to the atom undefined. The value of a field is retrieved using the expression “Variable#Rec.Field”. -module(employee). -export([new/2]). -record(person, {name, age, employed=erixon}). new(Name, Age) -> #person{name=Name, age=Age}. The function employee:new/2 can be used in another module which must also include the same record definition of person. {P = employee:new(ernie,44)} ⇒ {person, ernie, 44, erixon} P#person.age ⇒ 44 P#person.employed ⇒ erixon When working with records in the Erlang shell, the functions rd(RecordName, RecordDefinition) and rr(Module) can be used to define and load record definitions. Refer to the Erlang Reference Manual for more information. 3.2.3 Lists A list is a compound data type that holds a variable number of terms enclosed within square brackets. [Term1,...,TermN] Each term TermX in the list is called an element. The length of a list refers to the number of elements. Common in functional programming, the first element is called the head of the list and the remainder (from the 2nd element onwards) is called the tail of the list. Note that individual elements within a list do not have to have the same type, although it is common (and perhaps good) practice to do so — where mixed types are involved, records are more commonly used. BIFs to manipulate lists length(List) hd(List) tl(List) Returns the length of List Returns the 1st (head) element of List Returns List with the 1st element removed (tail) CHAPTER 3. DATA TYPES (TERMS) 13 The vertical bar operator (|) separates the leading elements of a list (one or more) from the remainder. For example: [H | T] = [1, 2, 3, 4, 5] ⇒ H=1 and T=[2, 3, 4, 5] [X, Y | Z] = [a, b, c, d, e] ⇒ X=a, Y=b and Z=[c, d, e] Implicitly a list will end with an empty list, i.e. [a, b] is the same as [a, b | []]. A list looking like [a, b | c] is badly formed and should be avoided (because the atom ’c’ is not a list). Lists lend themselves naturally to recursive functional programming. For example, the following function ‘sum’ computes the sum of a list, and ‘double’ multiplies each element in a list by 2, constructing and returning a new list as it goes. sum([]) -> 0; sum([H | T]) -> H + sum(T). double([]) -> []; double([H | T]) -> [H*2 | double(T)]. The above definitions introduce pattern matching, described in chapter 4. Patterns of this form are common in recursive programming, explicitly providing a “base case” (for the empty list in these examples). For working with lists, the operator ++ joins two lists together (appends the second argument to the first) and returns the resulting list. The operator -- produces a list that is a copy of its first argument, except that for each element in the second argument, the first occurrence of this element (if any) in the resulting list is removed. [1,2,3] ++ [4,5] ⇒ [1,2,3,4,5] [1,2,3,2,1,2] -- [2,1,2] ⇒ [3,1,2] A collection of list processing functions can be found in the STDLIB module lists. 3.2.4 Strings Strings are character strings enclosed within double quotes but are, in fact, stored as lists of integers. "abcdefghi" is the same as [97,98,99,100,101,102,103,104,105] "" is the same as [] Two adjacent strings will be concatenated into one at compile-time and do not incur any runtime overhead. "string" "42" ⇒ "string42" 3.2.5 Binaries A binary is a chunk of untyped memory by default a sequence of 8-bit bytes. <<Elem1,...,ElemN>> Each ElemX is specified as Value[:Size][/TypeSpecifierList]. Element specification Value Should evaluate to an integer, float or binary Size Should evaluate to an integer TypeSpecifierList A sequence of optional type specifiers, in any order, separated by hyphens (-) CHAPTER 3. DATA TYPES (TERMS) 14 Type specifiers Type Signedness Endianness big | little | native Unit integer | float | binary Default is integer Default is unsigned signed | unsigned CPU dependent. Default is big Allowed range is 1..256. Default is 1 for integer and float, and 8 for binary unit:IntegerLiteral The value of Size multiplied by the unit gives the number of bits for the segment. Each segment can consist of zero or more bits but the total number of bits must be a multiple of 8, or a badarg run-time error will occur. Also, a segment of type binary must have a size evenly divisible by 8. Binaries cannot be nested. <<1, 17, 42>> <<"abc">> <<1, 17, 42:16>> <<>> <<15:8/unit:10>> <<(-1)/unsigned>> % <<1, 17, 42>> % <<97, 98, 99>> (The same as <<$a, $b, $c>>) % <<1,17,0,42>> % <<>> % <<0,0,0,0,0,0,0,0,0,15>> % <<255>> 3.3 Escape sequences Escape sequences are allowed in strings and quoted atoms. Escape sequences \b \d \e \f \n \r \s \t \v \XYZ, \XY, \X \^A .. \^Z \^a .. \^z \’ \" \\ Backspace Delete Escape Form feed New line Carriage return Space Tab Vertical tab Character with octal representation XYZ, XY or X Control A to control Z Control A to control Z Single quote Double quote Backslash 3.4 Type conversions There are a number of built-in functions for type conversion: CHAPTER 3. DATA TYPES (TERMS) 15 Type conversions atom integer float atom integer float pid fun tuple list binary - X - - - X X - - - - - X X - X - - - X X pid - - - - - X X fun - - - - - X X tuple - - - - - X X list X X X X X X X binary X X X X X X X The BIF float/1 converts integers to floats. The BIFs round/1 and trunc/1 convert floats to integers. The BIFs Type_to_list/1 and list_to_Type/1 convert to and from lists. The BIFs term_to_binary/1 and binary_to_term/1 convert to and from binaries. % "hello" % hello % "7.00000000000000000000e+00" atom_to_list(hello) list_to_atom("hello") float_to_list(7.0) list_to_float("7.000e+00") % 7.00000 integer_to_list(77) list_to_integer("77") tuple_to_list({a, b ,c}) list_to_tuple([a, b, c]) pid_to_list(self()) term_to_binary(<<17>>) term_to_binary({a, b ,c}) binary_to_term(<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>) term_to_binary(math:pi()) % "77" % 77 % [a,b,c] % {a,b,c} % "<0.25.0>" % <<131,109,0,0,0,1,17>> % <<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>> % <<131,99,51,46,49,52,49,53,57,50,54,53,51,...>> % {a,b,c} 4 Pattern Matching 4.1 Variables Variables are introduced as arguments to a function or as a result of pattern matching. Variables begin with an uppercase letter or underscore (_) and may contain alphanumeric characters, underscores and at-signs (@). Variables can only be bound (assigned) once. Abc A_long_variable_name AnObjectOrientatedVariableName _Height An anonymous variable is denoted by a single underscore (_) and can be used when a variable is required but its value can be ignored. [H|_] = [1,2,3] % H=1 and the rest is ignored Variables beginning with underscore like _Height are normal variables, not anonymous. They are however ignored by the compiler in the sense that they will not generate any warnings for unused variables. Thus it is possible to write: member(_Elem, []) -> false. instead of: member(_, []) -> false. which can make for more readable code. The scope for a variable is its function clause. Variables bound in a branch of an if, case, or receive expression must be bound in all branches to have a value outside the expression, otherwise they will be regarded as unsafe (possibly undefined) outside the expression. 16 CHAPTER 4. PATTERN MATCHING 4.2 Pattern Matching 17 A pattern has the same structure as a term but may contain new unbound variables. Name1 [H|T] {error,Reason} Patterns occur in function heads, case, receive, and try expressions and in match operator (=) expressions. Patterns are evaluated through pattern matching against an expression and this is how variables are defined and bound. Pattern = Expr Both sides of the expression must have the same structure. If the matching succeeds, all unbound variables, if any, in the pattern become bound. If the matching fails, a badmatch run-time error will occur. > {A, B} = {answer, 42}. {answer,42} > A. answer > B. 42 4.2.1 Match operator (=) in patterns If Pattern1 and Pattern2 are valid patterns, then the following is also a valid pattern: Pattern1 = Pattern2 The = introduces an alias which when matched against an expression, both Pattern1 and Pattern2 are matched against it. The purpose of this is to avoid the reconstruction of terms. foo({connect,From,To,Number,Options}, To) -> Signal = {connect,From,To,Number,Options}, fox(Signal), ...; which can be written more efficiently as: foo({connect,From,To,Number,Options} = Signal, To) -> fox(Signal), ...; 4.2.2 String prefix in patterns When matching strings, the following is a valid pattern: f("prefix" ++ Str) -> ... which is equivalent to and easier to read than: f([$p,$r,$e,$f,$i,$x | Str]) -> ... CHAPTER 4. PATTERN MATCHING 18 You can only use strings as prefix expressions; patterns such as Str ++ "postfix" are not allowed. 4.2.3 Expressions in patterns An arithmetic expression can be used within a pattern, provided it only uses numeric or bitwise operators and its value can be evaluated to a constant at compile-time. case {Value, Result} of {?Threshold+1, ok} -> ... % ?Threshold is a macro 4.2.4 Matching binaries Bin = <<1, 2, 3>> <<A, B, C>> = Bin <<D:16, E>> = Bin <<F, G/binary>> = Bin % <<1,2,3>> All elements are 8-bit bytes % A=1, B=2 and C=3 % D=258 and E=3 % F=1 and G=<<2,3>> In the last line, the variable G of unspecified size matches the rest of the binary Bin. Always put a space between (=) and (<<) so as to avoid confusion with the (=<) operator. 5 Functions 5.1 Function definition A function is defined as a sequence of one or more function clauses. The function name is an atom. Func(Pattern11,...,Pattern1N) [when GuardSeq1] -> Body1; ...; ...; Func(PatternK1,...,PatternKN) [when GuardSeqK] -> BodyK. The function clauses are separated by semicolons (;) and terminated by full stop (.). A function clause consists of a clause head and a clause body separated by an arrow (->). A clause head consists of the function name (an atom), arguments within parentheses and an optional guard sequence beginning with the keyword when. Each argument is a pattern. A clause body consists of a sequence of expressions separated by commas (,). Expr1, ..., ExprM The number of arguments N is the arity of the function. A function is uniquely defined by the module name, function name and arity. Two different functions in the same module with different arities may have the same name. A function Func in Module with arity N is often denoted as Module:Func/N. -module(mathStuff). -export([area/1]). area({square, Side}) -> Side * Side; area({circle, Radius}) -> math:pi() * Radius * Radius; area({triangle, A, B, C}) -> S = (A + B + C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)). 19 CHAPTER 5. FUNCTIONS 5.2 Function calls A function is called using: [Module:]Func(Expr1, ..., ExprN) 20 Module evaluates to a module name and Func to a function name or a fun. When calling a function in another module, the module name must be provided and the function must be exported. This is referred to as a fully qualified function name. lists:keysearch(Name, 1, List) The module name can be omitted if Func evaluates to the name of a local function, an imported function, or an auto-imported BIF. In such cases, the function is called using an implicitly qualified function name. Before calling a function the arguments ExprX are evaluated. If the function cannot be found, an undef run-time error will occur. Next the function clauses are scanned sequentially until a clause is found such that the patterns in the clause head can be successfully matched against the given arguments and that the guard sequence, if any, is true. If no such clause can be found, a function_clause run-time error will occur. If a matching clause is found, the corresponding clause body is evaluated, i.e. the expressions in the body are evaluated sequentially and the value of the last expression is returned. The fully qualified function name must be used when calling a function with the same name as a BIF (built-in function, see section 5.8). The compiler does not allow defining a function with the same name as an imported function. When calling a local function, there is a difference between using the implicitly or fully qualified function name, as the latter always refers to the latest version of the module (see chapter 10). 5.3 Expressions An expression is either a term or the invocation of an operator, for example: Term op Expr Expr1 op Expr2 (Expr) begin Expr1, ..., ExprM end % no comma (,) before end The simplest form of expression is a term, i.e. an integer, float, atom, string, list or tuple and the return value is the term itself. There are both unary and binary operators. An expression may contain macro or record expressions which will expanded at compile time. Parenthesised expressions are useful to override operator precedence (see section 5.3.5): 1 + 2 * 3 (1 + 2) * 3 % 7 % 9 Block expressions within begin...end can be used to group a sequence of expressions and the return value is the value of the last expression ExprM. CHAPTER 5. FUNCTIONS 21 All subexpressions are evaluated before the expression itself is evaluated, but the order in which subexpres- sions are evaluated is undefined. Most operators can only be applied to arguments of a certain type. For example, arithmetic operators can only be applied to integers or floats. An argument of the wrong type will cause a badarg run-time error. 5.3.1 Term comparisons Expr1 op Expr2 A term comparison returns a boolean value, in the form of atoms true or false. Comparison operators == /= =:= =/= Equal to Not equal to Exactly equal to Exactly not equal to =< < >= > Less than or equal to Less than Greater than or equal to Greater than 1==1.0 1=:=1.0 1 > a % true % false % false The arguments may be of different data types. The following order is defined: number < atom < reference < fun < port < pid < tuple < list < binary Lists are compared element by element. Tuples are ordered by size, two tuples with the same size are compared element by element. When comparing an integer and a float, the integer is first converted to a float. In the case of =:= or =/= there is no type conversion. 5.3.2 Arithmetic expressions op Expr Expr1 op Expr2 An arithmetic expression returns the result after applying the operator. Arithmetic operators + - + - * / bnot div rem band bor bxor bsl bsr Unary + Unary - Addition Subtraction Multiplication Floating point division Unary bitwise not Integer division Integer remainder of X/Y Bitwise and Bitwise or Arithmetic bitwise xor Arithmetic bitshift left Bitshift right Integer | Float Integer | Float Integer | Float Integer | Float Integer | Float Integer | Float Integer Integer Integer Integer Integer Integer Integer Integer CHAPTER 5. FUNCTIONS 22 +1 4/2 5 div 2 5 rem 2 2#10 band 2#01 2#10 bor 2#01 % 1 % 2.00000 % 2 % 1 % 0 % 3 5.3.3 Boolean expressions op Expr Expr1 op Expr2 A boolean expression returns the value true or false after applying the operator. Boolean operators not and or xor Unary logical not Logical and Logical or Logical exclusive or not true true and false true xor false % false % false % true 5.3.4 Short-circuit boolean expressions Expr1 orelse Expr2 Expr1 andalso Expr2 These are boolean expressions where Expr2 is evaluated only if necessary. In an orelse expression Expr2 will be evaluated only if Expr1 evaluates to false. In an andalso expression Expr2 will be evaluated only if Expr1 evaluates to true. if A >= 0 andalso math:sqrt(A) > B -> ... if is_list(L) andalso length(L) == 1 -> ... 5.3.5 Operator precedences In an expression consisting of subexpressions the operators will be applied according to a defined operator precedence order: CHAPTER 5. FUNCTIONS 23 Operator precedence (from high to low) : # Unary + - bnot not / * div rem band and + - bor bxor bsl bsr or xor ++ -- == /= =< < >= > =:= =/= andalso orelse = ! catch Left associative Left associative Right associative Right associative The operator with the highest priority is evaluated first. Operators with the same priority are evaluated according to their associativity. The left associative arithmetic operators are evaluated left to right: 6 + 5 * 4 - 3 / 2 ⇒ 6 + 20 - 1.5 ⇒ 26 - 1.5 ⇒ 24.5 5.4 Compound expressions 5.4.1 If if end GuardSeq1 -> Body1; ...; GuardSeqN -> BodyN % Note no semicolon (;) before end The branches of an if expression are scanned sequentially until a guard sequence GuardSeq which evaluates to true is found. The corresponding Body (sequence of expressions separated by commas) is then evaluated. The return value of Body is the return value of the if expression. If no guard sequence is true, an if_clause run-time error will occur. If necessary, the guard expression true can be used in the last branch, as that guard sequence is always true (known as a “catch all”). is_greater_than(X, Y) -> if end X>Y -> true; true -> false % works as an ’else’ branch It should be noted that pattern matching in function clauses can be used to replace if cases (most of the time). Overuse of if sentences withing function bodies is considered a bad Erlang practice. 5.4.2 Case Case expressions provide for inline pattern matching, similar to the way in which function clauses are matched. CHAPTER 5. FUNCTIONS 24 case Expr of Pattern1 [when GuardSeq1] -> Body1; ...; PatternN [when GuardSeqN] -> BodyN % Note no semicolon (;) before end end The expression Expr is evaluated and the patterns Pattern1...PatternN are sequentially matched against the result. If a match succeeds and the optional guard sequence GuardSeqX is true, then the corresponding BodyX is evaluated. The return value of BodyX is the return value of the case expression. If there is no matching pattern with a true guard sequence, a case_clause run-time error will occur. is_valid_signal(Signal) -> case Signal of {signal, _What, _From, _To} -> true; {signal, _What, _To} -> true; _Else -> false end. % ’catch all’ 5.4.3 List comprehensions List comprehensions are analogous to the setof and findall predicates in Prolog. [Expr || Qualifier1,...,QualifierN] Expr is an arbitrary expression, and each QualifierX is either a generator or a filter. A generator is written as: Pattern <- ListExpr where ListExpr must be an expression which evaluates to a list of terms. A filter is an expression which evaluates to true or false. Variables in list generator expressions shadow variables in the function clause surrounding the list comprehension. The qualifiers are evaluated from left to right, the generators creating values and the filters constraining them. The list comprehension then returns a list where the elements are the result of evaluating Expr for each combination of the resulting values. > [{X, Y} || X <- [1,2,3,4,5,6], X > 4, Y <- [a,b,c]]. [{5,a},{5,b},{5,c},{6,a},{6,b},{6,c}] 5.5 Guard sequences A guard sequence is a set of guards separated by semicolons (;). The guard sequence is true if at least one of the guards is true. Guard1; ...; GuardK CHAPTER 5. FUNCTIONS 25 A guard is a set of guard expressions separated by commas (,). The guard is true if all guard expressions evaluate to true. GuardExpr1, ..., GuardExprN The permitted guard expressions (sometimes called guard tests) are a subset of valid Erlang expressions, since the evaluation of a guard expression must be guaranteed to be free of side-effects. Valid guard expressions: The atom true; Other constants (terms and bound variables), are all regarded as false; Term comparisons; Arithmetic and boolean expressions; Calls to the BIFs specified below. Type test BIFs is_atom/1 is_constant/1 is_integer/1 is_float/1 is_number/1 is_reference/1 is_port/1 is_pid/1 is_function/1 is_tuple/1 is_record/2 The 2nd argument is the record name is_list/1 is_binary/1 Other BIFs allowed in guards: abs(Integer | Float) float(Term) trunc(Integer | Float) round(Integer | Float) size(Tuple | Binary) element(N, Tuple) hd(List) tl(List) length(List) self() node() node(Pid | Ref |Port) A small example: fact(N) when N>0 -> N * fact(N-1); fact(0) -> 1. % first clause head % first clause body % second clause head % second clause body 5.6 Tail recursion If the last expression of a function body is a function call, a tail recursive call is performed in such a way that no system resources (like the call stack) are consumed. This means that an infinite loop like a server can be programmed provided it only uses tail recursive calls. The function fact/1 above could be rewritten using tail recursion in the following manner: fact(N) when N>1 -> fact(N, N-1); fact(N) when N==1; N==0 -> 1. fact(F,0) -> F; fact(F,N) -> fact(F*N, N-1). % The variable F is used as an accumulator CHAPTER 5. FUNCTIONS 5.7 Funs 26 A fun defines a functional object. Funs make it possible to pass an entire function, not just the function name, as an argument. A ‘fun’ expression begins with the keyword fun and ends with the keyword end instead of a full stop (.). Between these should be a regular function declaration, except that no function name is specified. fun end (Pattern11,...,Pattern1N) [when GuardSeq1] -> Body1; ...; (PatternK1,...,PatternKN) [when GuardSeqK] -> BodyK Variables in a fun head shadow variables in the function clause surrounding the fun but variables bound in a fun body are local to the body. The return value of the expression is the resulting function. The expression fun Name/N is equivalent to: fun (Arg1,...,ArgN) -> Name(Arg1,...,ArgN) end The expression fun Module:Func/Arity is also allowed, provided that Func is exported from Module. Fun1 = fun (X) -> X+1 end. Fun1(2) % 3 Fun2 = fun (X) when X>=1000 -> big; (X) -> small end. Fun2(2000) % big Anonymous funs: When a fun is anonymous, i.e. there is no function name in the definition of the fun, the definition of a recursive fun has to be done in two steps. This example shows how to define an anonymous fun sum(List) (see section 3.2.3) as an anonymous fun. Sum1 = fun ([], _Foo) -> 0;([H|T], Foo) -> H + Foo(T, Foo) end. Sum = fun (List) -> Sum1(List, Sum1) end. Sum([1,2,3,4,5]) % 15 The definition of Sum is done in a way such that it takes itself as a parameter, matched to _Foo (empty list) or Foo, which it then calls recursively. The definition of Sum calls Sum1, also passing Sum1 as a parameter. Names in funs: In Erlang you can use a name inside a fun before the name has been defined. The syntax of funs with names allows a variable name to be consistently present before each argument list. This allows funs to be recursive in one steps. This example shows how to define the function sum(List) (see section 3.2.3) as a funs with names. Sum = fun Sum([])-> 0;Sum([H|T]) -> H + Sum(T) end. Sum([1,2,3,4,5]) % 15 5.8 BIFs — Built-in functions The built-in functions, BIFs, are implemented in the C code of the runtime system and do things that are difficult or impossible to implement in Erlang. Most of the built-in functions belong to the module erlang but there are also built-in functions that belong to other modules like lists and ets. The most commonly CHAPTER 5. FUNCTIONS 27 used BIFs belonging to the module erlang are auto-imported, i.e. they do not need to be prefixed with the module name. Some useful BIFs date() now() time() halt() processes() process_info(Pid) Returns today’s date as {Year, Month, Day} Returns current time in microseconds. System dependent Returns current time as {Hour, Minute, Second} System dependent Stops the Erlang system Returns a list of all processes currently known to the system Returns a dictionary containing information about Pid Module:module_info() Returns a dictionary containing information about the code in Module A dictionary is a list of {Key, Value} terms (see also section 6.8). size({a, b, c}) atom_to_list(’Erlang’) date() time() % 3 % "Erlang" % {2013,5,27} % {01,27,42} 6 Processes A process corresponds to one thread of control. Erlang permits very large numbers of concurrent processes, each executing like it had an own virtual processor. When a process executing functionA calls another functionB, it will wait until functionB is finished and then retrieve its result. If instead it spawns another process executing functionB, both will continue in parallel (concurrently). functionA will not wait for functionB and the only way they can communicate is through message passing. Erlang processes are light-weight with a small memory footprint, fast to create and shut-down, and the scheduling overhead is low. A process identifier, Pid, identifies a process. The BIF self/0 returns the Pid of the calling process. 6.1 Process creation A process is created using the BIF spawn/3. spawn(Module, Func, [Expr1, ..., ExprN]) Module should evaluate to a module name and Func to a function name in that module. The list Expr1...ExprN are the arguments to the function. spawn creates a new process and returns the process identifier, Pid. The new process starts by executing: Module:Func(Expr1, ..., ExprN) The function Func has to be exported even if it is spawned by another function in the same module. There are other spawn BIFs, for example spawn/4 for spawning a process on another node. 6.2 Registered processes A process can be associated with a name. The name must be an atom and is automatically unregistered if the process terminates. Only static (cyclic) processes should be registered. 28 CHAPTER 6. PROCESSES 29 Name registration BIFs register(Name, Pid) registered() whereis(Name) Associates the atom Name with the process Pid Returns a list of names which have been registered Returns the Pid registered under Name or undefined if the name is not registered 6.3 Process communication Processes communicate by sending and receiving messages. Messages are sent using the send operator (!) and are received using receive. Message passing is asynchronous and reliable, i.e. the message is guaranteed to eventually reach the recipient, provided that the recipient exists. 6.3.1 Send Pid ! Expr The send (!) operator sends the value of Expr as a message to the process specified by Pid where it will be placed last in its message queue. The value of Expr is also the return value of the (!) expression. Pid must evaluate to a process identifier, a registered name or a tuple {Name,Node}, where Name is a registered process at Node (see chapter 8). The message sending operator (!) never fails, even if it addresses a non-existent process. 6.3.2 Receive receive Pattern1 [when GuardSeq1] -> Body1; ... PatternN [when GuardSeqN] -> BodyN % Note no semicolon (;) before end end This expression receives messages sent to the process using the send operator (!). The patterns PatternX are sequentially matched against the first message in time order in the message queue, then the second and so on. If a match succeeds and the optional guard sequence GuardSeqX is true, then the message is removed from the message queue and the corresponding BodyX is evaluated. It is the order of the pattern clauses that decides the order in which messages will be received prior to the order in which they arrive. This is called selective receive. The return value of BodyX is the return value of the receive expression. receive never fails. The process may be suspended, possibly indefinitely, until a message arrives that matches one of the patterns and with a true guard sequence. CHAPTER 6. PROCESSES 30 wait_for_onhook() -> receive onhook -> disconnect(), idle(); {connect, B} -> B ! {busy, self()}, wait_for_onhook() end. 6.3.3 Receive with timeout receive Pattern1 [when GuardSeq1] -> Body1; ...; PatternN [when GuardSeqN] -> BodyN after ExprT -> BodyT end ExprT should evaluate to an integer between 0 and 16#ffffffff (the value must fit in 32 bits). If no matching message has arrived within ExprT milliseconds, then BodyT will be evaluated and its return value becomes the return value of the receive expression. wait_for_onhook() -> receive onhook -> disconnect(), idle(); {connect, B} -> B ! {busy, self()}, wait_for_onhook() after 60000 -> disconnect(), error() end. A receive...after expression with no branches can be used to implement simple timeouts. receive after ExprT -> BodyT end CHAPTER 6. PROCESSES 31 Two special cases for the timeout value ExprT infinity This is equivalent to not using a timeout and can be useful for timeout values that are calculated at run-time If there is no matching message in the mailbox, the timeout will occur immediately 0 6.4 Process termination A process always terminates with an exit reason which may be any term. If a process terminates normally, i.e. it has run to the end of its code, then the reason is the atom normal. A process can terminate itself by calling one of the following BIFs. exit(Reason) erlang:error(Reason) erlang:error(Reason, Args) A process terminates with the exit reason {Reason,Stack} when a run-time error occurs. A process may also be terminated if it receives an exit signal with a reason other than normal (see section 6.5.3). 6.5 Process links Two processes can be linked to each other. Links are bidirectional and there can only be one link be- tween two distinct processes (unique Pids). A process with Pid1 can link to a process with Pid2 using the BIF link(Pid2). The BIF spawn_link(Module, Func, Args) spawns and links a process in one atomic operation. A link can be removed using the BIF unlink(Pid). 6.5.1 Error handling between processes When a process terminates it will send exit signals to all processes that it is linked to. These in turn will also be terminated or handle the exit signal in some way. This feature can be used to build hierarchical program structures where some processes are supervising other processes, for example restarting them if they terminate abnormally. 6.5.2 Sending exit signals A process always terminates with an exit reason which is sent as an exit signal to all linked processes. The BIF exit(Pid, Reason) sends an exit signal with the reason Reason to Pid, without affecting the calling process. 6.5.3 Receiving exit signals If a process receives an exit signal with an exit reason other than normal it will also be terminated, and will send exit signals with the same exit reason to its linked processes. An exit signal with reason normal is ignored. This behaviour can be changed using the BIF process_flag(trap_exit, true). CHAPTER 6. PROCESSES 32 The process is then able to trap exits. This means that an exit signal will be transformed into a message {’EXIT’, FromPid, Reason} which is put into the process’s mailbox and can be handled by the process like a regular message using receive. However, a call to the BIF exit(Pid, kill) unconditionally terminates the process Pid regardless whether it is able to trap exit signals or not. 6.6 Monitors A process Pid1 can create a monitor for Pid2 using the BIF: erlang:monitor(process, Pid2) which returns a reference Ref. If Pid2 terminates with exit reason Reason, a message as follows will be sent to Pid1: {’DOWN’, Ref, process, Pid2, Reason} If Pid2 does not exist, the ’DOWN’ message is sent immediately with Reason set to noproc. Monitors are unidirectional in that if Pid1 monitors Pid2 then it will receive a message when Pid2 dies but Pid2 will not receive a message when Pid1 dies. Repeated calls to erlang:monitor(process, Pid) will create several, independent monitors and each one will be sent a ’DOWN’ message when Pid terminates. A monitor can be removed by calling erlang:demonitor(Ref). It is possible to create monitors for processes with registered names, also at other nodes. 6.7 Process priorities The BIF process_flag(priority, Prio) defines the priority of the current process. Prio may have the value normal, which is the default, low, high or max. Modifying a process’s priority is discouraged and should only be done in special circumstances. A problem that requires changing process priorities can generally be solved by another approach. 6.8 Process dictionary Each process has its own process dictionary which is a list of {Key, Value} terms. Process dictionary BIFs put(Key, Value) get(Key) get() Saves the Value under the Key or replaces an older value Retrieves the value stored under Key or undefined Returns the entire process dictionary as a list of {Key, Value} terms get_keys(Value) Returns a list of keys that have the value Value erase(Key) erase() Deletes {Key, Value}, if any, and returns Key Returns the entire process dictionary and deletes it Process dictionaries could be used to keep global variables within an application, but the extensive use of them for this is usually regarded as poor programming style. 7 Error handling This chapter deals with error handling within a process. Such errors are known as exceptions. 7.1 Exception classes and error reasons Exception classes error exit throw Run-time error for example when applying an operator to the wrong types of arguments. Run-time errors can be raised by calling the BIFs erlang:error(Reason) or erlang:error(Reason, Args) The process calls exit(Reason), see section 6.4 The process calls throw(Expr), see section 7.2 An exception will cause the process to crash, i.e. its execution is stopped and it is removed from the system. It is also said to terminate. Then exit signals will be sent to any linked processes. An exception consists of its class, an exit reason and a stack. The stack trace can be retrieved using the BIF erlang:get_stacktrace/0. Run-time errors and other exceptions can be prevented from causing the process to terminate by using the expressions try and catch. For exceptions of class error, for example normal run-time errors, the exit reason is a tuple {Reason, Stack} where Reason is a term indicating which type of error. 33 CHAPTER 7. ERROR HANDLING 34 Exit reasons badarg badarith {badmatch, Value} function_clause {case_clause, Value} if_clause Argument is of wrong type. Argument is of wrong type in an arithmetic expression. Evaluation of a match expression failed. Value did not match. No matching function clause is found when evaluating a function call. No matching branch is found when evaluating a case expression. Value did not match. No true branch is found when evaluating an if expression. {try_clause, Value} No matching branch is found when evaluating the undef {badfun, Fun} {badarity, Fun} timeout_value noproc {nocatch, Value} system_limit of section of a try expression. Value did not match. The function cannot be found when evaluating a function call There is something wrong with Fun A fun is applied to the wrong number of arguments. Fun describes it and the arguments The timeout value in a receive...after expression is evaluated to something else than an integer or infinity Trying to link to a non-existant process Trying to evaluate a throw outside of a catch. Value is the thrown term A system limit has been reached Stack is the stack of function calls being evaluated when the error occurred, given as a list of tuples {Module, Name, Arity} with the most recent function call first. The most recent function call tuple may in some cases be {Module, Name, Args}. 7.2 Catch and throw catch Expr This returns the value of Expr unless an exception occurs during its evaluation. Then the return value will be a tuple containing information about the exception. {’EXIT’, {Reason, Stack}} Then the exception is caught. Otherwise it would terminate the process. a function call exit(Term) the tuple {’EXIT’,Term} is returned. throw(Term) then Term will be returned. If the exception is caused by If the exception is caused by calling catch 1+2 ⇒ 3 catch 1+a ⇒ {’EXIT’,{badarith,[...]}} catch has low precedence and catch subexpressions often need to be enclosed in a block expression or in parentheses. A = (catch 1+2) ⇒ 3 The BIF throw(Expr) is used for non-local return from a function. It must be evaluated within a catch, which returns the result from evaluating Expr. CHAPTER 7. ERROR HANDLING 35 catch begin 1,2,3,throw(four),5,6 end ⇒ four If throw/1 is not evaluated within a catch, a nocatch run-time error will occur. A catch will not prevent a process from terminating due to an exit signal from another linked process (unless it has been set to trap exits). 7.3 Try The try expression is able to distinguish between different exception classes. The following example emulates the behaviour of catch Expr: try Expr catch throw:Term -> Term; exit:Reason -> {’EXIT’, Reason}; error:Reason -> {’EXIT’,{Reason, erlang:get_stacktrace()}} end The full description of try is as follows: try Expr [of Pattern1 [when GuardSeq1] -> Body1; ...; PatternN [when GuardSeqN] -> BodyN] [catch [Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] -> ExceptionBody1; ...; [ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] -> ExceptionBodyN] [after AfterBody] end There has to be at least one catch or an after clause. There may be an of clause following the Expr which adds a case expression on the value of Expr. try returns the value of Expr unless an exception occurs during its evaluation. Then the exception is caught and the patterns ExceptionPattern with the right exception Class are sequentially matched against the caught exception. An omitted Class is shorthand for throw. If a match succeeds and the optional guard sequence ExceptionGuardSeq is true, the corresponding ExceptionBody is evaluated and becomes the return value. If there is no matching ExceptionPattern of the right Class with a true guard sequence, the exception is passed on as if Expr had not been enclosed in a try expression. An exception occurring during the evaluation of an ExceptionBody it is not caught. If none of the of Patterns match, a try_clause run-time error will occur. If defined then AfterBody is always evaluated last irrespective of whether and error occurred or not. Its return value is ignored and the return value of the try is the same as without an after section. AfterBody is evaluated even if an exception occurs in Body or ExceptionBody, in which case the exception is passed on. An exception that occurs during the evaluation of AfterBody itself is not caught, so if the AfterBody is evaluated due to an exception in Expr, Body or ExceptionBody, that exception is lost and masked by the new exception. 8 Distributed Erlang A distributed Erlang system consists of a number of Erlang runtime systems communicating with each other. Each such runtime system is called a node. Nodes can reside on the same host or on different hosts connected through a network. The standard distribution mechanism is implemented using TCP/IP sockets but other mechanisms can also be implemented. Message passing between processes on different nodes, as well as links and monitors, is transparent when using Pids. However, registered names are local to each node. A registered process at a particular node is referred to as {Name,Node}. The Erlang Port Mapper Daemon epmd is automatically started on every host where an Erlang node is started. It is responsible for mapping the symbolic node names to machine addresses. 8.1 Nodes A node is an executing Erlang runtime system which has been given a name, using the command line flag -name (long name) or -sname (short name). The format of the node name is an atom Name@Host where Name is the name given by the user and Host is the full host name if long names are used, or the first part of the host name if short names are used. node() returns the name of the node. Nodes using long names cannot communicate with nodes using short names. 8.2 Node connections The nodes in a distributed Erlang system are fully connected. The first time the name of another node is used, a connection attempt to that node will be made. If a node A connects to node B, and node B has a connection to node C, then node A will also try to connect to node C. This feature can be turned off using the command line flag: -connect_all false If a node goes down, all connections to that node are removed. The BIF: erlang:disconnect(Node) disconnects Node. The BIF nodes() returns the list of currently connected (visible) nodes. 36 CHAPTER 8. DISTRIBUTED ERLANG 8.3 Hidden nodes 37 It is sometimes useful to connect to a node without also connecting to all other nodes. For this purpose, a hidden node may be used. A hidden node is a node started with the command line flag -hidden. Connections between hidden nodes and other nodes must be set up explicitly. Hidden nodes do not show up in the list of nodes returned by nodes(). Instead, nodes(hidden) or nodes(connected) must be used. A hidden node will not be included in the set of nodes that the module global keeps track of. A C node is a C program written to act as a hidden node in a distributed Erlang system. The library erl_interface contains functions for this purpose. 8.4 Cookies Each node has its own magic cookie, which is an atom. The Erlang network authentication server (auth) reads the cookie in the file $HOME/.erlang.cookie. If the file does not exist, it will be created with a random string as content. The permissions of the file must be set to octal 400 (read-only by user). The cookie of the local node may also be set using the BIF erlang:set_cookie(node(), Cookie). The current node is only allowed to communicate with another node Node2 if it knows its cookie. If this is different from the current node (whose cookie will be used by default) it must be explicitly set with the BIF erlang:set_cookie(Node2, Cookie2). 8.5 Distribution BIFs Distribution BIFs node() is_alive() erlang:get_cookie() set_cookie(Node, Cookie) nodes() nodes(connected|hidden) monitor_node(Node, true|false) node(Pid|Ref|Port) erlang:disconnect_node(Node) spawn[_link|_opt](Node, Module, Function, Args) spawn[_link|_opt](Node, Fun) Returns the name of the current node. Allowed in guards Returns true if the runtime system is a node and can connect to other nodes, false otherwise Returns the magic cookie of the current node Sets the magic cookie used when connecting to Node. If Node is the current node, Cookie will be used when connecting to all new nodes Returns a list of all visible nodes to which the current node is connected to Returns a list not only of visible nodes, but also hidden nodes and previously known nodes, etc. Monitors the status of Node. A message {nodedown, Node} is received if the connection to it is lost Returns the node where the argument is located Forces the disconnection of Node Creates a process at a remote node Creates a process at a remote node CHAPTER 8. DISTRIBUTED ERLANG 38 8.6 Distribution command line flags Distribution command line flags -connect_all false -hidden -name Name -setcookie Cookie -sname Name Only explicit connection set-ups will be used Makes a node into a hidden node Makes a runtime system into a node, using long node names Same as calling erlang:set_cookie(node(), Cookie)) Makes a runtime system into a node, using short node names 8.7 Distribution modules There are several modules available which are useful for distributed programming: Kernel modules useful for distribution global global_group net_adm net_kernel STDLIB modules useful for distribution slave A global name registration facility Grouping nodes to global name registration groups Various net administration routines Erlang networking kernel Start and control of slave nodes 9 Ports and Port Drivers Ports provide a byte-oriented interface to external programs and communicate with Erlang processes by sending and receiving lists of bytes as messages. The Erlang process that creates a port is called the port owner or the connected process of the port. All communication to and from the port should go via the port owner. If the port owner terminates, so will the port (and the external program, if it has been programmed correctly). The external program forms another OS process. By default, it should read from standard input (file descriptor 0) and write to standard output (file descriptor 1). The external program should terminate when the port is closed. 9.1 Port Drivers Drivers are normally programmed in C and are dynamically linked to the Erlang runtime system. The linked- in driver behaves like a port and is called a port driver. However, an erroneous port driver might cause the entire Erlang runtime system to leak memory, hang or crash. 9.2 Port BIFs Port creation BIF open_port(PortName, PortSettings) Returns a port identifier Port as the result of opening a new Erlang port. Messages can be sent to and received from a port identifier, just like a Pid. Port identifiers can also be linked to or registered under a name using link/1 and register/2. PortName is usually a tuple {spawn,Command} where the string Command is the name of the external program. The external program runs outside the Erlang workspace unless a port driver with the name Command is found. If the driver is found, it will be started. PortSettings is a list of settings (options) for the port. The list typically contains at least a tuple {packet,N} which specifies that data sent between the port and the external program are preceded by an N-byte length indicator. Valid values for N are 1, 2 or 4. If binaries should be used instead of lists of bytes, the option binary must be included. 39 CHAPTER 9. PORTS AND PORT DRIVERS 40 The port owner Pid communicates with Port by sending and receiving messages. (Any process could send the messages to the port, but messages from the port will always be sent to the port owner). Messages sent to a port {Pid, {command, Data}} {Pid, close} {Pid,{connect,NewPid}} Sends Data to the port. Closes the port. Unless the port is already closed, the port replies with {Port, closed} when all buffers have been flushed and the port really closes. Sets the port owner of Port to NewPid. Unless the port is already closed, the port replies with {Port, connected} to the old port owner. Note that the old port owner is still linked to the port, but the new port owner is not. Data must be an I/O list. An I/O list is a binary or a (possibly deep) list of binaries or integers in the range 0..255. Messages received from a port {Port, {data, Data}} {Port, closed} {Port, connected} {’EXIT’, Port, Reason} Data is received from the external program Reply to Port ! {Pid,close} Reply to Port ! {Pid,{connect, NewPid}} If Port has terminated for some reason. Instead of sending and receiving messages, there are also a number of BIFs that can be used. These can be called by any process, not only the port owner. Port BIFs port_command(Port, Data) port_close(Port) port_connect(Port, NewPid) erlang:port_info(Port, Item) erlang:ports() Sends Data to Port Closes Port Sets the port owner of Port to NewPid. The old port owner Pid stays linked to the port and has to call unlink(Port) if this is not desired. Returns information as specified by Item Returns a list of all ports on the current node There are some additional BIFs that only apply to port drivers: port_control/3 and erlang:port_call/3. 10 Code loading Erlang supports code updating in a running system. Code replacement is performed at module level. The code of a module can exist in two versions in a system: current and old. When a module is loaded into the system for the first time, the code becomes current. If a new instance of the module is loaded, the code of the previous instance becomes old and the new instance becomes current. Normally a module is automatically loaded the first time a function in it is called. If the module is already loaded then it must explicitly be loaded again to a new version. Both old and current code are valid, and may be used concurrently. Fully qualified function calls will always refer to the current code. However, the old code may still be run by other processes. If a third instance of the module is loaded, the code server will remove (purge) the old code and any processes lingering in it are terminated. Then the third instance becomes current and the previously current code becomes old. To change from old code to current code, a process must make a fully qualified function call. -module(mod). -export([loop/0]). loop() -> receive code_switch -> mod:loop(); Msg -> ... loop() end. To make the process change code, send the message code_switch to it. The process then will make a fully qualified call to mod:loop() and change to the current code. Note that mod:loop/0 must be exported. 41 11 Macros 11.1 Defining and using macros -define(Const, Replacement). -define(Func(Var1, ..., VarN), Replacement). A macro must be defined before it is used but a macro definition may be placed anywhere among the attributes and function declarations of a module. If a macro is used in several modules it is advisable to put the macro definition in an include file. A macro is used as follows: ?Const ?Func(Arg1,...,ArgN) Macros are expanded during compilation. A macro reference ?Const is replaced by Replacement like this: -define(TIMEOUT, 200). ... call(Request) -> server:call(refserver, Request, ?TIMEOUT). is expanded to: call(Request) -> server:call(refserver, Request, 200). A macro reference ?Func(Arg1, ..., ArgN) will be replaced by Replacement, where all occurrences of a variable VarX from the macro definition are replaced by the corresponding argument ArgX. -define(MACRO1(X, Y), {a, X, b, Y}). ... bar(X) -> ?MACRO1(a, b), ?MACRO1(X, 123). will be expanded to: bar(X) -> {a, a, b, b}, 42 CHAPTER 11. MACROS {a, X, b, 123}. 43 To view the result of macro expansion, a module can be compiled with the ‘P’ option: compile:file(File, [’P’]). This produces a listing of the parsed code after preprocessing and parse transforms, in the file File.P. 11.2 Predefined macros Predefined macros ?MODULE ?MODULE_STRING ?FILE ?LINE ?MACHINE The name of the current module The name of the current module, as a string The file name of the current module The current line number The machine name, ’BEAM’ 11.3 Flow Control in Macros -undef(Macro). % This inhibits the macro definition. -ifdef(Macro). %% Lines that are evaluated if Macro was defined -else. %% If the condition was false, these lines are evaluated instead. -endif. ifndef(Macro) can be used instead of ifdef and means the opposite. -ifdef(debug). -define(LOG(X), io:format("{~p,~p}:~p~n",[?MODULE,?LINE,X])). -else. -define(LOG(X), true). -endif. If debug is defined when the module is compiled, ?LOG(Arg) will expand to a call to io:format/2 and provide the user with some simple trace output. 11.4 Stringifying Macro Arguments ??Arg, where Arg is a macro argument expands to the argument in the form of a string. -define(TESTCALL(Call), io:format("Call ~s: ~w~n", [ ? ?Call, Call])). ?TESTCALL(myfunction(1,2)), ?TESTCALL(you:function(2,1)), results in: CHAPTER 11. MACROS 44 io:format("Call ~s: ~w~n", ["myfunction(1,2)", m:myfunction(1,2)]), io:format("Call ~s: ~w~n", ["you:function(2,1)", you:function(2,1)]), That is, a trace output with both the function called and the resulting value. 12 Further Reading and Resources Following websites provide in-depth explanation of topics and concepts briefly covered in this document: • Official Erlang documentation: http://www.erlang.org/doc/ • Learn You Some Erlang for Great Good: http://learnyousomeerlang.com/ • Tutorials section at Erlang Central: https://erlangcentral.org/wiki/index.php?title=Category: HowTo Still have questions? erlang-questions mailing list (http://erlang.org/mailman/listinfo/erlang-questions) is a good place for general discussions about Erlang/OTP, the language, implementation, usage and beginners questions. 45 |