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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>perlop - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-gb" />
<link href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
</head>
<script language="JavaScript" type="text/javascript" src="label.js"></script>
<script language="JavaScript">
pageDepth = 0;
setPath();
</script>
<body onLoad="showToolbars();loadLabels()">
<div id="pageHeader">
<div id="pageHeaderLogo">
<img src="onion.gif">
</div>
<div id="pageHeaderText">
<a href="http://perldoc.perl.org">perldoc.perl.org</a>
</div>
</div>
<div id="pageBody">
<div id="left">
<div id="leftContent">
<div id="leftClose">
<a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
</div>
<h1>Manual:</h1>
<ul>
<li><a href="index-overview.html">Overview</a></li>
<li><a href="index-tutorials.html">Tutorials</a></li>
<li><a href="index-faq.html">FAQs</a></li>
<li><a href="index-history.html">History / Changes</a></li>
<li><a href="index-licence.html">Licence</a></li>
</ul>
<h2>Reference:</h2>
<ul>
<li><a href="index-language.html">Language</a></li>
<li><a href="index-functions.html">Functions</a></li>
<li><a href="perlop.html">Operators</a></li>
<li><a href="perlvar.html">Special variables</a></li>
<li><a href="index-pragmas.html">Pragmas</a></li>
<li><a href="index-modules-A.html">Core modules</a></li>
<li><a href="index-utilities.html">Utilities</a></li>
<li><a href="index-internals.html">Internals</a></li>
<li><a href="index-platforms.html">Platform specific</a></li>
</ul>
<h2>Links:</h2>
<ul>
<li><a href="http://search.cpan.org">CPAN</a></li>
<li><a href="http://www.perl.org">Perl.org</a></li>
<li><a href="http://www.perl.com">Perl.com</a></li>
<li><a href="http://perlbuzz.com">Perl Buzz</a></li>
<li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
<li><a href="http://jobs.perl.org">Perl Jobs</a></li>
<li><a href="http://www.pm.org">Perl Mongers</a></li>
<li><a href="http://www.perlmonks.org">Perl Monks</a></li>
<li><a href="http://planet.perl.org">Planet Perl</a></li>
<li><a href="http://use.perl.org">Use Perl</a></li>
</ul>
<h2>Contact:</h2>
<ul>
<li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
(<a href="http://perl.jonallen.info">JJ</a>)</li>
<li class="spaced">Last updated on<br>23 December 2007</li>
<li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
more details</li>
</ul>
</div>
</div>
<div id="center">
<div id="centerContent">
<div id="contentHeader">
<div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
<div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
<div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
</div>
<div id="breadCrumbs"><a href="index.html">Home</a> > <a href="index-language.html">Language reference</a> > perlop</div>
<script language="JavaScript">fromSearch();</script>
<div id="contentBody"><div class="title_container"><div class="page_title">perlop</div></div><ul><li><a href="#NAME">NAME
</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Operator-Precedence-and-Associativity">Operator Precedence and Associativity
</a><li><a href="#Terms-and-List-Operators-(Leftward)">Terms and List Operators (Leftward)
</a><li><a href="#The-Arrow-Operator">The Arrow Operator
</a><li><a href="#Auto-increment-and-Auto-decrement">Auto-increment and Auto-decrement
</a><li><a href="#Exponentiation">Exponentiation
</a><li><a href="#Symbolic-Unary-Operators">Symbolic Unary Operators
</a><li><a href="#Binding-Operators">Binding Operators
</a><li><a href="#Multiplicative-Operators">Multiplicative Operators
</a><li><a href="#Additive-Operators">Additive Operators
</a><li><a href="#Shift-Operators">Shift Operators
</a><li><a href="#Named-Unary-Operators">Named Unary Operators
</a><li><a href="#Relational-Operators">Relational Operators
</a><li><a href="#Equality-Operators">Equality Operators
</a><li><a href="#Bitwise-And">Bitwise And
</a><li><a href="#Bitwise-Or-and-Exclusive-Or">Bitwise Or and Exclusive Or
</a><li><a href="#C-style-Logical-And">C-style Logical And
</a><li><a href="#C-style-Logical-Or">C-style Logical Or
</a><li><a href="#C-style-Logical-Defined-Or">C-style Logical Defined-Or
</a><li><a href="#Range-Operators">Range Operators
</a><li><a href="#Conditional-Operator">Conditional Operator
</a><li><a href="#Assignment-Operators">Assignment Operators
</a><li><a href="#Comma-Operator">Comma Operator
</a><li><a href="#List-Operators-(Rightward)">List Operators (Rightward)
</a><li><a href="#Logical-Not">Logical Not
</a><li><a href="#Logical-And">Logical And
</a><li><a href="#Logical-or%2c-Defined-or%2c-and-Exclusive-Or">Logical or, Defined or, and Exclusive Or
</a><li><a href="#C-Operators-Missing-From-Perl">C Operators Missing From Perl
</a><li><a href="#Quote-and-Quote-like-Operators">Quote and Quote-like Operators
</a><li><a href="#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators
</a><li><a href="#Quote-Like-Operators">Quote-Like Operators
</a><li><a href="#Gory-details-of-parsing-quoted-constructs">Gory details of parsing quoted constructs
</a><li><a href="#I%2fO-Operators">I/O Operators
</a><li><a href="#Constant-Folding">Constant Folding
</a><li><a href="#No-ops">No-ops
</a><li><a href="#Bitwise-String-Operators">Bitwise String Operators
</a><li><a href="#Integer-Arithmetic">Integer Arithmetic
</a><li><a href="#Floating-point-Arithmetic">Floating-point Arithmetic
</a><li><a href="#Bigger-Numbers">Bigger Numbers
</a></ul></ul><a name="NAME"></a><h1>NAME
</h1>
<p>perlop - Perl operators and precedence</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<a name="Operator-Precedence-and-Associativity"></a><h2>Operator Precedence and Associativity
</h2>
<p>Operator precedence and associativity work in Perl more or less like
they do in mathematics.</p>
<p><i>Operator precedence</i> means some operators are evaluated before
others. For example, in <code class="inline"><span class="n">2</span> + <span class="n">4</span> * <span class="n">5</span></code>
, the multiplication has higher
precedence so <code class="inline"><span class="n">4</span> * <span class="n">5</span></code>
is evaluated first yielding <code class="inline"><span class="n">2</span> + <span class="n">20</span> ==
<span class="n">22</span></code>
and not <code class="inline"><span class="n">6</span> * <span class="n">5</span> == <span class="n">30</span></code>
.</p>
<p><i>Operator associativity</i> defines what happens if a sequence of the
same operators is used one after another: whether the evaluator will
evaluate the left operations first or the right. For example, in <code class="inline"><span class="n">8</span>
- <span class="n">4</span> - <span class="n">2</span></code>
, subtraction is left associative so Perl evaluates the
expression left to right. <code class="inline"><span class="n">8</span> - <span class="n">4</span></code>
is evaluated first making the
expression <code class="inline"><span class="n">4</span> - <span class="n">2</span> == <span class="n">2</span></code>
and not <code class="inline"><span class="n">8</span> - <span class="n">2</span> == <span class="n">6</span></code>
.</p>
<p>Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. Operators borrowed from
C keep the same precedence relationship with each other, even where
C's precedence is slightly screwy. (This makes learning Perl easier
for C folks.) With very few exceptions, these all operate on scalar
values only, not array values.</p>
<pre class="verbatim"> left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor</pre><p>In the following sections, these operators are covered in precedence order.</p>
<p>Many operators can be overloaded for objects. See <a href="overload.html">overload</a>.</p>
<a name="Terms-and-List-Operators-(Leftward)"></a><h2>Terms and List Operators (Leftward)
</h2>
<p>A TERM has the highest precedence in Perl. They include variables,
quote and quote-like operators, any expression in parentheses,
and any function whose arguments are parenthesized. Actually, there
aren't really functions in this sense, just list operators and unary
operators behaving as functions because you put parentheses around
the arguments. These are all documented in <a href="perlfunc.html">perlfunc</a>.</p>
<p>If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
is followed by a left parenthesis as the next token, the operator and
arguments within parentheses are taken to be of highest precedence,
just like a normal function call.</p>
<p>In the absence of parentheses, the precedence of list operators such as
<code class="inline"><a class="l_k" href="functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="functions/sort.html">sort</a></code>, or <code class="inline"><a class="l_k" href="functions/chmod.html">chmod</a></code> is either very high or very low depending on
whether you are looking at the left side or the right side of the operator.
For example, in</p>
<pre class="verbatim"> <span class="i">@ary</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="n">4</span><span class="cm">,</span> <span class="n">2</span><span class="s">)</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">@ary</span><span class="sc">;</span> <span class="c"># prints 1324</span></pre>
<p>the commas on the right of the sort are evaluated before the sort,
but the commas on the left are evaluated after. In other words,
list operators tend to gobble up all arguments that follow, and
then act like a simple TERM with regard to the preceding expression.
Be careful with parentheses:</p>
<pre class="verbatim"> <span class="c"># These evaluate exit before doing the print:</span>
<a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <a class="l_k" href="functions/exit.html">exit</a><span class="s">)</span><span class="sc">;</span> <span class="c"># Obviously not what you want.</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$foo</span><span class="cm">,</span> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span> <span class="c"># Nor is this.</span></pre>
<pre class="verbatim"> <span class="c"># These do the print before evaluating exit:</span>
<span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="i">$foo</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span> <span class="c"># This is what you want.</span>
<a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span> <span class="c"># Or this.</span>
<a class="l_k" href="functions/print.html">print</a> <span class="s">(</span><span class="i">$foo</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span> <span class="c"># Or even this.</span></pre>
<p>Also note that</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="s">(</span><span class="i">$foo</span> & <span class="n">255</span><span class="s">)</span> + <span class="n">1</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></pre>
<p>probably doesn't do what you expect at first glance. The parentheses
enclose the argument list for <code class="inline"><a class="l_k" href="functions/print.html">print</a></code> which is evaluated (printing
the result of <code class="inline"><span class="i">$foo</span> & <span class="n">255</span></code>
). Then one is added to the return value
of <code class="inline"><a class="l_k" href="functions/print.html">print</a></code> (usually 1). The result is something like this:</p>
<pre class="verbatim"> <span class="n">1</span> + <span class="n">1</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span> <span class="c"># Obviously not what you meant.</span></pre>
<p>To do what you meant properly, you must write:</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="s">(</span><span class="i">$foo</span> & <span class="n">255</span><span class="s">)</span> + <span class="n">1</span><span class="cm">,</span> <span class="q">"\n"</span><span class="s">)</span><span class="sc">;</span></pre>
<p>See <a href="#Named-Unary-Operators">"Named Unary Operators"</a> for more discussion of this.</p>
<p>Also parsed as terms are the <code class="inline"><a class="l_k" href="functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
and <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
constructs, as
well as subroutine and method calls, and the anonymous
constructors <code class="inline"><span class="s">[</span><span class="s">]</span></code>
and <code class="inline"><span class="s">{</span><span class="s">}</span></code>
.</p>
<p>See also <a href="#Quote-and-Quote-like-Operators">"Quote and Quote-like Operators"</a> toward the end of this section,
as well as <a href="#I%2fO-Operators">"I/O Operators"</a>.</p>
<a name="The-Arrow-Operator"></a><h2>The Arrow Operator
</h2>
<p>"<code class="inline">-></code>
" is an infix dereference operator, just as it is in C
and C++. If the right side is either a <code class="inline"><span class="s">[</span>...<span class="s">]</span></code>
, <code class="inline"><span class="s">{</span>...<span class="s">}</span></code>
, or a
<code class="inline"><span class="s">(</span>...<span class="s">)</span></code>
subscript, then the left side must be either a hard or
symbolic reference to an array, a hash, or a subroutine respectively.
(Or technically speaking, a location capable of holding a hard
reference, if it's an array or hash reference being used for
assignment.) See <a href="perlreftut.html">perlreftut</a> and <a href="perlref.html">perlref</a>.</p>
<p>Otherwise, the right side is a method name or a simple scalar
variable containing either the method name or a subroutine reference,
and the left side must be either an object (a blessed reference)
or a class name (that is, a package name). See <a href="perlobj.html">perlobj</a>.</p>
<a name="Auto-increment-and-Auto-decrement"></a><h2>Auto-increment and Auto-decrement
</h2>
<p>"++" and "--" work as in C. That is, if placed before a variable,
they increment or decrement the variable by one before returning the
value, and if placed after, increment or decrement after returning the
value.</p>
<pre class="verbatim"> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$j</span> = <span class="n">0</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$i</span>++<span class="sc">;</span> <span class="c"># prints 0</span>
<a class="l_k" href="functions/print.html">print</a> ++<span class="i">$j</span><span class="sc">;</span> <span class="c"># prints 1</span></pre>
<p>Note that just as in C, Perl doesn't define <b>when</b> the variable is
incremented or decremented. You just know it will be done sometime
before or after the value is returned. This also means that modifying
a variable twice in the same statement will lead to undefined behaviour.
Avoid statements like:</p>
<pre class="verbatim"> <span class="i">$i</span> = <span class="i">$i</span> ++<span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> ++ <span class="i">$i</span> + <span class="i">$i</span> ++<span class="sc">;</span></pre>
<p>Perl will not guarantee what the result of the above statements is.</p>
<p>The auto-increment operator has a little extra builtin magic to it. If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment. If, however, the
variable has been used in only string contexts since it was set, and
has a value that is not the empty string and matches the pattern
<code class="inline"><span class="q">/^[a-zA-Z]*[0-9]*\z/</span></code>
, the increment is done as a string, preserving each
character within its range, with carry:</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> ++<span class="s">(</span><span class="i">$foo</span> = <span class="q">'99'</span><span class="s">)</span><span class="sc">;</span> <span class="c"># prints '100'</span>
<a class="l_k" href="functions/print.html">print</a> ++<span class="s">(</span><span class="i">$foo</span> = <span class="q">'a0'</span><span class="s">)</span><span class="sc">;</span> <span class="c"># prints 'a1'</span>
<a class="l_k" href="functions/print.html">print</a> ++<span class="s">(</span><span class="i">$foo</span> = <span class="q">'Az'</span><span class="s">)</span><span class="sc">;</span> <span class="c"># prints 'Ba'</span>
<a class="l_k" href="functions/print.html">print</a> ++<span class="s">(</span><span class="i">$foo</span> = <span class="q">'zz'</span><span class="s">)</span><span class="sc">;</span> <span class="c"># prints 'aaa'</span></pre>
<p><code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is always treated as numeric, and in particular is changed
to <code class="inline"><span class="n">0</span></code>
before incrementing (so that a post-increment of an undef value
will return <code class="inline"><span class="n">0</span></code>
rather than <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>).</p>
<p>The auto-decrement operator is not magical.</p>
<a name="Exponentiation"></a><h2>Exponentiation
</h2>
<p>Binary "**" is the exponentiation operator. It binds even more
tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
implemented using C's pow(3) function, which actually works on doubles
internally.)</p>
<a name="Symbolic-Unary-Operators"></a><h2>Symbolic Unary Operators
</h2>
<p>Unary "!" performs logical negation, i.e., "not". See also <code class="inline">not</code>
for a lower
precedence version of this.
</p>
<p>Unary "-" performs arithmetic negation if the operand is numeric. If
the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned. Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
is returned. One effect of these rules is that -bareword is equivalent
to the string "-bareword". If, however, the string begins with a
non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert
the string to a numeric and the arithmetic negation is performed. If the
string cannot be cleanly converted to a numeric, Perl will give the warning
<b>Argument "the string" isn't numeric in negation (-) at ...</b>.
</p>
<p>Unary "~" performs bitwise negation, i.e., 1's complement. For
example, <code class="inline"><span class="n">0666</span> & ~<span class="n">027</span></code>
is 0640. (See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a> and
<a href="#Bitwise-String-Operators">"Bitwise String Operators"</a>.) Note that the width of the result is
platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64
bits wide on a 64-bit platform, so if you are expecting a certain bit
width, remember to use the & operator to mask off the excess bits.
</p>
<p>Unary "+" has no effect whatsoever, even on strings. It is useful
syntactically for separating a function name from a parenthesized expression
that would otherwise be interpreted as the complete list of function
arguments. (See examples above under <a href="#Terms-and-List-Operators-(Leftward)">"Terms and List Operators (Leftward)"</a>.)
</p>
<p>Unary "\" creates a reference to whatever follows it. See <a href="perlreftut.html">perlreftut</a>
and <a href="perlref.html">perlref</a>. Do not confuse this behavior with the behavior of
backslash within a string, although both forms do convey the notion
of protecting the next thing from interpolation.
</p>
<a name="Binding-Operators"></a><h2>Binding Operators
</h2>
<p>Binary "=~" binds a scalar expression to a pattern match. Certain operations
search or modify the string $_ by default. This operator makes that kind
of operation work on some other string. The right argument is a search
pattern, substitution, or transliteration. The left argument is what is
supposed to be searched, substituted, or transliterated instead of the default
$_. When used in scalar context, the return value generally indicates the
success of the operation. Behavior in list context depends on the particular
operator. See <a href="#Regexp-Quote-Like-Operators">"Regexp Quote-Like Operators"</a> for details and
<a href="perlretut.html">perlretut</a> for examples using these operators.</p>
<p>If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at run
time. Note that this means that its contents will be interpolated twice, so</p>
<pre class="verbatim"> <span class="q">'\\'</span> =~ <span class="q">q'\\'</span><span class="sc">;</span></pre>
<p>is not ok, as the regex engine will end up trying to compile the
pattern <code class="inline">\</code>
, which it will consider a syntax error.</p>
<p>Binary "!~" is just like "=~" except the return value is negated in
the logical sense.</p>
<a name="Multiplicative-Operators"></a><h2>Multiplicative Operators
</h2>
<p>Binary "*" multiplies two numbers.
</p>
<p>Binary "/" divides two numbers.
</p>
<p>Binary "%" computes the modulus of two numbers. Given integer
operands <code class="inline"><span class="i">$a</span></code>
and <code class="inline"><span class="i">$b</span></code>
: If <code class="inline"><span class="i">$b</span></code>
is positive, then <code class="inline"><span class="i">$a</span> % <span class="i">$b</span></code>
is
<code class="inline"><span class="i">$a</span></code>
minus the largest multiple of <code class="inline"><span class="i">$b</span></code>
that is not greater than
<code class="inline"><span class="i">$a</span></code>
. If <code class="inline"><span class="i">$b</span></code>
is negative, then <code class="inline"><span class="i">$a</span> % <span class="i">$b</span></code>
is <code class="inline"><span class="i">$a</span></code>
minus the
smallest multiple of <code class="inline"><span class="i">$b</span></code>
that is not less than <code class="inline"><span class="i">$a</span></code>
(i.e. the
result will be less than or equal to zero). If the operands
<code class="inline"><span class="i">$a</span></code>
and <code class="inline"><span class="i">$b</span></code>
are floating point values and the absolute value of
<code class="inline"><span class="i">$b</span></code>
(that is <code class="inline"><a class="l_k" href="functions/abs.html">abs($b)</a></code>) is less than <code class="inline"><span class="s">(</span><span class="w">UV_MAX</span> + <span class="n">1</span><span class="s">)</span></code>
, only
the integer portion of <code class="inline"><span class="i">$a</span></code>
and <code class="inline"><span class="i">$b</span></code>
will be used in the operation
(Note: here <code class="inline"><span class="w">UV_MAX</span></code>
means the maximum of the unsigned integer type).
If the absolute value of the right operand (<code class="inline"><a class="l_k" href="functions/abs.html">abs($b)</a></code>) is greater than
or equal to <code class="inline"><span class="s">(</span><span class="w">UV_MAX</span> + <span class="n">1</span><span class="s">)</span></code>
, "%" computes the floating-point remainder
<code class="inline"><span class="i">$r</span></code>
in the equation <code class="inline"><span class="s">(</span><span class="i">$r</span> = <span class="i">$a</span> - <span class="i">$i</span>*<span class="i">$b</span><span class="s">)</span></code>
where <code class="inline"><span class="i">$i</span></code>
is a certain
integer that makes <code class="inline"><span class="i">$r</span></code>
should have the same sign as the right operand
<code class="inline"><span class="i">$b</span></code>
(<b>not</b> as the left operand <code class="inline"><span class="i">$a</span></code>
like C function <code class="inline"><span class="i">fmod</span><span class="s">(</span><span class="s">)</span></code>
)
and the absolute value less than that of <code class="inline"><span class="i">$b</span></code>
.
Note that when <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
is in scope, "%" gives you direct access
to the modulus operator as implemented by your C compiler. This
operator is not as well defined for negative operands, but it will
execute faster.
</p>
<p>Binary "x" is the repetition operator. In scalar context or if the left
operand is not enclosed in parentheses, it returns a string consisting
of the left operand repeated the number of times specified by the right
operand. In list context, if the left operand is enclosed in
parentheses or is a list formed by <code class="inline"><a class="l_k" href="functions/qw.html">qw/STRING/</a></code>, it repeats the list.
If the right operand is zero or negative, it returns an empty string
or an empty list, depending on the context.
</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="q">'-'</span> x <span class="n">80</span><span class="sc">;</span> <span class="c"># print row of dashes</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="q">"\t"</span> x <span class="s">(</span><span class="i">$tab</span>/<span class="n">8</span><span class="s">)</span><span class="cm">,</span> <span class="q">' '</span> x <span class="s">(</span><span class="i">$tab</span>%<span class="n">8</span><span class="s">)</span><span class="sc">;</span> <span class="c"># tab over</span></pre>
<pre class="verbatim"> <span class="i">@ones</span> = <span class="s">(</span><span class="n">1</span><span class="s">)</span> x <span class="n">80</span><span class="sc">;</span> <span class="c"># a list of 80 1's</span>
<span class="i">@ones</span> = <span class="s">(</span><span class="n">5</span><span class="s">)</span> x <span class="i">@ones</span><span class="sc">;</span> <span class="c"># set all elements to 5</span></pre>
<a name="Additive-Operators"></a><h2>Additive Operators
</h2>
<p>Binary "+" returns the sum of two numbers.
</p>
<p>Binary "-" returns the difference of two numbers.
</p>
<p>Binary "." concatenates two strings.
</p>
<a name="Shift-Operators"></a><h2>Shift Operators
</h2>
<p>Binary "<<" returns the value of its left argument shifted left by the
number of bits specified by the right argument. Arguments should be
integers. (See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a>.)</p>
<p>Binary ">>" returns the value of its left argument shifted right by
the number of bits specified by the right argument. Arguments should
be integers. (See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a>.)</p>
<p>Note that both "<<" and ">>" in Perl are implemented directly using
"<<" and ">>" in C. If <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
(see <a href="#Integer-Arithmetic">"Integer Arithmetic"</a>) is
in force then signed C integers are used, else unsigned C integers are
used. Either way, the implementation isn't going to generate results
larger than the size of the integer type Perl was built with (32 bits
or 64 bits).</p>
<p>The result of overflowing the range of the integers is undefined
because it is undefined also in C. In other words, using 32-bit
integers, <code class="inline"><span class="n">1</span> << <span class="n">32</span></code>
is undefined. Shifting by a negative number
of bits is also undefined.</p>
<a name="Named-Unary-Operators"></a><h2>Named Unary Operators
</h2>
<p>The various named unary operators are treated as functions with one
argument, with optional parentheses.</p>
<p>If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
is followed by a left parenthesis as the next token, the operator and
arguments within parentheses are taken to be of highest precedence,
just like a normal function call. For example,
because named unary operators are higher precedence than ||:</p>
<pre class="verbatim"> <a class="l_k" href="functions/chdir.html">chdir</a> <span class="i">$foo</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># (chdir $foo) || die</span>
<a class="l_k" href="functions/chdir.html">chdir</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># (chdir $foo) || die</span>
<a class="l_k" href="functions/chdir.html">chdir</a> <span class="s">(</span><span class="i">$foo</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># (chdir $foo) || die</span>
<a class="l_k" href="functions/chdir.html">chdir</a> +<span class="s">(</span><span class="i">$foo</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># (chdir $foo) || die</span></pre>
<p>but, because * is higher precedence than named operators:</p>
<pre class="verbatim"> <a class="l_k" href="functions/chdir.html">chdir</a> <span class="i">$foo</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># chdir ($foo * 20)</span>
<a class="l_k" href="functions/chdir.html">chdir</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># (chdir $foo) * 20</span>
<a class="l_k" href="functions/chdir.html">chdir</a> <span class="s">(</span><span class="i">$foo</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># (chdir $foo) * 20</span>
<a class="l_k" href="functions/chdir.html">chdir</a> +<span class="s">(</span><span class="i">$foo</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># chdir ($foo * 20)</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/rand.html">rand</a> <span class="n">10</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># rand (10 * 20)</span>
<a class="l_k" href="functions/rand.html">rand</a><span class="s">(</span><span class="n">10</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># (rand 10) * 20</span>
<a class="l_k" href="functions/rand.html">rand</a> <span class="s">(</span><span class="n">10</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># (rand 10) * 20</span>
<a class="l_k" href="functions/rand.html">rand</a> +<span class="s">(</span><span class="n">10</span><span class="s">)</span> * <span class="n">20</span><span class="sc">;</span> <span class="c"># rand (10 * 20)</span></pre>
<p>Regarding precedence, the filetest operators, like <code class="inline">-f</code>
, <code class="inline">-M</code>
, etc. are
treated like named unary operators, but they don't follow this functional
parenthesis rule. That means, for example, that <code class="inline">-f<span class="s">(</span><span class="i">$file</span><span class="s">)</span>.<span class="q">".bak"</span></code>
is
equivalent to <code class="inline">-f <span class="q">"$file.bak"</span></code>
.
</p>
<p>See also <a href="#Terms-and-List-Operators-(Leftward)">"Terms and List Operators (Leftward)"</a>.</p>
<a name="Relational-Operators"></a><h2>Relational Operators
</h2>
<p>Binary "<" returns true if the left argument is numerically less than
the right argument.
</p>
<p>Binary ">" returns true if the left argument is numerically greater
than the right argument.
</p>
<p>Binary "<=" returns true if the left argument is numerically less than
or equal to the right argument.
</p>
<p>Binary ">=" returns true if the left argument is numerically greater
than or equal to the right argument.
</p>
<p>Binary "lt" returns true if the left argument is stringwise less than
the right argument.
</p>
<p>Binary "gt" returns true if the left argument is stringwise greater
than the right argument.
</p>
<p>Binary "le" returns true if the left argument is stringwise less than
or equal to the right argument.
</p>
<p>Binary "ge" returns true if the left argument is stringwise greater
than or equal to the right argument.
</p>
<a name="Equality-Operators"></a><h2>Equality Operators
</h2>
<p>Binary "==" returns true if the left argument is numerically equal to
the right argument.
</p>
<p>Binary "!=" returns true if the left argument is numerically not equal
to the right argument.
</p>
<p>Binary "<=>" returns -1, 0, or 1 depending on whether the left
argument is numerically less than, equal to, or greater than the right
argument. If your platform supports NaNs (not-a-numbers) as numeric
values, using them with "<=>" returns undef. NaN is not "<", "==", ">",
"<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN
returns true, as does NaN != anything else. If your platform doesn't
support NaNs then NaN is just a string with numeric value 0.
</p>
<pre class="verbatim"> perl -le '$a = "NaN"; print "No NaN support here" if $a == $a'
perl -le '$a = "NaN"; print "NaN support here" if $a != $a'</pre><p>Binary "eq" returns true if the left argument is stringwise equal to
the right argument.
</p>
<p>Binary "ne" returns true if the left argument is stringwise not equal
to the right argument.
</p>
<p>Binary "cmp" returns -1, 0, or 1 depending on whether the left
argument is stringwise less than, equal to, or greater than the right
argument.
</p>
<p>Binary "~~" does a smart match between its arguments. Smart matching
is described in <a href="perlsyn.html#Smart-matching-in-detail">"Smart matching in detail" in perlsyn</a>.
This operator is only available if you enable the "~~" feature:
see <a href="feature.html">feature</a> for more information.
</p>
<p>"lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified
by the current locale if <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
is in effect. See <a href="perllocale.html">perllocale</a>.</p>
<a name="Bitwise-And"></a><h2>Bitwise And
</h2>
<p>Binary "&" returns its operands ANDed together bit by bit.
(See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a> and <a href="#Bitwise-String-Operators">"Bitwise String Operators"</a>.)</p>
<p>Note that "&" has lower priority than relational operators, so for example
the brackets are essential in a test like</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Even\n"</span> if <span class="s">(</span><span class="i">$x</span> & <span class="n">1</span><span class="s">)</span> == <span class="n">0</span><span class="sc">;</span></pre>
<a name="Bitwise-Or-and-Exclusive-Or"></a><h2>Bitwise Or and Exclusive Or
</h2>
<p>Binary "|" returns its operands ORed together bit by bit.
(See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a> and <a href="#Bitwise-String-Operators">"Bitwise String Operators"</a>.)</p>
<p>Binary "^" returns its operands XORed together bit by bit.
(See also <a href="#Integer-Arithmetic">"Integer Arithmetic"</a> and <a href="#Bitwise-String-Operators">"Bitwise String Operators"</a>.)</p>
<p>Note that "|" and "^" have lower priority than relational operators, so
for example the brackets are essential in a test like</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="q">"false\n"</span> if <span class="s">(</span><span class="n">8</span> | <span class="n">2</span><span class="s">)</span> != <span class="n">10</span><span class="sc">;</span></pre>
<a name="C-style-Logical-And"></a><h2>C-style Logical And
</h2>
<p>Binary "&&" performs a short-circuit logical AND operation. That is,
if the left operand is false, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.</p>
<a name="C-style-Logical-Or"></a><h2>C-style Logical Or
</h2>
<p>Binary "||" performs a short-circuit logical OR operation. That is,
if the left operand is true, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.</p>
<a name="C-style-Logical-Defined-Or"></a><h2>C-style Logical Defined-Or
</h2>
<p>Although it has no direct equivalent in C, Perl's <code class="inline"><span class="q">//</span></code>
operator is related
to its C-style or. In fact, it's exactly the same as <code class="inline">||</code>, except that it
tests the left hand side's definedness instead of its truth. Thus, <code class="inline"><span class="i">$a</span> // <span class="i">$b</span></code>
is similar to <code class="inline"><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span> || <span class="i">$b</span></code>
(except that it returns the value of <code class="inline"><span class="i">$a</span></code>
rather than the value of <code class="inline"><a class="l_k" href="functions/defined.html">defined($a)</a></code>) and is exactly equivalent to
<code class="inline"><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span></code>
. This is very useful for providing default values
for variables. If you actually want to test if at least one of <code class="inline"><span class="i">$a</span></code>
and
<code class="inline"><span class="i">$b</span></code>
is defined, use <code class="inline"><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$a</span> // <span class="i">$b</span><span class="s">)</span></code>
.</p>
<p>The <code class="inline">||</code>, <code class="inline"><span class="q">//</span></code>
and <code class="inline">&&</code> operators return the last value evaluated
(unlike C's <code class="inline">||</code> and <code class="inline">&&</code>, which return 0 or 1). Thus, a reasonably
portable way to find out the home directory might be:</p>
<pre class="verbatim"> <span class="i">$home</span> = <span class="i">$ENV</span>{<span class="q">'HOME'</span>} // <span class="i">$ENV</span>{<span class="q">'LOGDIR'</span>} //
<span class="s">(</span><a class="l_k" href="functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$<</span><span class="s">)</span><span class="s">)</span>[<span class="n">7</span>] // <a class="l_k" href="functions/die.html">die</a> <span class="q">"You're homeless!\n"</span><span class="sc">;</span></pre>
<p>In particular, this means that you shouldn't use this
for selecting between two aggregates for assignment:</p>
<pre class="verbatim"> <span class="i">@a</span> = <span class="i">@b</span> || <span class="i">@c</span><span class="sc">;</span> <span class="c"># this is wrong</span>
<span class="i">@a</span> = <a class="l_k" href="functions/scalar.html">scalar</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span> || <span class="i">@c</span><span class="sc">;</span> <span class="c"># really meant this</span>
<span class="i">@a</span> = <span class="i">@b</span> ? <span class="i">@b</span> <span class="co">:</span> <span class="i">@c</span><span class="sc">;</span> <span class="c"># this works fine, though</span></pre>
<p>As more readable alternatives to <code class="inline">&&</code> and <code class="inline">||</code> when used for
control flow, Perl provides the <code class="inline">and</code>
and <code class="inline">or</code>
operators (see below).
The short-circuit behavior is identical. The precedence of "and"
and "or" is much lower, however, so that you can safely use them after a
list operator without the need for parentheses:</p>
<pre class="verbatim"> <a class="l_k" href="functions/unlink.html">unlink</a> <span class="q">"alpha"</span><span class="cm">,</span> <span class="q">"beta"</span><span class="cm">,</span> <span class="q">"gamma"</span>
or <span class="i">gripe</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">LINE</span><span class="sc">;</span></pre>
<p>With the C-style operators that would have been written like this:</p>
<pre class="verbatim"> <a class="l_k" href="functions/unlink.html">unlink</a><span class="s">(</span><span class="q">"alpha"</span><span class="cm">,</span> <span class="q">"beta"</span><span class="cm">,</span> <span class="q">"gamma"</span><span class="s">)</span>
|| <span class="s">(</span><span class="i">gripe</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">LINE</span><span class="s">)</span><span class="sc">;</span></pre>
<p>Using "or" for assignment is unlikely to do what you want; see below.</p>
<a name="Range-Operators"></a><h2>Range Operators
</h2>
<p>Binary ".." is the range operator, which is really two different
operators depending on the context. In list context, it returns a
list of values counting (up by ones) from the left value to the right
value. If the left value is greater than the right value then it
returns the empty list. The range operator is useful for writing
<code class="inline">foreach <span class="s">(</span><span class="n">1</span>..<span class="n">10</span><span class="s">)</span></code>
loops and for doing slice operations on arrays. In
the current implementation, no temporary array is created when the
range operator is used as the expression in <code class="inline">foreach</code>
loops, but older
versions of Perl might burn a lot of memory when you write something
like this:</p>
<pre class="verbatim"> for <span class="s">(</span><span class="n">1</span> .. <span class="n">1_000_000</span><span class="s">)</span> <span class="s">{</span>
<span class="c"># code</span>
<span class="s">}</span></pre>
<p>The range operator also works on strings, using the magical auto-increment,
see below.</p>
<p>In scalar context, ".." returns a boolean value. The operator is
bistable, like a flip-flop, and emulates the line-range (comma) operator
of <b>sed</b>, <b>awk</b>, and various editors. Each ".." operator maintains its
own boolean state. It is false as long as its left operand is false.
Once the left operand is true, the range operator stays true until the
right operand is true, <i>AFTER</i> which the range operator becomes false
again. It doesn't become false till the next time the range operator is
evaluated. It can test the right operand and become false on the same
evaluation it became true (as in <b>awk</b>), but it still returns true once.
If you don't want it to test the right operand till the next
evaluation, as in <b>sed</b>, just use three dots ("...") instead of
two. In all other regards, "..." behaves just like ".." does.</p>
<p>The right operand is not evaluated while the operator is in the
"false" state, and the left operand is not evaluated while the
operator is in the "true" state. The precedence is a little lower
than || and &&. The value returned is either the empty string for
false, or a sequence number (beginning with 1) for true. The
sequence number is reset for each range encountered. The final
sequence number in a range has the string "E0" appended to it, which
doesn't affect its numeric value, but gives you something to search
for if you want to exclude the endpoint. You can exclude the
beginning point by waiting for the sequence number to be greater
than 1.</p>
<p>If either operand of scalar ".." is a constant expression,
that operand is considered true if it is equal (<code class="inline">==</code>
) to the current
input line number (the <code class="inline"><span class="i">$.</span></code>
variable).</p>
<p>To be pedantic, the comparison is actually <code class="inline"><a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> == <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
,
but that is only an issue if you use a floating point expression; when
implicitly using <code class="inline"><span class="i">$.</span></code>
as described in the previous paragraph, the
comparison is <code class="inline"><a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> == <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="i">$.</span><span class="s">)</span></code>
which is only an issue when <code class="inline"><span class="i">$.</span></code>
is set to a floating point value and you are not reading from a file.
Furthermore, <code class="inline"><span class="q">"span"</span> .. <span class="q">"spat"</span></code>
or <code class="inline"><span class="n">2.18</span> .. <span class="n">3.14</span></code>
will not do what
you want in scalar context because each of the operands are evaluated
using their integer representation.</p>
<p>Examples:</p>
<p>As a scalar operator:</p>
<pre class="verbatim"> if <span class="s">(</span><span class="n">101</span> .. <span class="n">200</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span> <span class="c"># print 2nd hundred lines, short for</span>
<span class="c"># if ($. == 101 .. $. == 200) ...</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/next.html">next</a> <span class="j">LINE</span> if <span class="s">(</span><span class="n">1</span> .. <span class="q">/^$/</span><span class="s">)</span><span class="sc">;</span> <span class="c"># skip header lines, short for</span>
<span class="c"># ... if ($. == 1 .. /^$/);</span>
<span class="c"># (typically in a loop labeled LINE)</span></pre>
<pre class="verbatim"> <span class="q">s/^/> /</span> if <span class="s">(</span><span class="q">/^$/</span> .. <a class="l_k" href="functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span> <span class="c"># quote body</span></pre>
<pre class="verbatim"> <span class="c"># parse mail messages</span>
while <span class="s">(</span><><span class="s">)</span> <span class="s">{</span>
<span class="i">$in_header</span> = <span class="n">1</span> .. <span class="q">/^$/</span><span class="sc">;</span>
<span class="i">$in_body</span> = <span class="q">/^$/</span> .. <a class="l_k" href="functions/eof.html">eof</a><span class="sc">;</span>
if <span class="s">(</span><span class="i">$in_header</span><span class="s">)</span> <span class="s">{</span>
<span class="c"># ...</span>
<span class="s">}</span> else <span class="s">{</span> <span class="c"># in body</span>
<span class="c"># ...</span>
<span class="s">}</span>
<span class="s">}</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">{</span>
<a class="l_k" href="functions/close.html">close</a> <span class="w">ARGV</span> if <a class="l_k" href="functions/eof.html">eof</a><span class="sc">;</span> <span class="c"># reset $. each file</span>
<span class="s">}</span></pre>
<p>Here's a simple example to illustrate the difference between
the two range operators:</p>
<pre class="verbatim"> <span class="i">@lines</span> = <span class="s">(</span><span class="q">" - Foo"</span><span class="cm">,</span>
<span class="q">"01 - Bar"</span><span class="cm">,</span>
<span class="q">"1 - Baz"</span><span class="cm">,</span>
<span class="q">" - Quux"</span><span class="s">)</span><span class="sc">;</span></pre>
<pre class="verbatim"> foreach <span class="s">(</span><span class="i">@lines</span><span class="s">)</span> <span class="s">{</span>
if <span class="s">(</span><span class="q">/0/</span> .. <span class="q">/1/</span><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"$_\n"</span><span class="sc">;</span>
<span class="s">}</span>
<span class="s">}</span></pre>
<p>This program will print only the line containing "Bar". If
the range operator is changed to <code class="inline">...</code>
, it will also print the
"Baz" line.</p>
<p>And now some examples as a list operator:</p>
<pre class="verbatim"> for <span class="s">(</span><span class="n">101</span> .. <span class="n">200</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span> <span class="c"># print $_ 100 times</span>
<span class="i">@foo</span> = <span class="i">@foo</span>[<span class="n">0</span> .. <span class="i">$#foo</span>]<span class="sc">;</span> <span class="c"># an expensive no-op</span>
<span class="i">@foo</span> = <span class="i">@foo</span>[<span class="i">$#foo</span>-<span class="n">4</span> .. <span class="i">$#foo</span>]<span class="sc">;</span> <span class="c"># slice last 5 items</span></pre>
<p>The range operator (in list context) makes use of the magical
auto-increment algorithm if the operands are strings. You
can say</p>
<pre class="verbatim"> <span class="i">@alphabet</span> = <span class="s">(</span><span class="q">'A'</span> .. <span class="q">'Z'</span><span class="s">)</span><span class="sc">;</span></pre>
<p>to get all normal letters of the English alphabet, or</p>
<pre class="verbatim"> <span class="i">$hexdigit</span> = <span class="s">(</span><span class="n">0</span> .. <span class="n">9</span><span class="cm">,</span> <span class="q">'a'</span> .. <span class="q">'f'</span><span class="s">)</span>[<span class="i">$num</span> & <span class="n">15</span>]<span class="sc">;</span></pre>
<p>to get a hexadecimal digit, or</p>
<pre class="verbatim"> <span class="i">@z2</span> = <span class="s">(</span><span class="q">'01'</span> .. <span class="q">'31'</span><span class="s">)</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">$z2</span>[<span class="i">$mday</span>]<span class="sc">;</span></pre>
<p>to get dates with leading zeros.</p>
<p>If the final value specified is not in the sequence that the magical
increment would produce, the sequence goes until the next value would
be longer than the final value specified.</p>
<p>If the initial value specified isn't part of a magical increment
sequence (that is, a non-empty string matching "/^[a-zA-Z]*[0-9]*\z/"),
only the initial value will be returned. So the following will only
return an alpha:</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">'greek'</span><span class="sc">;</span>
<a class="l_k" href="functions/my.html">my</a> <span class="i">@greek_small</span> = <span class="s">(</span><span class="q">"\N{alpha}"</span> .. <span class="q">"\N{omega}"</span><span class="s">)</span><span class="sc">;</span></pre>
<p>To get lower-case greek letters, use this instead:</p>
<pre class="verbatim"> <a class="l_k" href="functions/my.html">my</a> <span class="i">@greek_small</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="functions/chr.html">chr</a> <span class="s">}</span> <span class="s">(</span> <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">"\N{alpha}"</span><span class="s">)</span> .. <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">"\N{omega}"</span><span class="s">)</span> <span class="s">)</span><span class="sc">;</span></pre>
<p>Because each operand is evaluated in integer form, <code class="inline"><span class="n">2.18</span> .. <span class="n">3.14</span></code>
will
return two elements in list context.</p>
<pre class="verbatim"> <span class="i">@list</span> = <span class="s">(</span><span class="n">2.18</span> .. <span class="n">3.14</span><span class="s">)</span><span class="sc">;</span> <span class="c"># same as @list = (2 .. 3);</span></pre>
<a name="Conditional-Operator"></a><h2>Conditional Operator
</h2>
<p>Ternary "?:" is the conditional operator, just as in C. It works much
like an if-then-else. If the argument before the ? is true, the
argument before the : is returned, otherwise the argument after the :
is returned. For example:</p>
<pre class="verbatim"> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">"I have %d dog%s.\n"</span><span class="cm">,</span> <span class="i">$n</span><span class="cm">,</span>
<span class="s">(</span><span class="i">$n</span> == <span class="n">1</span><span class="s">)</span> ? <span class="q">''</span> <span class="co">:</span> <span class="q">"s"</span><span class="sc">;</span></pre>
<p>Scalar or list context propagates downward into the 2nd
or 3rd argument, whichever is selected.</p>
<pre class="verbatim"> <span class="i">$a</span> = <span class="i">$ok</span> ? <span class="i">$b</span> <span class="co">:</span> <span class="i">$c</span><span class="sc">;</span> <span class="c"># get a scalar</span>
<span class="i">@a</span> = <span class="i">$ok</span> ? <span class="i">@b</span> <span class="co">:</span> <span class="i">@c</span><span class="sc">;</span> <span class="c"># get an array</span>
<span class="i">$a</span> = <span class="i">$ok</span> ? <span class="i">@b</span> <span class="co">:</span> <span class="i">@c</span><span class="sc">;</span> <span class="c"># oops, that's just a count!</span></pre>
<p>The operator may be assigned to if both the 2nd and 3rd arguments are
legal lvalues (meaning that you can assign to them):</p>
<pre class="verbatim"> <span class="s">(</span><span class="i">$a_or_b</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">$c</span><span class="sc">;</span></pre>
<p>Because this operator produces an assignable result, using assignments
without parentheses will get you in trouble. For example, this:</p>
<pre class="verbatim"> <span class="i">$a</span> % <span class="n">2</span> ? <span class="i">$a</span> += <span class="n">10</span> <span class="co">:</span> <span class="i">$a</span> += <span class="n">2</span></pre>
<p>Really means this:</p>
<pre class="verbatim"> <span class="s">(</span><span class="s">(</span><span class="i">$a</span> % <span class="n">2</span><span class="s">)</span> ? <span class="s">(</span><span class="i">$a</span> += <span class="n">10</span><span class="s">)</span> <span class="co">:</span> <span class="i">$a</span><span class="s">)</span> += <span class="n">2</span></pre>
<p>Rather than this:</p>
<pre class="verbatim"> <span class="s">(</span><span class="i">$a</span> % <span class="n">2</span><span class="s">)</span> ? <span class="s">(</span><span class="i">$a</span> += <span class="n">10</span><span class="s">)</span> <span class="co">:</span> <span class="s">(</span><span class="i">$a</span> += <span class="n">2</span><span class="s">)</span></pre>
<p>That should probably be written more simply as:</p>
<pre class="verbatim"> <span class="i">$a</span> += <span class="s">(</span><span class="i">$a</span> % <span class="n">2</span><span class="s">)</span> ? <span class="n">10</span> <span class="co">:</span> <span class="n">2</span><span class="sc">;</span></pre>
<a name="Assignment-Operators"></a><h2>Assignment Operators
</h2>
<p>"=" is the ordinary assignment operator.</p>
<p>Assignment operators work as in C. That is,</p>
<pre class="verbatim"> <span class="i">$a</span> += <span class="n">2</span><span class="sc">;</span></pre>
<p>is equivalent to</p>
<pre class="verbatim"> <span class="i">$a</span> = <span class="i">$a</span> + <span class="n">2</span><span class="sc">;</span></pre>
<p>although without duplicating any side effects that dereferencing the lvalue
might trigger, such as from tie(). Other assignment operators work similarly.
The following are recognized:</p>
<pre class="verbatim"> <span class="i">**</span>= += <span class="i">*=</span> &= <<= &&=
-= <span class="q">/= |= >>= ||=</span>
<span class="q"> .= %= ^= /</span>/=
<span class="w">x</span>=</pre>
<p>Although these are grouped by family, they all have the precedence
of assignment.</p>
<p>Unlike in C, the scalar assignment operator produces a valid lvalue.
Modifying an assignment is equivalent to doing the assignment and
then modifying the variable that was assigned to. This is useful
for modifying a copy of something, like this:</p>
<pre class="verbatim"> <span class="s">(</span><span class="i">$tmp</span> = <span class="i">$global</span><span class="s">)</span> =~ <span class="q">tr [A-Z] [a-z]</span><span class="sc">;</span></pre>
<p>Likewise,</p>
<pre class="verbatim"> <span class="s">(</span><span class="i">$a</span> += <span class="n">2</span><span class="s">)</span> *= <span class="n">3</span><span class="sc">;</span></pre>
<p>is equivalent to</p>
<pre class="verbatim"> <span class="i">$a</span> += <span class="n">2</span><span class="sc">;</span>
<span class="i">$a</span> *= <span class="n">3</span><span class="sc">;</span></pre>
<p>Similarly, a list assignment in list context produces the list of
lvalues assigned to, and a list assignment in scalar context returns
the number of elements produced by the expression on the right hand
side of the assignment.</p>
<a name="Comma-Operator"></a><h2>Comma Operator
</h2>
<p>Binary "," is the comma operator. In scalar context it evaluates
its left argument, throws that value away, then evaluates its right
argument and returns that value. This is just like C's comma operator.</p>
<p>In list context, it's just the list argument separator, and inserts
both its arguments into the list. These arguments are also evaluated
from left to right.</p>
<p>The <code class="inline"><span class="cm">=></span></code>
operator is a synonym for the comma, but forces any word
(consisting entirely of word characters) to its left to be interpreted
as a string (as of 5.001). This includes words that might otherwise be
considered a constant or function call.</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span> <span class="i">FOO</span> <span class="cm">=></span> <span class="q">"something"</span><span class="sc">;</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span> = <span class="s">(</span> <span class="w">FOO</span> <span class="cm">=></span> <span class="n">23</span> <span class="s">)</span><span class="sc">;</span></pre>
<p>is equivalent to:</p>
<pre class="verbatim"> <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span> = <span class="s">(</span><span class="q">"FOO"</span><span class="cm">,</span> <span class="n">23</span><span class="s">)</span><span class="sc">;</span></pre>
<p>It is <i>NOT</i>:</p>
<pre class="verbatim"> <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span> = <span class="s">(</span><span class="q">"something"</span><span class="cm">,</span> <span class="n">23</span><span class="s">)</span><span class="sc">;</span></pre>
<p>If the argument on the left is not a word, it is first interpreted as
an expression, and then the string value of that is used.</p>
<p>The <code class="inline"><span class="cm">=></span></code>
operator is helpful in documenting the correspondence
between keys and values in hashes, and other paired elements in lists.</p>
<pre class="verbatim"> <span class="i">%hash</span> = <span class="s">(</span> <span class="i">$key</span> <span class="cm">=></span> <span class="i">$value</span> <span class="s">)</span><span class="sc">;</span>
<span class="i">login</span><span class="s">(</span> <span class="i">$username</span> <span class="cm">=></span> <span class="i">$password</span> <span class="s">)</span><span class="sc">;</span></pre>
<a name="List-Operators-(Rightward)"></a><h2>List Operators (Rightward)
</h2>
<p>On the right side of a list operator, it has very low precedence,
such that it controls all comma-separated expressions found there.
The only operators with lower precedence are the logical operators
"and", "or", and "not", which may be used to evaluate calls to list
operators without the need for extra parentheses:</p>
<pre class="verbatim"> <a class="l_k" href="functions/open.html">open</a> <span class="w">HANDLE</span><span class="cm">,</span> <span class="q">"filename"</span>
or <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't open: $!\n"</span><span class="sc">;</span></pre>
<p>See also discussion of list operators in <a href="#Terms-and-List-Operators-(Leftward)">"Terms and List Operators (Leftward)"</a>.</p>
<a name="Logical-Not"></a><h2>Logical Not
</h2>
<p>Unary "not" returns the logical negation of the expression to its right.
It's the equivalent of "!" except for the very low precedence.</p>
<a name="Logical-And"></a><h2>Logical And
</h2>
<p>Binary "and" returns the logical conjunction of the two surrounding
expressions. It's equivalent to && except for the very low
precedence. This means that it short-circuits: i.e., the right
expression is evaluated only if the left expression is true.</p>
<a name="Logical-or%2c-Defined-or%2c-and-Exclusive-Or"></a><h2>Logical or, Defined or, and Exclusive Or
</h2>
<p>Binary "or" returns the logical disjunction of the two surrounding
expressions. It's equivalent to || except for the very low precedence.
This makes it useful for control flow</p>
<pre class="verbatim"> <a class="l_k" href="functions/print.html">print</a> <span class="i">FH</span> <span class="i">$data</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't write to FH: $!"</span><span class="sc">;</span></pre>
<p>This means that it short-circuits: i.e., the right expression is evaluated
only if the left expression is false. Due to its precedence, you should
probably avoid using this for assignment, only for control flow.</p>
<pre class="verbatim"> <span class="i">$a</span> = <span class="i">$b</span> or <span class="i">$c</span><span class="sc">;</span> <span class="c"># bug: this is wrong</span>
<span class="s">(</span><span class="i">$a</span> = <span class="i">$b</span><span class="s">)</span> or <span class="i">$c</span><span class="sc">;</span> <span class="c"># really means this</span>
<span class="i">$a</span> = <span class="i">$b</span> || <span class="i">$c</span><span class="sc">;</span> <span class="c"># better written this way</span></pre>
<p>However, when it's a list-context assignment and you're trying to use
"||" for control flow, you probably need "or" so that the assignment
takes higher precedence.</p>
<pre class="verbatim"> <span class="i">@info</span> = <a class="l_k" href="functions/stat.html">stat</a><span class="s">(</span><span class="i">$file</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># oops, scalar sense of stat!</span>
<span class="i">@info</span> = <a class="l_k" href="functions/stat.html">stat</a><span class="s">(</span><span class="i">$file</span><span class="s">)</span> or <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span> <span class="c"># better, now @info gets its due</span></pre>
<p>Then again, you could always use parentheses.</p>
<p>Binary "xor" returns the exclusive-OR of the two surrounding expressions.
It cannot short circuit, of course.</p>
<a name="C-Operators-Missing-From-Perl"></a><h2>C Operators Missing From Perl
</h2>
<p>Here is what C has that Perl doesn't:</p>
<ul>
<li><a name="unary-%26"></a><b>unary &</b>
<p>Address-of operator. (But see the "\" operator for taking a reference.)</p>
</li>
<li><a name="unary-*"></a><b>unary *</b>
<p>Dereference-address operator. (Perl's prefix dereferencing
operators are typed: $, @, %, and &.)</p>
</li>
<li><a name="(TYPE)"></a><b>(TYPE)</b>
<p>Type-casting operator.</p>
</li>
</ul>
<a name="Quote-and-Quote-like-Operators"></a><h2>Quote and Quote-like Operators
</h2>
<p>While we usually think of quotes as literal values, in Perl they
function as operators, providing various kinds of interpolating and
pattern matching capabilities. Perl provides customary quote characters
for these behaviors, but also provides a way for you to choose your
quote character for any of them. In the following table, a <code class="inline"><span class="s">{</span><span class="s">}</span></code>
represents
any pair of delimiters you choose.</p>
<pre class="verbatim"> Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
<<EOF here-doc yes*</pre><pre class="verbatim"> * unless the delimiter is ''.</pre><p>Non-bracketing delimiters use the same character fore and aft, but the four
sorts of brackets (round, angle, square, curly) will all nest, which means
that</p>
<pre class="verbatim"> <span class="q">q{foo{bar}baz}</span></pre>
<p>is the same as</p>
<pre class="verbatim"> <span class="q">'foo{bar}baz'</span></pre>
<p>Note, however, that this does not always work for quoting Perl code:</p>
<pre class="verbatim"> $s = q{ if($a eq "}") ... }; # WRONG</pre><p>is a syntax error. The <code class="inline"><span class="w">Text::Balanced</span></code>
module (from CPAN, and
starting from Perl 5.8 part of the standard distribution) is able
to do this properly.</p>
<p>There can be whitespace between the operator and the quoting
characters, except when <code class="inline"><span class="c">#</span></code>
is being used as the quoting character.
<code class="inline"><a class="l_k" href="functions/q.html">q#foo#</a></code> is parsed as the string <code class="inline"><span class="w">foo</span></code>
, while <code class="inline">q #foo#</code> is the
operator <code class="inline"><a class="l_k" href="functions/q.html">q</a></code> followed by a comment. Its argument will be taken
from the next line. This allows you to write:</p>
<pre class="verbatim"> <span class="q">s {foo} # Replace foo</span>
<span class="q"> {bar}</span> <span class="c"># with bar.</span></pre>
<p>The following escape sequences are available in constructs that interpolate
and in transliterations.
</p>
<pre class="verbatim"> \t tab (HT, TAB)
\n newline (NL)
\r return (CR)
\f form feed (FF)
\b backspace (BS)
\a alarm (bell) (BEL)
\e escape (ESC)
\033 octal char (example: ESC)
\x1b hex char (example: ESC)
\x{263a} wide hex char (example: SMILEY)
\c[ control char (example: ESC)
\N{name} named Unicode character</pre><p>The character following <code class="inline">\<span class="w">c</span></code>
is mapped to some other character by
converting letters to upper case and then (on ASCII systems) by inverting
the 7th bit (0x40). The most interesting range is from '@' to '_'
(0x40 through 0x5F), resulting in a control character from 0x00
through 0x1F. A '?' maps to the DEL character. On EBCDIC systems only
'@', the letters, '[', '\', ']', '^', '_' and '?' will work, resulting
in 0x00 through 0x1F and 0x7F.</p>
<p><b>NOTE</b>: Unlike C and other languages, Perl has no \v escape sequence for
the vertical tab (VT - ASCII 11), but you may use <code class="inline">\<span class="w">ck</span></code>
or <code class="inline">\<span class="w">x0b</span></code>
.</p>
<p>The following escape sequences are available in constructs that interpolate
but not in transliterations.
</p>
<pre class="verbatim"> \<span class="w">l</span> <span class="w">lowercase</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">char</span>
\<span class="w">u</span> <span class="w">uppercase</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">char</span>
\<span class="w">L</span> <span class="w">lowercase</span> <span class="w">till</span> \<span class="w">E</span>
\<span class="w">U</span> <span class="w">uppercase</span> <span class="w">till</span> \<span class="w">E</span>
\<span class="w">E</span> <span class="w">end</span> case <span class="w">modification</span>
\<span class="w">Q</span> <span class="w">quote</span> <span class="w">non</span>-<span class="w">word</span> <span class="w">characters</span> <span class="w">till</span> \<span class="w">E</span></pre>
<p>If <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
is in effect, the case map used by <code class="inline">\<span class="w">l</span></code>
, <code class="inline">\<span class="w">L</span></code>
,
<code class="inline">\<span class="w">u</span></code>
and <code class="inline">\<span class="w">U</span></code>
is taken from the current locale. See <a href="perllocale.html">perllocale</a>.
If Unicode (for example, <code class="inline">\<span class="w">N</span><span class="s">{</span><span class="s">}</span></code>
or wide hex characters of 0x100 or
beyond) is being used, the case map used by <code class="inline">\<span class="w">l</span></code>
, <code class="inline">\<span class="w">L</span></code>
, <code class="inline">\<span class="w">u</span></code>
and
<code class="inline">\<span class="w">U</span></code>
is as defined by Unicode. For documentation of <code class="inline">\<span class="i">N</span><span class="s">{</span><span class="w">name</span><span class="s">}</span></code>
,
see <a href="charnames.html">charnames</a>.</p>
<p>All systems use the virtual <code class="inline"><span class="q">"\n"</span></code>
to represent a line terminator,
called a "newline". There is no such thing as an unvarying, physical
newline character. It is only an illusion that the operating system,
device drivers, C libraries, and Perl all conspire to preserve. Not all
systems read <code class="inline"><span class="q">"\r"</span></code>
as ASCII CR and <code class="inline"><span class="q">"\n"</span></code>
as ASCII LF. For example,
on a Mac, these are reversed, and on systems without line terminator,
printing <code class="inline"><span class="q">"\n"</span></code>
may emit no actual data. In general, use <code class="inline"><span class="q">"\n"</span></code>
when
you mean a "newline" for your system, but use the literal ASCII when you
need an exact character. For example, most networking protocols expect
and prefer a CR+LF (<code class="inline"><span class="q">"\015\012"</span></code>
or <code class="inline"><span class="q">"\cM\cJ"</span></code>
) for line terminators,
and although they often accept just <code class="inline"><span class="q">"\012"</span></code>
, they seldom tolerate just
<code class="inline"><span class="q">"\015"</span></code>
. If you get in the habit of using <code class="inline"><span class="q">"\n"</span></code>
for networking,
you may be burned some day.
</p>
<p>For constructs that do interpolate, variables beginning with "<code class="inline"><span class="i">$</span></code>
"
or "<code class="inline"><span class="i">@</span></code>
" are interpolated. Subscripted variables such as <code class="inline"><span class="i">$a</span>[<span class="n">3</span>]</code>
or
<code class="inline"><span class="i">$href</span>->{<span class="w">key</span>}[<span class="n">0</span>]</code>
are also interpolated, as are array and hash slices.
But method calls such as <code class="inline"><span class="i">$obj</span><span class="i">->meth</span></code>
are not.</p>
<p>Interpolating an array or slice interpolates the elements in order,
separated by the value of <code class="inline"><span class="i">$"</span></code>
, so is equivalent to interpolating
<code class="inline"><a class="l_k" href="functions/join.html">join</a> <span class="i">$"</span><span class="cm">,</span> <span class="i">@array</span></code>
. "Punctuation" arrays such as <code class="inline"><span class="i">@*</span></code>
are only
interpolated if the name is enclosed in braces <code class="inline">@{*}</code>, but special
arrays <code class="inline"><span class="i">@_</span></code>
, <code class="inline"><span class="i">@+</span></code>
, and <code class="inline"><span class="i">@-</span></code>
are interpolated, even without braces.</p>
<p>You cannot include a literal <code class="inline"><span class="i">$</span></code>
or <code class="inline"><span class="i">@</span></code>
within a <code class="inline">\<span class="w">Q</span></code>
sequence.
An unescaped <code class="inline"><span class="i">$</span></code>
or <code class="inline"><span class="i">@</span></code>
interpolates the corresponding variable,
while escaping will cause the literal string <code class="inline">\<span class="i">$</span></code>
to be inserted.
You'll need to write something like <code class="inline"><a class="l_k" href="functions/m.html">m/\Quser\E\@\Qhost/</a></code>.</p>
<p>Patterns are subject to an additional level of interpretation as a
regular expression. This is done as a second pass, after variables are
interpolated, so that regular expressions may be incorporated into the
pattern from the variables. If this is not what you want, use <code class="inline">\<span class="w">Q</span></code>
to
interpolate a variable literally.</p>
<p>Apart from the behavior described above, Perl does not expand
multiple levels of interpolation. In particular, contrary to the
expectations of shell programmers, back-quotes do <i>NOT</i> interpolate
within double quotes, nor do single quotes impede evaluation of
variables when used within double quotes.</p>
<a name="Regexp-Quote-Like-Operators"></a><h2>Regexp Quote-Like Operators
</h2>
<p>Here are the quote-like operators that apply to pattern
matching and related activities.</p>
<ul>
<li><a name="qr%2fSTRING%2fmsixpo"></a><b>qr/STRING/msixpo
</b>
<p>This operator quotes (and possibly compiles) its <i>STRING</i> as a regular
expression. <i>STRING</i> is interpolated the same way as <i>PATTERN</i>
in <code class="inline"><a class="l_k" href="functions/m.html">m/PATTERN/</a></code>. If "'" is used as the delimiter, no interpolation
is done. Returns a Perl value which may be used instead of the
corresponding <code class="inline"><span class="q">/STRING/msixpo</span></code>
expression. The returned value is a
normalized version of the original pattern. It magically differs from
a string containing the same characters: <code class="inline"><a class="l_k" href="functions/ref.html">ref(qr/x/)</a></code> returns "Regexp",
even though dereferencing the result returns undef.</p>
<p>For example,</p>
<pre class="verbatim"> <span class="i">$rex</span> = <span class="q">qr/my.STRING/is</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$rex</span><span class="sc">;</span> <span class="c"># prints (?si-xm:my.STRING)</span>
<span class="q">s/$rex/foo/</span><span class="sc">;</span></pre>
<p>is equivalent to</p>
<pre class="verbatim"> <span class="q">s/my.STRING/foo/is</span><span class="sc">;</span></pre>
<p>The result may be used as a subpattern in a match:</p>
<pre class="verbatim"> <span class="i">$re</span> = <span class="q">qr/$pattern/</span><span class="sc">;</span>
<span class="i">$string</span> =~ <span class="q">/foo${re}bar/</span><span class="sc">;</span> <span class="c"># can be interpolated in other patterns</span>
<span class="i">$string</span> =~ <span class="i">$re</span><span class="sc">;</span> <span class="c"># or used standalone</span>
<span class="i">$string</span> =~ <span class="q">/$re/</span><span class="sc">;</span> <span class="c"># or this way</span></pre>
<p>Since Perl may compile the pattern at the moment of execution of qr()
operator, using qr() may have speed advantages in some situations,
notably if the result of qr() is used standalone:</p>
<pre class="verbatim"><a name="match"></a> sub <span class="m">match</span> <span class="s">{</span>
<a class="l_k" href="functions/my.html">my</a> <span class="i">$patterns</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span>
<a class="l_k" href="functions/my.html">my</a> <span class="i">@compiled</span> = <a class="l_k" href="functions/map.html">map</a> <span class="q">qr/$_/i</span><span class="cm">,</span> <span class="i">@$patterns</span><span class="sc">;</span>
<a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span>
<a class="l_k" href="functions/my.html">my</a> <span class="i">$success</span> = <span class="n">0</span><span class="sc">;</span>
foreach <a class="l_k" href="functions/my.html">my</a> <span class="i">$pat</span> <span class="s">(</span><span class="i">@compiled</span><span class="s">)</span> <span class="s">{</span>
<span class="i">$success</span> = <span class="n">1</span><span class="cm">,</span> <a class="l_k" href="functions/last.html">last</a> if <span class="q">/$pat/</span><span class="sc">;</span>
<span class="s">}</span>
<span class="i">$success</span><span class="sc">;</span>
<span class="s">}</span> <span class="i">@_</span><span class="sc">;</span>
<span class="s">}</span></pre>
<p>Precompilation of the pattern into an internal representation at
the moment of qr() avoids a need to recompile the pattern every
time a match <code class="inline"><span class="q">/$pat/</span></code>
is attempted. (Perl has many other internal
optimizations, but none would be triggered in the above example if
we did not use qr() operator.)</p>
<p>Options are:</p>
<pre class="verbatim"> m Treat string as multiple lines.
s Treat string as single line. (Make . match a newline)
i Do case-insensitive pattern matching.
x Use extended regular expressions.
p When matching preserve a copy of the matched string so
that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined.
o Compile pattern only once.</pre><p>If a precompiled pattern is embedded in a larger pattern then the effect
of 'msixp' will be propagated appropriately. The effect of the 'o'
modifier has is not propagated, being restricted to those patterns
explicitly using it.</p>
<p>See <a href="perlre.html">perlre</a> for additional information on valid syntax for STRING, and
for a detailed look at the semantics of regular expressions.</p>
</li>
<li><a name="m%2fPATTERN%2fmsixpogc"></a><b>m/PATTERN/msixpogc
</b>
</li>
<li><a name="%2fPATTERN%2fmsixpogc"></a><b>/PATTERN/msixpogc</b>
<p>Searches a string for a pattern match, and in scalar context returns
true if it succeeds, false if it fails. If no string is specified
via the <code class="inline">=~</code>
or <code class="inline">!~</code>
operator, the $_ string is searched. (The
string specified with <code class="inline">=~</code>
need not be an lvalue--it may be the
result of an expression evaluation, but remember the <code class="inline">=~</code>
binds
rather tightly.) See also <a href="perlre.html">perlre</a>. See <a href="perllocale.html">perllocale</a> for
discussion of additional considerations that apply when <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
is in effect.</p>
<p>Options are as described in <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code>; in addition, the following match
process modifiers are available:</p>
<pre class="verbatim"> g Match globally, i.e., find all occurrences.
c Do not reset search position on a failed match when /g is in effect.</pre><p>If "/" is the delimiter then the initial <code class="inline"><a class="l_k" href="functions/m.html">m</a></code> is optional. With the <code class="inline"><a class="l_k" href="functions/m.html">m</a></code>
you can use any pair of non-alphanumeric, non-whitespace characters
as delimiters. This is particularly useful for matching path names
that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is
the delimiter, then the match-only-once rule of <code class="inline"><span class="q">?PATTERN?</span></code>
applies.
If "'" is the delimiter, no interpolation is performed on the PATTERN.</p>
<p>PATTERN may contain variables, which will be interpolated (and the
pattern recompiled) every time the pattern search is evaluated, except
for when the delimiter is a single quote. (Note that <code class="inline"><span class="i">$(</span></code>
, <code class="inline"><span class="i">$)</span></code>
, and
<code class="inline"><span class="i">$|</span></code>
are not interpolated because they look like end-of-string tests.)
If you want such a pattern to be compiled only once, add a <code class="inline">/o</code> after
the trailing delimiter. This avoids expensive run-time recompilations,
and is useful when the value you are interpolating won't change over
the life of the script. However, mentioning <code class="inline">/o</code> constitutes a promise
that you won't change the variables in the pattern. If you change them,
Perl won't even notice. See also <a href="#qr%2fSTRING%2fmsixpo">"qr/STRING/msixpo"</a>.</p>
<p>If the PATTERN evaluates to the empty string, the last
<i>successfully</i> matched regular expression is used instead. In this
case, only the <code class="inline"><span class="w">g</span></code>
and <code class="inline"><span class="w">c</span></code>
flags on the empty pattern is honoured -
the other flags are taken from the original pattern. If no match has
previously succeeded, this will (silently) act instead as a genuine
empty pattern (which will always match).</p>
<p>Note that it's possible to confuse Perl into thinking <code class="inline"><span class="q">//</span></code>
(the empty
regex) is really <code class="inline"><span class="q">//</span></code>
(the defined-or operator). Perl is usually pretty
good about this, but some pathological cases might trigger this, such as
<code class="inline">$a///</code> (is that <code class="inline"><span class="s">(</span><span class="i">$a</span><span class="s">)</span> / <span class="s">(</span><span class="q">//</span><span class="s">)</span></code>
or <code class="inline">$a // /</code>?) and <code class="inline"><a class="l_k" href="functions/print.html">print</a> <span class="i">$fh</span> <span class="q">//</span></code>
(<code class="inline">print $fh(//</code> or <code class="inline">print($fh //</code>?). In all of these examples, Perl
will assume you meant defined-or. If you meant the empty regex, just
use parentheses or spaces to disambiguate, or even prefix the empty
regex with an <code class="inline"><a class="l_k" href="functions/m.html">m</a></code> (so <code class="inline"><span class="q">//</span></code>
becomes <code class="inline"><a class="l_k" href="functions/m.html">m//</a></code>).</p>
<p>If the <code class="inline">/g</code> option is not used, <code class="inline"><a class="l_k" href="functions/m.html">m//</a></code> in list context returns a
list consisting of the subexpressions matched by the parentheses in the
pattern, i.e., (<code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, <code class="inline"><span class="i">$3</span></code>
...). (Note that here <code class="inline"><span class="i">$1</span></code>
etc. are
also set, and that this differs from Perl 4's behavior.) When there are
no parentheses in the pattern, the return value is the list <code class="inline"><span class="s">(</span><span class="n">1</span><span class="s">)</span></code>
for
success. With or without parentheses, an empty list is returned upon
failure.</p>
<p>Examples:</p>
<pre class="verbatim"> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">TTY</span><span class="cm">,</span> <span class="q">'/dev/tty'</span><span class="s">)</span><span class="sc">;</span>
<span class="q"><TTY></span> =~ <span class="q">/^y/i</span> && <span class="i">foo</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="c"># do foo if desired</span></pre>
<pre class="verbatim"> if <span class="s">(</span><span class="q">/Version: *([0-9.]*)/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$version</span> = <span class="i">$1</span><span class="sc">;</span> <span class="s">}</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/next.html">next</a> if <span class="q">m#^/usr/spool/uucp#</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># poor man's grep</span>
<span class="i">$arg</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span>
while <span class="s">(</span><><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/print.html">print</a> if <span class="q">/$arg/o</span><span class="sc">;</span> <span class="c"># compile only once</span>
<span class="s">}</span></pre>
<pre class="verbatim"> if <span class="s">(</span><span class="s">(</span><span class="i">$F1</span><span class="cm">,</span> <span class="i">$F2</span><span class="cm">,</span> <span class="i">$Etc</span><span class="s">)</span> = <span class="s">(</span><span class="i">$foo</span> =~ <span class="q">/^(\S+)\s+(\S+)\s*(.*)/</span><span class="s">)</span><span class="s">)</span></pre>
<p>This last example splits $foo into the first two words and the
remainder of the line, and assigns those three fields to $F1, $F2, and
$Etc. The conditional is true if any variables were assigned, i.e., if
the pattern matched.</p>
<p>The <code class="inline">/g</code> modifier specifies global pattern matching--that is,
matching as many times as possible within the string. How it behaves
depends on the context. In list context, it returns a list of the
substrings matched by any capturing parentheses in the regular
expression. If there are no parentheses, it returns a list of all
the matched strings, as if there were parentheses around the whole
pattern.</p>
<p>In scalar context, each execution of <code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code> finds the next match,
returning true if it matches, and false if there is no further match.
The position after the last match can be read or set using the pos()
function; see <a href="functions/pos.html">pos</a>. A failed match normally resets the
search position to the beginning of the string, but you can avoid that
by adding the <code class="inline">/c</code> modifier (e.g. <code class="inline"><a class="l_k" href="functions/m.html">m//gc</a></code>). Modifying the target
string also resets the search position.</p>
<p>You can intermix <code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code> matches with <code class="inline"><a class="l_k" href="functions/m.html">m/\G.../g</a></code>, where <code class="inline">\<span class="w">G</span></code>
is a
zero-width assertion that matches the exact position where the previous
<code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code>, if any, left off. Without the <code class="inline">/g</code> modifier, the <code class="inline">\<span class="w">G</span></code>
assertion
still anchors at pos(), but the match is of course only attempted once.
Using <code class="inline">\<span class="w">G</span></code>
without <code class="inline">/g</code> on a target string that has not previously had a
<code class="inline">/g</code> match applied to it is the same as using the <code class="inline">\<span class="w">A</span></code>
assertion to match
the beginning of the string. Note also that, currently, <code class="inline">\<span class="w">G</span></code>
is only
properly supported when anchored at the very beginning of the pattern.</p>
<p>Examples:</p>
<pre class="verbatim"> <span class="c"># list context</span>
<span class="s">(</span><span class="i">$one</span><span class="cm">,</span><span class="i">$five</span><span class="cm">,</span><span class="i">$fifteen</span><span class="s">)</span> = <span class="s">(</span><span class="q">`uptime`</span> =~ <span class="q">/(\d+\.\d+)/g</span><span class="s">)</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># scalar context</span>
<span class="i">$/</span> = <span class="q">""</span><span class="sc">;</span>
while <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$paragraph</span> = <><span class="s">)</span><span class="s">)</span> <span class="s">{</span>
while <span class="s">(</span><span class="i">$paragraph</span> =~ <span class="q">/[a-z]['")]*[.!?]+['")]*\s/g</span><span class="s">)</span> <span class="s">{</span>
<span class="i">$sentences</span>++<span class="sc">;</span>
<span class="s">}</span>
<span class="s">}</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"$sentences\n"</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># using m//gc with \G</span>
<span class="i">$_</span> = <span class="q">"ppooqppqq"</span><span class="sc">;</span>
while <span class="s">(</span><span class="i">$i</span>++ < <span class="n">2</span><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"1: '"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span> while <span class="q">/(o)/gc</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"', pos="</span><span class="cm">,</span> <a class="l_k" href="functions/pos.html">pos</a><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"2: '"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span> if <span class="q">/\G(q)/gc</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"', pos="</span><span class="cm">,</span> <a class="l_k" href="functions/pos.html">pos</a><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"3: '"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span> while <span class="q">/(p)/gc</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"', pos="</span><span class="cm">,</span> <a class="l_k" href="functions/pos.html">pos</a><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span>
<span class="s">}</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"Final: '$1', pos="</span><span class="cm">,</span><a class="l_k" href="functions/pos.html">pos</a><span class="cm">,</span><span class="q">"\n"</span> if <span class="q">/\G(.)/</span><span class="sc">;</span></pre>
<p>The last example should print:</p>
<pre class="verbatim"> 1: 'oo', pos=4
2: 'q', pos=5
3: 'pp', pos=7
1: '', pos=7
2: 'q', pos=8
3: '', pos=8
Final: 'q', pos=8</pre><p>Notice that the final match matched <code class="inline"><a class="l_k" href="functions/q.html">q</a></code> instead of <code class="inline"><span class="w">p</span></code>
, which a match
without the <code class="inline">\<span class="w">G</span></code>
anchor would have done. Also note that the final match
did not update <code class="inline"><a class="l_k" href="functions/pos.html">pos</a></code> -- <code class="inline"><a class="l_k" href="functions/pos.html">pos</a></code> is only updated on a <code class="inline">/g</code> match. If the
final match did indeed match <code class="inline"><span class="w">p</span></code>
, it's a good bet that you're running an
older (pre-5.6.0) Perl.</p>
<p>A useful idiom for <code class="inline"><span class="w">lex</span></code>
-like scanners is <code class="inline"><span class="q">/\G.../gc</span></code>
. You can
combine several regexps like this to process a string part-by-part,
doing different actions depending on which regexp matched. Each
regexp tries to match where the previous one leaves off.</p>
<pre class="verbatim"> $_ = <<'EOL';
$url = URI::URL->new( "http://www/" ); die if $url eq "xXx";
EOL
LOOP:
{
print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
print(" lowercase"), redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
print(" UPPERCASE"), redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
print(" Capitalized"), redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
print(" MiXeD"), redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
print(" alphanumeric"), redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
print(" line-noise"), redo LOOP if /\G[^A-Za-z0-9]+/gc;
print ". That's all!\n";
}</pre><p>Here is the output (split into several lines):</p>
<pre class="verbatim"> <span class="w">line</span>-<span class="w">noise</span> <span class="w">lowercase</span> <span class="w">line</span>-<span class="w">noise</span> <span class="w">lowercase</span> <span class="w">UPPERCASE</span> <span class="w">line</span>-<span class="w">noise</span>
<span class="w">UPPERCASE</span> <span class="w">line</span>-<span class="w">noise</span> <span class="w">lowercase</span> <span class="w">line</span>-<span class="w">noise</span> <span class="w">lowercase</span> <span class="w">line</span>-<span class="w">noise</span>
<span class="w">lowercase</span> <span class="w">lowercase</span> <span class="w">line</span>-<span class="w">noise</span> <span class="w">lowercase</span> <span class="w">lowercase</span> <span class="w">line</span>-<span class="w">noise</span>
<span class="w">MiXeD</span> <span class="w">line</span>-<span class="w">noise</span>. <span class="w">That's</span> <span class="w">all</span>!</pre>
</li>
<li><a name="%3fPATTERN%3f"></a><b>?PATTERN?
</b>
<p>This is just like the <code class="inline"><span class="q">/pattern/</span></code>
search, except that it matches only
once between calls to the reset() operator. This is a useful
optimization when you want to see only the first occurrence of
something in each file of a set of files, for instance. Only <code class="inline"><span class="q">??</span></code>
patterns local to the current package are reset.</p>
<pre class="verbatim"> while <span class="s">(</span><><span class="s">)</span> <span class="s">{</span>
if <span class="s">(</span><span class="q">?^$?</span><span class="s">)</span> <span class="s">{</span>
<span class="c"># blank line between header and body</span>
<span class="s">}</span>
<span class="s">}</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">{</span>
<a class="l_k" href="functions/reset.html">reset</a> if <a class="l_k" href="functions/eof.html">eof</a><span class="sc">;</span> <span class="c"># clear ?? status for next file</span>
<span class="s">}</span></pre>
<p>This usage is vaguely deprecated, which means it just might possibly
be removed in some distant future version of Perl, perhaps somewhere
around the year 2168.</p>
</li>
<li><a name="s%2fPATTERN%2fREPLACEMENT%2fmsixpogce"></a><b>s/PATTERN/REPLACEMENT/msixpogce
</b>
<p>Searches a string for a pattern, and if found, replaces that pattern
with the replacement text and returns the number of substitutions
made. Otherwise it returns false (specifically, the empty string).</p>
<p>If no string is specified via the <code class="inline">=~</code>
or <code class="inline">!~</code>
operator, the <code class="inline"><span class="i">$_</span></code>
variable is searched and modified. (The string specified with <code class="inline">=~</code>
must
be scalar variable, an array element, a hash element, or an assignment
to one of those, i.e., an lvalue.)</p>
<p>If the delimiter chosen is a single quote, no interpolation is
done on either the PATTERN or the REPLACEMENT. Otherwise, if the
PATTERN contains a $ that looks like a variable rather than an
end-of-string test, the variable will be interpolated into the pattern
at run-time. If you want the pattern compiled only once the first time
the variable is interpolated, use the <code class="inline">/o</code> option. If the pattern
evaluates to the empty string, the last successfully executed regular
expression is used instead. See <a href="perlre.html">perlre</a> for further explanation on these.
See <a href="perllocale.html">perllocale</a> for discussion of additional considerations that apply
when <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
is in effect.</p>
<p>Options are as with m// with the addition of the following replacement
specific options:</p>
<pre class="verbatim"> <span class="w">e</span> <span class="w">Evaluate</span> <span class="w">the</span> <span class="w">right</span> <span class="w">side</span> <span class="w">as</span> <span class="w">an</span> <span class="w">expression</span>.
<span class="w">ee</span> <span class="w">Evaluate</span> <span class="w">the</span> <span class="w">right</span> <span class="w">side</span> <span class="w">as</span> <span class="w">a</span> <span class="w">string</span> <span class="w">then</span> <a class="l_k" href="functions/eval.html">eval</a> <span class="w">the</span> <span class="w">result</span></pre>
<p>Any non-alphanumeric, non-whitespace delimiter may replace the
slashes. If single quotes are used, no interpretation is done on the
replacement string (the <code class="inline">/e</code> modifier overrides this, however). Unlike
Perl 4, Perl 5 treats backticks as normal delimiters; the replacement
text is not evaluated as a command. If the
PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own
pair of quotes, which may or may not be bracketing quotes, e.g.,
<code class="inline"><a class="l_k" href="functions/s.html">s(foo)(bar)</a></code> or <code class="inline"><a class="l_k" href="functions/s.html">s<foo>/bar/</a></code>. A <code class="inline">/e</code> will cause the
replacement portion to be treated as a full-fledged Perl expression
and evaluated right then and there. It is, however, syntax checked at
compile-time. A second <code class="inline"><span class="w">e</span></code>
modifier will cause the replacement portion
to be <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>ed before being run as a Perl expression.</p>
<p>Examples:</p>
<pre class="verbatim"> <span class="q">s/\bgreen\b/mauve/g</span><span class="sc">;</span> <span class="c"># don't change wintergreen</span></pre>
<pre class="verbatim"> <span class="i">$path</span> =~ <span class="q">s|/usr/bin|/usr/local/bin|</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="q">s/Login: $foo/Login: $bar/</span><span class="sc">;</span> <span class="c"># run-time pattern</span></pre>
<pre class="verbatim"> <span class="s">(</span><span class="i">$foo</span> = <span class="i">$bar</span><span class="s">)</span> =~ <span class="q">s/this/that/</span><span class="sc">;</span> <span class="c"># copy first, then change</span></pre>
<pre class="verbatim"> <span class="i">$count</span> = <span class="s">(</span><span class="i">$paragraph</span> =~ <span class="q">s/Mister\b/Mr./g</span><span class="s">)</span><span class="sc">;</span> <span class="c"># get change-count</span></pre>
<pre class="verbatim"> <span class="i">$_</span> = <span class="q">'abc123xyz'</span><span class="sc">;</span>
<span class="q">s/\d+/$&*2/e</span><span class="sc">;</span> <span class="c"># yields 'abc246xyz'</span>
<span class="q">s/\d+/sprintf("%5d",$&)/e</span><span class="sc">;</span> <span class="c"># yields 'abc 246xyz'</span>
<span class="q">s/\w/$& x 2/eg</span><span class="sc">;</span> <span class="c"># yields 'aabbcc 224466xxyyzz'</span></pre>
<pre class="verbatim"> <span class="q">s/%(.)/$percent{$1}/g</span><span class="sc">;</span> <span class="c"># change percent escapes; no /e</span>
<span class="q">s/%(.)/$percent{$1} || $&/ge</span><span class="sc">;</span> <span class="c"># expr now, so /e</span>
<span class="q">s/^=(\w+)/pod($1)/ge</span><span class="sc">;</span> <span class="c"># use function call</span></pre>
<pre class="verbatim"> <span class="c"># expand variables in $_, but dynamics only, using</span>
<span class="c"># symbolic dereferencing</span>
<span class="q">s/\$(\w+)/${$1}/g</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># Add one to the value of any numbers in the string</span>
<span class="q">s/(\d+)/1 + $1/eg</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># This will expand any embedded scalar variable</span>
<span class="c"># (including lexicals) in $_ : First $1 is interpolated</span>
<span class="c"># to the variable name, and then evaluated</span>
<span class="q">s/(\$\w+)/$1/eeg</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># Delete (most) C comments.</span>
<span class="i">$program</span> =~ <span class="q">s {</span>
<span class="q"> /\* # Match the opening delimiter.</span>
<span class="q"> .*? # Match a minimal number of characters.</span>
<span class="q"> \*/ # Match the closing delimiter.</span>
<span class="q"> } []gsx</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="q">s/^\s*(.*?)\s*$/$1/</span><span class="sc">;</span> <span class="c"># trim whitespace in $_, expensively</span></pre>
<pre class="verbatim"> for <span class="s">(</span><span class="i">$variable</span><span class="s">)</span> <span class="s">{</span> <span class="c"># trim whitespace in $variable, cheap</span>
<span class="q">s/^\s+//</span><span class="sc">;</span>
<span class="q">s/\s+$//</span><span class="sc">;</span>
<span class="s">}</span></pre>
<pre class="verbatim"> <span class="q">s/([^ ]*) *([^ ]*)/$2 $1/</span><span class="sc">;</span> <span class="c"># reverse 1st two fields</span></pre>
<p>Note the use of $ instead of \ in the last example. Unlike
<b>sed</b>, we use the \<<i>digit</i>> form in only the left hand side.
Anywhere else it's $<<i>digit</i>>.</p>
<p>Occasionally, you can't use just a <code class="inline">/g</code> to get all the changes
to occur that you might want. Here are two common cases:</p>
<pre class="verbatim"> <span class="c"># put commas in the right places in an integer</span>
<span class="n">1</span> while <span class="q">s/(\d)(\d\d\d)(?!\d)/$1,$2/g</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># expand tabs to 8-column spacing</span>
<span class="n">1</span> while <span class="q">s/\t+/' ' x (length($&)*8 - length($`)%8)/e</span><span class="sc">;</span></pre>
</li>
</ul>
<a name="Quote-Like-Operators"></a><h2>Quote-Like Operators
</h2>
<ul>
<li><a name="q%2fSTRING%2f"></a><b>q/STRING/
</b>
</li>
<li><a name="'STRING'"></a><b>'STRING'</b>
<p>A single-quoted, literal string. A backslash represents a backslash
unless followed by the delimiter or another backslash, in which case
the delimiter or backslash is interpolated.</p>
<pre class="verbatim"> <span class="i">$foo</span> = <span class="q">q!I said, "You said, 'She said it.'"!</span><span class="sc">;</span>
<span class="i">$bar</span> = <span class="q">q('This is it.')</span><span class="sc">;</span>
<span class="i">$baz</span> = <span class="q">'\n'</span><span class="sc">;</span> <span class="c"># a two-character string</span></pre>
</li>
<li><a name="qq%2fSTRING%2f"></a><b>qq/STRING/
</b>
</li>
<li><a name="%22STRING%22"></a><b>"STRING"</b>
<p>A double-quoted, interpolated string.</p>
<pre class="verbatim"> <span class="i">$_</span> .= <span class="q">qq</span>
<span class="q"> (*** The previous line contains the naughty word "$1".\n)</span>
if <span class="q">/\b(tcl|java|python)\b/i</span><span class="sc">;</span> <span class="c"># :-)</span>
<span class="i">$baz</span> = <span class="q">"\n"</span><span class="sc">;</span> <span class="c"># a one-character string</span></pre>
</li>
<li><a name="qx%2fSTRING%2f"></a><b>qx/STRING/
</b>
</li>
<li><a name="%60STRING%60"></a><b>`STRING`</b>
<p>A string which is (possibly) interpolated and then executed as a
system command with <code class="inline">/bin/sh</code> or its equivalent. Shell wildcards,
pipes, and redirections will be honored. The collected standard
output of the command is returned; standard error is unaffected. In
scalar context, it comes back as a single (potentially multi-line)
string, or undef if the command failed. In list context, returns a
list of lines (however you've defined lines with $/ or
$INPUT_RECORD_SEPARATOR), or an empty list if the command failed.</p>
<p>Because backticks do not affect standard error, use shell file descriptor
syntax (assuming the shell supports this) if you care to address this.
To capture a command's STDERR and STDOUT together:</p>
<pre class="verbatim"> <span class="i">$output</span> = <span class="q">`cmd 2>&1`</span><span class="sc">;</span></pre>
<p>To capture a command's STDOUT but discard its STDERR:</p>
<pre class="verbatim"> <span class="i">$output</span> = <span class="q">`cmd 2>/dev/null`</span><span class="sc">;</span></pre>
<p>To capture a command's STDERR but discard its STDOUT (ordering is
important here):</p>
<pre class="verbatim"> <span class="i">$output</span> = <span class="q">`cmd 2>&1 1>/dev/null`</span><span class="sc">;</span></pre>
<p>To exchange a command's STDOUT and STDERR in order to capture the STDERR
but leave its STDOUT to come out the old STDERR:</p>
<pre class="verbatim"> <span class="i">$output</span> = <span class="q">`cmd 3>&1 1>&2 2>&3 3>&-`</span><span class="sc">;</span></pre>
<p>To read both a command's STDOUT and its STDERR separately, it's easiest
to redirect them separately to files, and then read from those files
when the program is done:</p>
<pre class="verbatim"> <a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">"program args 1>program.stdout 2>program.stderr"</span><span class="s">)</span><span class="sc">;</span></pre>
<p>The STDIN filehandle used by the command is inherited from Perl's STDIN.
For example:</p>
<pre class="verbatim"> <a class="l_k" href="functions/open.html">open</a> <span class="w">BLAM</span><span class="cm">,</span> <span class="q">"blam"</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't open: $!"</span><span class="sc">;</span>
<a class="l_k" href="functions/open.html">open</a> <span class="w">STDIN</span><span class="cm">,</span> <span class="q">"<&BLAM"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">`sort`</span><span class="sc">;</span></pre>
<p>will print the sorted contents of the file "blam".</p>
<p>Using single-quote as a delimiter protects the command from Perl's
double-quote interpolation, passing it on to the shell instead:</p>
<pre class="verbatim"> <span class="i">$perl_info</span> = <span class="q">qx(ps $$)</span><span class="sc">;</span> <span class="c"># that's Perl's $$</span>
<span class="i">$shell_info</span> = <span class="q">qx'ps $$'</span><span class="sc">;</span> <span class="c"># that's the new shell's $$</span></pre>
<p>How that string gets evaluated is entirely subject to the command
interpreter on your system. On most platforms, you will have to protect
shell metacharacters if you want them treated literally. This is in
practice difficult to do, as it's unclear how to escape which characters.
See <a href="perlsec.html">perlsec</a> for a clean and safe example of a manual fork() and exec()
to emulate backticks safely.</p>
<p>On some platforms (notably DOS-like ones), the shell may not be
capable of dealing with multiline commands, so putting newlines in
the string may not get you what you want. You may be able to evaluate
multiple commands in a single line by separating them with the command
separator character, if your shell supports that (e.g. <code class="inline"><span class="sc">;</span></code>
on many Unix
shells; <code class="inline"><span class="i">&</span></code>
on the Windows NT <code class="inline"><span class="w">cmd</span></code>
shell).</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before starting the child process, but this may not be supported
on some platforms (see <a href="perlport.html">perlport</a>). To be safe, you may need to set
<code class="inline"><span class="i">$|</span></code>
($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
method of
<code class="inline"><span class="w">IO::Handle</span></code>
on any open handles.</p>
<p>Beware that some command shells may place restrictions on the length
of the command line. You must ensure your strings don't exceed this
limit after any necessary interpolations. See the platform-specific
release notes for more details about your particular environment.</p>
<p>Using this operator can lead to programs that are difficult to port,
because the shell commands called vary between systems, and may in
fact not be present at all. As one example, the <code class="inline"><span class="w">type</span></code>
command under
the POSIX shell is very different from the <code class="inline"><span class="w">type</span></code>
command under DOS.
That doesn't mean you should go out of your way to avoid backticks
when they're the right way to get something done. Perl was made to be
a glue language, and one of the things it glues together is commands.
Just understand what you're getting yourself into.</p>
<p>See <a href="#I%2fO-Operators">"I/O Operators"</a> for more discussion.</p>
</li>
<li><a name="qw%2fSTRING%2f"></a><b>qw/STRING/
</b>
<p>Evaluates to a list of the words extracted out of STRING, using embedded
whitespace as the word delimiters. It can be understood as being roughly
equivalent to:</p>
<pre class="verbatim"> <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">' '</span><span class="cm">,</span> <span class="q">q/STRING/</span><span class="s">)</span><span class="sc">;</span></pre>
<p>the differences being that it generates a real list at compile time, and
in scalar context it returns the last element in the list. So
this expression:</p>
<pre class="verbatim"> <span class="q">qw(foo bar baz)</span></pre>
<p>is semantically equivalent to the list:</p>
<pre class="verbatim"> <span class="q">'foo'</span><span class="cm">,</span> <span class="q">'bar'</span><span class="cm">,</span> <span class="q">'baz'</span></pre>
<p>Some frequently seen examples:</p>
<pre class="verbatim"> use POSIX qw( setlocale localeconv )
@EXPORT = qw( foo bar baz );</pre><p>A common mistake is to try to separate the words with comma or to
put comments into a multi-line <code class="inline"><a class="l_k" href="functions/qw.html">qw</a></code>-string. For this reason, the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
pragma and the <b>-w</b> switch (that is, the <code class="inline"><span class="i">$^W</span></code>
variable)
produces warnings if the STRING contains the "," or the "#" character.</p>
</li>
<li><a name="tr%2fSEARCHLIST%2fREPLACEMENTLIST%2fcds"></a><b>tr/SEARCHLIST/REPLACEMENTLIST/cds
</b>
</li>
<li><a name="y%2fSEARCHLIST%2fREPLACEMENTLIST%2fcds"></a><b>y/SEARCHLIST/REPLACEMENTLIST/cds</b>
<p>Transliterates all occurrences of the characters found in the search list
with the corresponding character in the replacement list. It returns
the number of characters replaced or deleted. If no string is
specified via the =~ or !~ operator, the $_ string is transliterated. (The
string specified with =~ must be a scalar variable, an array element, a
hash element, or an assignment to one of those, i.e., an lvalue.)</p>
<p>A character range may be specified with a hyphen, so <code class="inline"><a class="l_k" href="functions/tr.html">tr/A-J/0-9/</a></code>
does the same replacement as <code class="inline"><a class="l_k" href="functions/tr.html">tr/ACEGIBDFHJ/0246813579/</a></code>.
For <b>sed</b> devotees, <code class="inline"><a class="l_k" href="functions/y.html">y</a></code> is provided as a synonym for <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code>. If the
SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has
its own pair of quotes, which may or may not be bracketing quotes,
e.g., <code class="inline"><a class="l_k" href="functions/tr.html">tr[A-Z][a-z]</a></code> or <code class="inline"><a class="l_k" href="functions/tr.html">tr(+\-*/)/ABCD/</a></code>.</p>
<p>Note that <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> does <b>not</b> do regular expression character classes
such as <code class="inline">\<span class="w">d</span></code>
or <code class="inline">[:lower:]</code>. The <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> operator is not equivalent to
the tr(1) utility. If you want to map strings between lower/upper
cases, see <a href="functions/lc.html">lc</a> and <a href="functions/uc.html">uc</a>, and in general consider
using the <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> operator if you need regular expressions.</p>
<p>Note also that the whole range idea is rather unportable between
character sets--and even within character sets they may cause results
you probably didn't expect. A sound principle is to use only ranges
that begin from and end at either alphabets of equal case (a-e, A-E),
or digits (0-4). Anything else is unsafe. If in doubt, spell out the
character sets in full.</p>
<p>Options:</p>
<pre class="verbatim"> c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.</pre><p>If the <code class="inline">/c</code> modifier is specified, the SEARCHLIST character set
is complemented. If the <code class="inline">/d</code> modifier is specified, any characters
specified by SEARCHLIST not found in REPLACEMENTLIST are deleted.
(Note that this is slightly more flexible than the behavior of some
<b>tr</b> programs, which delete anything they find in the SEARCHLIST,
period.) If the <code class="inline">/s</code> modifier is specified, sequences of characters
that were transliterated to the same character are squashed down
to a single instance of the character.</p>
<p>If the <code class="inline">/d</code> modifier is used, the REPLACEMENTLIST is always interpreted
exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter
than the SEARCHLIST, the final character is replicated till it is long
enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.
This latter is useful for counting characters in a class or for
squashing character sequences in a class.</p>
<p>Examples:</p>
<pre class="verbatim"> <span class="i">$ARGV</span>[<span class="n">1</span>] =~ <span class="q">tr/A-Z/a-z/</span><span class="sc">;</span> <span class="c"># canonicalize to lower case</span></pre>
<pre class="verbatim"> <span class="i">$cnt</span> = <span class="q">tr/*/*/</span><span class="sc">;</span> <span class="c"># count the stars in $_</span></pre>
<pre class="verbatim"> <span class="i">$cnt</span> = <span class="i">$sky</span> =~ <span class="q">tr/*/*/</span><span class="sc">;</span> <span class="c"># count the stars in $sky</span></pre>
<pre class="verbatim"> <span class="i">$cnt</span> = <span class="q">tr/0-9//</span><span class="sc">;</span> <span class="c"># count the digits in $_</span></pre>
<pre class="verbatim"> <span class="q">tr/a-zA-Z//s</span><span class="sc">;</span> <span class="c"># bookkeeper -> bokeper</span></pre>
<pre class="verbatim"> <span class="s">(</span><span class="i">$HOST</span> = <span class="i">$host</span><span class="s">)</span> =~ <span class="q">tr/a-z/A-Z/</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="q">tr/a-zA-Z/ /cs</span><span class="sc">;</span> <span class="c"># change non-alphas to single space</span></pre>
<pre class="verbatim"> <span class="q">tr [\200-\377]</span>
<span class="q"> [\000-\177]</span><span class="sc">;</span> <span class="c"># delete 8th bit</span></pre>
<p>If multiple transliterations are given for a character, only the
first one is used:</p>
<pre class="verbatim"> <span class="q">tr/AAA/XYZ/</span></pre>
<p>will transliterate any A to X.</p>
<p>Because the transliteration table is built at compile time, neither
the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote
interpolation. That means that if you want to use variables, you
must use an eval():</p>
<pre class="verbatim"> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"tr/$oldlist/$newlist/"</span><span class="sc">;</span>
<a class="l_k" href="functions/die.html">die</a> <span class="i">$@</span> if <span class="i">$@</span><span class="sc">;</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"tr/$oldlist/$newlist/, 1"</span> or <a class="l_k" href="functions/die.html">die</a> <span class="i">$@</span><span class="sc">;</span></pre>
</li>
<li><a name="%3c%3cEOF"></a><b><<EOF
</b>
<p>A line-oriented form of quoting is based on the shell "here-document"
syntax. Following a <code class="inline"><<</code>
you specify a string to terminate
the quoted material, and all lines following the current line down to
the terminating string are the value of the item.</p>
<p>The terminating string may be either an identifier (a word), or some
quoted text. An unquoted identifier works like double quotes.
There may not be a space between the <code class="inline"><<</code>
and the identifier,
unless the identifier is explicitly quoted. (If you put a space it
will be treated as a null identifier, which is valid, and matches the
first empty line.) The terminating string must appear by itself
(unquoted and with no surrounding whitespace) on the terminating line.</p>
<p>If the terminating string is quoted, the type of quotes used determine
the treatment of the text.</p>
<ul>
<li><a name="Double-Quotes"></a><b>Double Quotes</b>
<p>Double quotes indicate that the text will be interpolated using exactly
the same rules as normal double quoted strings.</p>
<pre class="verbatim"> print <<EOF;
The price is $Price.
EOF</pre><pre class="verbatim"> print << "EOF"; # same as above
The price is $Price.
EOF</pre></li>
<li><a name="Single-Quotes"></a><b>Single Quotes</b>
<p>Single quotes indicate the text is to be treated literally with no
interpolation of its content. This is similar to single quoted
strings except that backslashes have no special meaning, with <code class="inline">\\</code>
being treated as two backslashes and not one as they would in every
other quoting construct.</p>
<p>This is the only form of quoting in perl where there is no need
to worry about escaping content, something that code generators
can and do make good use of.</p>
</li>
<li><a name="Backticks"></a><b>Backticks</b>
<p>The content of the here doc is treated just as it would be if the
string were embedded in backticks. Thus the content is interpolated
as though it were double quoted and then executed via the shell, with
the results of the execution returned.</p>
<pre class="verbatim"> print << `EOC`; # execute command and get results
echo hi there
EOC</pre></li>
</ul>
<p>It is possible to stack multiple here-docs in a row:</p>
<pre class="verbatim"> print <<"foo", <<"bar"; # you can stack them
I said foo.
foo
I said bar.
bar</pre><pre class="verbatim"> myfunc(<< "THIS", 23, <<'THAT');
Here's a line
or two.
THIS
and here's another.
THAT</pre><p>Just don't forget that you have to put a semicolon on the end
to finish the statement, as Perl doesn't know you're not going to
try to do this:</p>
<pre class="verbatim"> print <<ABC
179231
ABC
+ 20;</pre><p>If you want to remove the line terminator from your here-docs,
use <code class="inline"><a class="l_k" href="functions/chomp.html">chomp()</a></code>.</p>
<pre class="verbatim"> chomp($string = <<'END');
This is a string.
END</pre><p>If you want your here-docs to be indented with the rest of the code,
you'll need to remove leading whitespace from each line manually:</p>
<pre class="verbatim"> ($quote = <<'FINIS') =~ s/^\s+//gm;
The Road goes ever on and on,
down from the door where it began.
FINIS</pre><p>If you use a here-doc within a delimited construct, such as in <code class="inline"><a class="l_k" href="functions/s.html">s///eg</a></code>,
the quoted material must come on the lines following the final delimiter.
So instead of</p>
<pre class="verbatim"> s/this/<<E . 'that'
the other
E
. 'more '/eg;</pre><p>you have to write</p>
<pre class="verbatim"> s/this/<<E . 'that'
. 'more '/eg;
the other
E</pre><p>If the terminating identifier is on the last line of the program, you
must be sure there is a newline after it; otherwise, Perl will give the
warning <b>Can't find string terminator "END" anywhere before EOF...</b>.</p>
<p>Additionally, the quoting rules for the end of string identifier are not
related to Perl's quoting rules -- <code class="inline"><a class="l_k" href="functions/q.html">q()</a></code>, <code class="inline"><a class="l_k" href="functions/qq.html">qq()</a></code>, and the like are not
supported in place of <code class="inline"><span class="q">''</span></code>
and <code class="inline"><span class="q">""</span></code>
, and the only interpolation is for
backslashing the quoting character:</p>
<pre class="verbatim"> print << "abc\"def";
testing...
abc"def</pre><p>Finally, quoted strings cannot span multiple lines. The general rule is
that the identifier must be a string literal. Stick with that, and you
should be safe.</p>
</li>
</ul>
<a name="Gory-details-of-parsing-quoted-constructs"></a><h2>Gory details of parsing quoted constructs
</h2>
<p>When presented with something that might have several different
interpretations, Perl uses the <b>DWIM</b> (that's "Do What I Mean")
principle to pick the most probable interpretation. This strategy
is so successful that Perl programmers often do not suspect the
ambivalence of what they write. But from time to time, Perl's
notions differ substantially from what the author honestly meant.</p>
<p>This section hopes to clarify how Perl handles quoted constructs.
Although the most common reason to learn this is to unravel labyrinthine
regular expressions, because the initial steps of parsing are the
same for all quoting operators, they are all discussed together.</p>
<p>The most important Perl parsing rule is the first one discussed
below: when processing a quoted construct, Perl first finds the end
of that construct, then interprets its contents. If you understand
this rule, you may skip the rest of this section on the first
reading. The other rules are likely to contradict the user's
expectations much less frequently than this first one.</p>
<p>Some passes discussed below are performed concurrently, but because
their results are the same, we consider them individually. For different
quoting constructs, Perl performs different numbers of passes, from
one to four, but these passes are always performed in the same order.</p>
<ul>
<li><a name="Finding-the-end"></a><b>Finding the end</b>
<p>The first pass is finding the end of the quoted construct, where
the information about the delimiters is used in parsing.
During this search, text between the starting and ending delimiters
is copied to a safe location. The text copied gets delimiter-independent.</p>
<p>If the construct is a here-doc, the ending delimiter is a line
that has a terminating string as the content. Therefore <code class="inline"><<EOF</code> is
terminated by <code class="inline"><span class="w">EOF</span></code>
immediately followed by <code class="inline"><span class="q">"\n"</span></code>
and starting
from the first column of the terminating line.
When searching for the terminating line of a here-doc, nothing
is skipped. In other words, lines after the here-doc syntax
are compared with the terminating string line by line.</p>
<p>For the constructs except here-docs, single characters are used as starting
and ending delimiters. If the starting delimiter is an opening punctuation
(that is <code class="inline">(</code>, <code class="inline">[</code>, <code class="inline">{</code>, or <code class="inline"><</code>
), the ending delimiter is the
corresponding closing punctuation (that is <code class="inline">)</code>, <code class="inline">]</code>, <code class="inline">}</code>, or <code class="inline">></code>).
If the starting delimiter is an unpaired character like <code class="inline">/</code> or a closing
punctuation, the ending delimiter is same as the starting delimiter.
Therefore a <code class="inline">/</code> terminates a <code class="inline"><a class="l_k" href="functions/qq.html">qq//</a></code> construct, while a <code class="inline">]</code> terminates
<code class="inline"><a class="l_k" href="functions/qq.html">qq[]</a></code> and <code class="inline"><a class="l_k" href="functions/qq.html">qq]]</a></code> constructs.</p>
<p>When searching for single-character delimiters, escaped delimiters
and <code class="inline">\\</code>
are skipped. For example, while searching for terminating <code class="inline">/</code>,
combinations of <code class="inline">\\</code>
and <code class="inline">\/</code> are skipped. If the delimiters are
bracketing, nested pairs are also skipped. For example, while searching
for closing <code class="inline">]</code> paired with the opening <code class="inline">[</code>, combinations of <code class="inline">\\</code>
, <code class="inline">\]</code>,
and <code class="inline">\[</code> are all skipped, and nested <code class="inline">[</code> and <code class="inline">]</code> are skipped as well.
However, when backslashes are used as the delimiters (like <code class="inline"><a class="l_k" href="functions/qq.html">qq\\</a></code> and
<code class="inline"><a class="l_k" href="functions/tr.html">tr\\\</a></code>), nothing is skipped.
During the search for the end, backslashes that escape delimiters
are removed (exactly speaking, they are not copied to the safe location).</p>
<p>For constructs with three-part delimiters (<code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>, <code class="inline"><a class="l_k" href="functions/y.html">y///</a></code>, and
<code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code>), the search is repeated once more.
If the first delimiter is not an opening punctuation, three delimiters must
be same such as <code class="inline"><a class="l_k" href="functions/s.html">s!!!</a></code> and <code class="inline"><a class="l_k" href="functions/tr.html">tr)))</a></code>, in which case the second delimiter
terminates the left part and starts the right part at once.
If the left part is delimited by bracketing punctuations (that is <code class="inline"><span class="s">(</span><span class="s">)</span></code>
,
<code class="inline"><span class="s">[</span><span class="s">]</span></code>
, <code class="inline"><span class="s">{</span><span class="s">}</span></code>
, or <code class="inline"><></code>
), the right part needs another pair of
delimiters such as <code class="inline"><a class="l_k" href="functions/s.html">s(){}</a></code> and <code class="inline"><a class="l_k" href="functions/tr.html">tr[]//</a></code>. In these cases, whitespaces
and comments are allowed between both parts, though the comment must follow
at least one whitespace; otherwise a character expected as the start of
the comment may be regarded as the starting delimiter of the right part.</p>
<p>During this search no attention is paid to the semantics of the construct.
Thus:</p>
<pre class="verbatim"> "$hash{"$foo/$bar"}"</pre><p>or:</p>
<pre class="verbatim"> m/
bar # NOT a comment, this slash / terminated m//!
/x</pre><p>do not form legal quoted expressions. The quoted part ends on the
first <code class="inline">"</code> and <code class="inline">/</code>, and the rest happens to be a syntax error.
Because the slash that terminated <code class="inline"><a class="l_k" href="functions/m.html">m//</a></code> was followed by a <code class="inline"><span class="w">SPACE</span></code>
,
the example above is not <code class="inline"><a class="l_k" href="functions/m.html">m//x</a></code>, but rather <code class="inline"><a class="l_k" href="functions/m.html">m//</a></code> with no <code class="inline">/x</code>
modifier. So the embedded <code class="inline"><span class="c">#</span></code>
is interpreted as a literal <code class="inline"><span class="c">#</span></code>
.</p>
<p>Also no attention is paid to <code class="inline">\<span class="w">c</span>\</code>
(multichar control char syntax) during
this search. Thus the second <code class="inline">\</code>
in <code class="inline"><a class="l_k" href="functions/qq.html">qq/\c\/</a></code> is interpreted as a part
of <code class="inline">\/</code>, and the following <code class="inline">/</code> is not recognized as a delimiter.
Instead, use <code class="inline">\<span class="n">034</span></code>
or <code class="inline">\<span class="w">x1c</span></code>
at the end of quoted constructs.</p>
</li>
<li><a name="Interpolation"></a><b>Interpolation
</b>
<p>The next step is interpolation in the text obtained, which is now
delimiter-independent. There are multiple cases.</p>
<ul>
<li><a name="'%3c%3c'EOF''"></a><b><code class="inline"><<'EOF'</code></b>
<p>No interpolation is performed.
Note that the combination <code class="inline">\\</code>
is left intact, since escaped delimiters
are not available for here-docs.</p>
</li>
<li><a name="'m'''%2c-the-pattern-of-'s''''"></a><b><code class="inline"><a class="l_k" href="functions/m.html">m''</a></code>, the pattern of <code class="inline"><a class="l_k" href="functions/s.html">s'''</a></code></b>
<p>No interpolation is performed at this stage.
Any backslashed sequences including <code class="inline">\\</code>
are treated at the stage
to <a href="#parsing-regular-expressions">"parsing regular expressions"</a>.</p>
</li>
<li><a name="''''%2c-'q%2f%2f'%2c-'tr''''%2c-'y''''%2c-the-replacement-of-'s''''"></a><b><code class="inline"><span class="q">''</span></code>
, <code class="inline"><a class="l_k" href="functions/q.html">q//</a></code>, <code class="inline"><a class="l_k" href="functions/tr.html">tr'''</a></code>, <code class="inline"><a class="l_k" href="functions/y.html">y'''</a></code>, the replacement of <code class="inline"><a class="l_k" href="functions/s.html">s'''</a></code></b>
<p>The only interpolation is removal of <code class="inline">\</code>
from pairs of <code class="inline">\\</code>
.
Therefore <code class="inline">-</code>
in <code class="inline"><a class="l_k" href="functions/tr.html">tr'''</a></code> and <code class="inline"><a class="l_k" href="functions/y.html">y'''</a></code> is treated literally
as a hyphen and no character range is available.
<code class="inline">\<span class="n">1</span></code>
in the replacement of <code class="inline"><a class="l_k" href="functions/s.html">s'''</a></code> does not work as <code class="inline"><span class="i">$1</span></code>
.</p>
</li>
<li><a name="'tr%2f%2f%2f'%2c-'y%2f%2f%2f'"></a><b><code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code>, <code class="inline"><a class="l_k" href="functions/y.html">y///</a></code></b>
<p>No variable interpolation occurs. String modifying combinations for
case and quoting such as <code class="inline">\<span class="w">Q</span></code>
, <code class="inline">\<span class="w">U</span></code>
, and <code class="inline">\<span class="w">E</span></code>
are not recognized.
The other escape sequences such as <code class="inline">\<span class="n">200</span></code>
and <code class="inline">\<span class="w">t</span></code>
and backslashed
characters such as <code class="inline">\\</code>
and <code class="inline">\-</code>
are converted to appropriate literals.
The character <code class="inline">-</code>
is treated specially and therefore <code class="inline">\-</code>
is treated
as a literal <code class="inline">-</code>
.</p>
</li>
<li><a name="'%22%22'%2c-'%60%60'%2c-'qq%2f%2f'%2c-'qx%2f%2f'%2c-'%3cfile*glob%3e'%2c-'%3c%3c%22EOF%22'"></a><b><code class="inline"><span class="q">""</span></code>
, <code class="inline"><span class="q">``</span></code>
, <code class="inline"><a class="l_k" href="functions/qq.html">qq//</a></code>, <code class="inline"><a class="l_k" href="functions/qx.html">qx//</a></code>, <code class="inline"><span class="q"><file*glob></span></code>
, <code class="inline"><<"EOF"</code></b>
<p><code class="inline">\<span class="w">Q</span></code>
, <code class="inline">\<span class="w">U</span></code>
, <code class="inline">\<span class="w">u</span></code>
, <code class="inline">\<span class="w">L</span></code>
, <code class="inline">\<span class="w">l</span></code>
(possibly paired with <code class="inline">\<span class="w">E</span></code>
) are
converted to corresponding Perl constructs. Thus, <code class="inline"><span class="q">"$foo\Qbaz$bar"</span></code>
is converted to <code class="inline"><span class="i">$foo</span> . <span class="s">(</span><a class="l_k" href="functions/quotemeta.html">quotemeta</a><span class="s">(</span><span class="q">"baz"</span> . <span class="i">$bar</span><span class="s">)</span><span class="s">)</span></code>
internally.
The other escape sequences such as <code class="inline">\<span class="n">200</span></code>
and <code class="inline">\<span class="w">t</span></code>
and backslashed
characters such as <code class="inline">\\</code>
and <code class="inline">\-</code>
are replaced with appropriate
expansions.</p>
<p>Let it be stressed that <i>whatever falls between <code class="inline">\<span class="w">Q</span></code>
and <code class="inline">\<span class="w">E</span></code>
</i>
is interpolated in the usual way. Something like <code class="inline"><span class="q">"\Q\\E"</span></code>
has
no <code class="inline">\<span class="w">E</span></code>
inside. instead, it has <code class="inline">\<span class="w">Q</span></code>
, <code class="inline">\\</code>
, and <code class="inline"><span class="w">E</span></code>
, so the
result is the same as for <code class="inline"><span class="q">"\\\\E"</span></code>
. As a general rule, backslashes
between <code class="inline">\<span class="w">Q</span></code>
and <code class="inline">\<span class="w">E</span></code>
may lead to counterintuitive results. So,
<code class="inline"><span class="q">"\Q\t\E"</span></code>
is converted to <code class="inline"><a class="l_k" href="functions/quotemeta.html">quotemeta("\t")</a></code>, which is the same
as <code class="inline"><span class="q">"\\\t"</span></code>
(since TAB is not alphanumeric). Note also that:</p>
<pre class="verbatim"> <span class="i">$str</span> = <span class="q">'\t'</span><span class="sc">;</span>
<a class="l_k" href="functions/return.html">return</a> <span class="q">"\Q$str"</span><span class="sc">;</span></pre>
<p>may be closer to the conjectural <i>intention</i> of the writer of <code class="inline"><span class="q">"\Q\t\E"</span></code>
.</p>
<p>Interpolated scalars and arrays are converted internally to the <code class="inline"><a class="l_k" href="functions/join.html">join</a></code> and
<code class="inline">.</code> catenation operations. Thus, <code class="inline"><span class="q">"$foo XXX '@arr'"</span></code>
becomes:</p>
<pre class="verbatim"> <span class="i">$foo</span> . <span class="q">" XXX '"</span> . <span class="s">(</span><a class="l_k" href="functions/join.html">join</a> <span class="i">$"</span><span class="cm">,</span> <span class="i">@arr</span><span class="s">)</span> . <span class="q">"'"</span><span class="sc">;</span></pre>
<p>All operations above are performed simultaneously, left to right.</p>
<p>Because the result of <code class="inline"><span class="q">"\Q STRING \E"</span></code>
has all metacharacters
quoted, there is no way to insert a literal <code class="inline"><span class="i">$</span></code>
or <code class="inline"><span class="i">@</span></code>
inside a
<code class="inline">\<span class="w">Q</span>\<span class="w">E</span></code>
pair. If protected by <code class="inline">\</code>
, <code class="inline"><span class="i">$</span></code>
will be quoted to became
<code class="inline"><span class="q">"\\\$"</span></code>
; if not, it is interpreted as the start of an interpolated
scalar.</p>
<p>Note also that the interpolation code needs to make a decision on
where the interpolated scalar ends. For instance, whether
<code class="inline"><span class="q">"a $b -> {c}"</span></code>
really means:</p>
<pre class="verbatim"> <span class="q">"a "</span> . <span class="i">$b</span> . <span class="q">" -> {c}"</span><span class="sc">;</span></pre>
<p>or:</p>
<pre class="verbatim"> <span class="q">"a "</span> . <span class="i">$b</span> -> {<span class="w">c</span>}<span class="sc">;</span></pre>
<p>Most of the time, the longest possible text that does not include
spaces between components and which contains matching braces or
brackets. because the outcome may be determined by voting based
on heuristic estimators, the result is not strictly predictable.
Fortunately, it's usually correct for ambiguous cases.</p>
</li>
<li><a name="the-replacement-of-'s%2f%2f%2f'"></a><b>the replacement of <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code></b>
<p>Processing of <code class="inline">\<span class="w">Q</span></code>
, <code class="inline">\<span class="w">U</span></code>
, <code class="inline">\<span class="w">u</span></code>
, <code class="inline">\<span class="w">L</span></code>
, <code class="inline">\<span class="w">l</span></code>
, and interpolation
happens as with <code class="inline"><a class="l_k" href="functions/qq.html">qq//</a></code> constructs.</p>
<p>It is at this step that <code class="inline">\<span class="n">1</span></code>
is begrudgingly converted to <code class="inline"><span class="i">$1</span></code>
in
the replacement text of <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>, in order to correct the incorrigible
<i>sed</i> hackers who haven't picked up the saner idiom yet. A warning
is emitted if the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
pragma or the <b>-w</b> command-line flag
(that is, the <code class="inline"><span class="i">$^W</span></code>
variable) was set.</p>
</li>
<li><a name="'RE'-in-'%3fRE%3f'%2c-'%2fRE%2f'%2c-'m%2fRE%2f'%2c-'s%2fRE%2ffoo%2f'%2c"></a><b><code class="inline"><span class="w">RE</span></code>
in <code class="inline"><span class="q">?RE?</span></code>
, <code class="inline"><span class="q">/RE/</span></code>
, <code class="inline"><a class="l_k" href="functions/m.html">m/RE/</a></code>, <code class="inline"><a class="l_k" href="functions/s.html">s/RE/foo/</a></code>,</b>
<p>Processing of <code class="inline">\<span class="w">Q</span></code>
, <code class="inline">\<span class="w">U</span></code>
, <code class="inline">\<span class="w">u</span></code>
, <code class="inline">\<span class="w">L</span></code>
, <code class="inline">\<span class="w">l</span></code>
, <code class="inline">\<span class="w">E</span></code>
,
and interpolation happens (almost) as with <code class="inline"><a class="l_k" href="functions/qq.html">qq//</a></code> constructs.</p>
<p>However any other combinations of <code class="inline">\</code>
followed by a character
are not substituted but only skipped, in order to parse them
as regular expressions at the following step.
As <code class="inline">\<span class="w">c</span></code>
is skipped at this step, <code class="inline"><span class="i">@</span></code>
of <code class="inline">\<span class="w">c</span><span class="i">@</span></code>
in RE is possibly
treated as an array symbol (for example <code class="inline"><span class="i">@foo</span></code>
),
even though the same text in <code class="inline"><a class="l_k" href="functions/qq.html">qq//</a></code> gives interpolation of <code class="inline">\<span class="w">c</span><span class="i">@</span></code>
.</p>
<p>Moreover, inside <code class="inline">(?{BLOCK})</code>, <code class="inline">(?# comment )</code>, and
a <code class="inline"><span class="c">#</span></code>
-comment in a <code class="inline"><span class="q">//x</span></code>
-regular expression, no processing is
performed whatsoever. This is the first step at which the presence
of the <code class="inline"><span class="q">//x</span></code>
modifier is relevant.</p>
<p>Interpolation in patterns has several quirks: <code class="inline"><span class="i">$|</span></code>
, <code class="inline"><span class="i">$(</span></code>
, <code class="inline"><span class="i">$)</span></code>
, <code class="inline"><span class="i">@+</span></code>
and <code class="inline"><span class="i">@-</span></code>
are not interpolated, and constructs <code class="inline"><span class="i">$var</span>[<span class="w">SOMETHING</span>]</code>
are
voted (by several different estimators) to be either an array element
or <code class="inline"><span class="i">$var</span></code>
followed by an RE alternative. This is where the notation
<code class="inline"><span class="i">$</span>{<span class="w">arr</span><span class="s">[</span><span class="i">$bar</span><span class="s">]</span>}</code>
comes handy: <code class="inline"><span class="q">/${arr[0-9]}/</span></code>
is interpreted as
array element <code class="inline"><span class="n">-9</span></code>
, not as a regular expression from the variable
<code class="inline"><span class="i">$arr</span></code>
followed by a digit, which would be the interpretation of
<code class="inline"><span class="q">/$arr[0-9]/</span></code>
. Since voting among different estimators may occur,
the result is not predictable.</p>
<p>The lack of processing of <code class="inline">\\</code>
creates specific restrictions on
the post-processed text. If the delimiter is <code class="inline">/</code>, one cannot get
the combination <code class="inline">\/</code> into the result of this step. <code class="inline">/</code> will
finish the regular expression, <code class="inline">\/</code> will be stripped to <code class="inline">/</code> on
the previous step, and <code class="inline">\\/</code> will be left as is. Because <code class="inline">/</code> is
equivalent to <code class="inline">\/</code> inside a regular expression, this does not
matter unless the delimiter happens to be character special to the
RE engine, such as in <code class="inline"><a class="l_k" href="functions/s.html">s*foo*bar*</a></code>, <code class="inline"><a class="l_k" href="functions/m.html">m[foo]</a></code>, or <code class="inline"><span class="q">?foo?</span></code>
; or an
alphanumeric char, as in:</p>
<pre class="verbatim"> <span class="q">m m ^ a \s* b mmx</span><span class="sc">;</span></pre>
<p>In the RE above, which is intentionally obfuscated for illustration, the
delimiter is <code class="inline"><a class="l_k" href="functions/m.html">m</a></code>, the modifier is <code class="inline"><span class="w">mx</span></code>
, and after delimiter-removal the
RE is the same as for <code class="inline"><span class="q">m/ ^ a \s* b /mx</span></code>
. There's more than one
reason you're encouraged to restrict your delimiters to non-alphanumeric,
non-whitespace choices.</p>
</li>
</ul>
<p>This step is the last one for all constructs except regular expressions,
which are processed further.</p>
</li>
<li><a name="parsing-regular-expressions"></a><b>parsing regular expressions
</b>
<p>Previous steps were performed during the compilation of Perl code,
but this one happens at run time--although it may be optimized to
be calculated at compile time if appropriate. After preprocessing
described above, and possibly after evaluation if concatenation,
joining, casing translation, or metaquoting are involved, the
resulting <i>string</i> is passed to the RE engine for compilation.</p>
<p>Whatever happens in the RE engine might be better discussed in <a href="perlre.html">perlre</a>,
but for the sake of continuity, we shall do so here.</p>
<p>This is another step where the presence of the <code class="inline"><span class="q">//x</span></code>
modifier is
relevant. The RE engine scans the string from left to right and
converts it to a finite automaton.</p>
<p>Backslashed characters are either replaced with corresponding
literal strings (as with <code class="inline">\{</code>), or else they generate special nodes
in the finite automaton (as with <code class="inline">\<span class="w">b</span></code>
). Characters special to the
RE engine (such as <code class="inline">|</code>) generate corresponding nodes or groups of
nodes. <code class="inline">(?#...)</code> comments are ignored. All the rest is either
converted to literal strings to match, or else is ignored (as is
whitespace and <code class="inline"><span class="c">#</span></code>
-style comments if <code class="inline"><span class="q">//x</span></code>
is present).</p>
<p>Parsing of the bracketed character class construct, <code class="inline"><span class="s">[</span>...<span class="s">]</span></code>
, is
rather different than the rule used for the rest of the pattern.
The terminator of this construct is found using the same rules as
for finding the terminator of a <code class="inline"><span class="s">{</span><span class="s">}</span></code>
-delimited construct, the only
exception being that <code class="inline">]</code> immediately following <code class="inline">[</code> is treated as
though preceded by a backslash. Similarly, the terminator of
<code class="inline">(?{...})</code> is found using the same rules as for finding the
terminator of a <code class="inline"><span class="s">{</span><span class="s">}</span></code>
-delimited construct.</p>
<p>It is possible to inspect both the string given to RE engine and the
resulting finite automaton. See the arguments <code class="inline"><span class="w">debug</span></code>
/<code class="inline"><span class="w">debugcolor</span></code>
in the <code class="inline">use <a href="[<-$path->]re.html">re</a></code> pragma, as well as Perl's <b>-Dr</b> command-line
switch documented in <a href="perlrun.html#Command-Switches">"Command Switches" in perlrun</a>.</p>
</li>
<li><a name="Optimization-of-regular-expressions"></a><b>Optimization of regular expressions
</b>
<p>This step is listed for completeness only. Since it does not change
semantics, details of this step are not documented and are subject
to change without notice. This step is performed over the finite
automaton that was generated during the previous pass.</p>
<p>It is at this stage that <code class="inline"><a class="l_k" href="functions/split.html">split()</a></code> silently optimizes <code class="inline"><span class="q">/^/</span></code>
to
mean <code class="inline"><span class="q">/^/m</span></code>
.</p>
</li>
</ul>
<a name="I%2fO-Operators"></a><h2>I/O Operators
</h2>
<p>There are several I/O operators you should know about.</p>
<p>A string enclosed by backticks (grave accents) first undergoes
double-quote interpolation. It is then interpreted as an external
command, and the output of that command is the value of the
backtick string, like in a shell. In scalar context, a single string
consisting of all output is returned. In list context, a list of
values is returned, one per line of output. (You can set <code class="inline"><span class="i">$/</span></code>
to use
a different line terminator.) The command is executed each time the
pseudo-literal is evaluated. The status value of the command is
returned in <code class="inline"><span class="i">$?</span></code>
(see <a href="perlvar.html">perlvar</a> for the interpretation of <code class="inline"><span class="i">$?</span></code>
).
Unlike in <b>csh</b>, no translation is done on the return data--newlines
remain newlines. Unlike in any of the shells, single quotes do not
hide variable names in the command from interpretation. To pass a
literal dollar-sign through to the shell you need to hide it with a
backslash. The generalized form of backticks is <code class="inline"><a class="l_k" href="functions/qx.html">qx//</a></code>. (Because
backticks always undergo shell expansion as well, see <a href="perlsec.html">perlsec</a> for
security concerns.)
</p>
<p>In scalar context, evaluating a filehandle in angle brackets yields
the next line from that file (the newline, if any, included), or
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> at end-of-file or on error. When <code class="inline"><span class="i">$/</span></code>
is set to <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>
(sometimes known as file-slurp mode) and the file is empty, it
returns <code class="inline"><span class="q">''</span></code>
the first time, followed by <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> subsequently.</p>
<p>Ordinarily you must assign the returned value to a variable, but
there is one situation where an automatic assignment happens. If
and only if the input symbol is the only thing inside the conditional
of a <code class="inline">while</code>
statement (even if disguised as a <code class="inline">for<span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span></code>
loop),
the value is automatically assigned to the global variable $_,
destroying whatever was there previously. (This may seem like an
odd thing to you, but you'll use the construct in almost every Perl
script you write.) The $_ variable is not implicitly localized.
You'll have to put a <code class="inline"><a class="l_k" href="functions/local.html">local</a> <span class="i">$_</span><span class="sc">;</span></code>
before the loop if you want that
to happen.</p>
<p>The following lines are equivalent:</p>
<pre class="verbatim"> while <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$_</span> = <span class="q"><STDIN></span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span>
while <span class="s">(</span><span class="i">$_</span> = <span class="q"><STDIN></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span>
while <span class="s">(</span><span class="q"><STDIN></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span>
for <span class="s">(</span><span class="sc">;</span><span class="q"><STDIN></span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span>
<a class="l_k" href="functions/print.html">print</a> while <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$_</span> = <span class="q"><STDIN></span><span class="s">)</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> while <span class="s">(</span><span class="i">$_</span> = <span class="q"><STDIN></span><span class="s">)</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> while <span class="q"><STDIN></span><span class="sc">;</span></pre>
<p>This also behaves similarly, but avoids $_ :</p>
<pre class="verbatim"> while <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> = <span class="q"><STDIN></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">$line</span> <span class="s">}</span></pre>
<p>In these loop constructs, the assigned value (whether assignment
is automatic or explicit) is then tested to see whether it is
defined. The defined test avoids problems where line has a string
value that would be treated as false by Perl, for example a "" or
a "0" with no trailing newline. If you really mean for such values
to terminate the loop, they should be tested for explicitly:</p>
<pre class="verbatim"> while <span class="s">(</span><span class="s">(</span><span class="i">$_</span> = <span class="q"><STDIN></span><span class="s">)</span> ne <span class="q">'0'</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span>
while <span class="s">(</span><span class="q"><STDIN></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/last.html">last</a> unless <span class="i">$_</span><span class="sc">;</span> ... <span class="s">}</span></pre>
<p>In other boolean contexts, <code class="inline"><<i>filehandle</i>></code> without an
explicit <code class="inline"><a class="l_k" href="functions/defined.html">defined</a></code> test or comparison elicit a warning if the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
pragma or the <b>-w</b>
command-line switch (the <code class="inline"><span class="i">$^W</span></code>
variable) is in effect.</p>
<p>The filehandles STDIN, STDOUT, and STDERR are predefined. (The
filehandles <code class="inline"><span class="w">stdin</span></code>
, <code class="inline"><span class="w">stdout</span></code>
, and <code class="inline"><span class="w">stderr</span></code>
will also work except
in packages, where they would be interpreted as local identifiers
rather than global.) Additional filehandles may be created with
the open() function, amongst others. See <a href="perlopentut.html">perlopentut</a> and
<a href="functions/open.html">open</a> for details on this.
</p>
<p>If a <FILEHANDLE> is used in a context that is looking for
a list, a list comprising all input lines is returned, one line per
list element. It's easy to grow to a rather large data space this
way, so use with care.</p>
<p><FILEHANDLE> may also be spelled <code class="inline"><a class="l_k" href="functions/readline.html">readline(*FILEHANDLE)</a></code>.
See <a href="functions/readline.html">readline</a>.</p>
<p>The null filehandle <> is special: it can be used to emulate the
behavior of <b>sed</b> and <b>awk</b>. Input from <> comes either from
standard input, or from each file listed on the command line. Here's
how it works: the first time <> is evaluated, the @ARGV array is
checked, and if it is empty, <code class="inline"><span class="i">$ARGV</span>[<span class="n">0</span>]</code>
is set to "-", which when opened
gives you standard input. The @ARGV array is then processed as a list
of filenames. The loop</p>
<pre class="verbatim"> while <span class="s">(</span><><span class="s">)</span> <span class="s">{</span>
... <span class="c"># code for each line</span>
<span class="s">}</span></pre>
<p>is equivalent to the following Perl-like pseudo code:</p>
<pre class="verbatim"> <a class="l_k" href="functions/unshift.html">unshift</a><span class="s">(</span><span class="i">@ARGV</span><span class="cm">,</span> <span class="q">'-'</span><span class="s">)</span> unless <span class="i">@ARGV</span><span class="sc">;</span>
while <span class="s">(</span><span class="i">$ARGV</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">ARGV</span><span class="cm">,</span> <span class="i">$ARGV</span><span class="s">)</span><span class="sc">;</span>
while <span class="s">(</span><span class="q"><ARGV></span><span class="s">)</span> <span class="s">{</span>
... <span class="c"># code for each line</span>
<span class="s">}</span>
<span class="s">}</span></pre>
<p>except that it isn't so cumbersome to say, and will actually work.
It really does shift the @ARGV array and put the current filename
into the $ARGV variable. It also uses filehandle <i>ARGV</i>
internally--<> is just a synonym for <ARGV>, which
is magical. (The pseudo code above doesn't work because it treats
<ARGV> as non-magical.)</p>
<p>You can modify @ARGV before the first <> as long as the array ends up
containing the list of filenames you really want. Line numbers (<code class="inline"><span class="i">$.</span></code>
)
continue as though the input were one big happy file. See the example
in <a href="functions/eof.html">eof</a> for how to reset line numbers on each file.</p>
<p>If you want to set @ARGV to your own list of files, go right ahead.
This sets @ARGV to all plain text files if no @ARGV was given:</p>
<pre class="verbatim"> <span class="i">@ARGV</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> -f && -T <span class="s">}</span> <a class="l_k" href="functions/glob.html">glob</a><span class="s">(</span><span class="q">'*'</span><span class="s">)</span> unless <span class="i">@ARGV</span><span class="sc">;</span></pre>
<p>You can even set them to pipe commands. For example, this automatically
filters compressed arguments through <b>gzip</b>:</p>
<pre class="verbatim"> <span class="i">@ARGV</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="q">/\.(gz|Z)$/</span> ? <span class="q">"gzip -dc < $_ |"</span> <span class="co">:</span> <span class="i">$_</span> <span class="s">}</span> <span class="i">@ARGV</span><span class="sc">;</span></pre>
<p>If you want to pass switches into your script, you can use one of the
Getopts modules or put a loop on the front like this:</p>
<pre class="verbatim"> while <span class="s">(</span><span class="i">$_</span> = <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="cm">,</span> <span class="q">/^-/</span><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span>
<a class="l_k" href="functions/last.html">last</a> if <span class="q">/^--$/</span><span class="sc">;</span>
if <span class="s">(</span><span class="q">/^-D(.*)/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$debug</span> = <span class="i">$1</span> <span class="s">}</span>
if <span class="s">(</span><span class="q">/^-v/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$verbose</span>++ <span class="s">}</span>
<span class="c"># ... # other switches</span>
<span class="s">}</span></pre>
<pre class="verbatim"> while <span class="s">(</span><><span class="s">)</span> <span class="s">{</span>
<span class="c"># ... # code for each line</span>
<span class="s">}</span></pre>
<p>The <> symbol will return <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> for end-of-file only once.
If you call it again after this, it will assume you are processing another
@ARGV list, and if you haven't set @ARGV, will read input from STDIN.</p>
<p>If what the angle brackets contain is a simple scalar variable (e.g.,
<$foo>), then that variable contains the name of the
filehandle to input from, or its typeglob, or a reference to the
same. For example:</p>
<pre class="verbatim"> <span class="i">$fh</span> = \<span class="i">*STDIN</span><span class="sc">;</span>
<span class="i">$line</span> = <span class="q"><$fh></span><span class="sc">;</span></pre>
<p>If what's within the angle brackets is neither a filehandle nor a simple
scalar variable containing a filehandle name, typeglob, or typeglob
reference, it is interpreted as a filename pattern to be globbed, and
either a list of filenames or the next filename in the list is returned,
depending on context. This distinction is determined on syntactic
grounds alone. That means <code class="inline"><span class="q"><$x></span></code>
is always a readline() from
an indirect handle, but <code class="inline"><span class="q"><$hash{key}></span></code>
is always a glob().
That's because $x is a simple scalar variable, but <code class="inline"><span class="i">$hash</span>{<span class="w">key</span>}</code>
is
not--it's a hash element. Even <code class="inline"><span class="q"><$x ></span></code>
(note the extra space)
is treated as <code class="inline"><a class="l_k" href="functions/glob.html">glob</a><span class="s">(</span><span class="q">"$x "</span><span class="s">)</span></code>
, not <code class="inline"><a class="l_k" href="functions/readline.html">readline($x)</a></code>.</p>
<p>One level of double-quote interpretation is done first, but you can't
say <code class="inline"><span class="q"><$foo></span></code>
because that's an indirect filehandle as explained
in the previous paragraph. (In older versions of Perl, programmers
would insert curly brackets to force interpretation as a filename glob:
<code class="inline"><span class="q"><${foo}></span></code>
. These days, it's considered cleaner to call the
internal function directly as <code class="inline"><a class="l_k" href="functions/glob.html">glob($foo)</a></code>, which is probably the right
way to have done it in the first place.) For example:</p>
<pre class="verbatim"> while <span class="s">(</span><span class="q"><*.c></span><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/chmod.html">chmod</a> <span class="n">0644</span><span class="cm">,</span> <span class="i">$_</span><span class="sc">;</span>
<span class="s">}</span></pre>
<p>is roughly equivalent to:</p>
<pre class="verbatim"> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">"echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|"</span><span class="s">)</span><span class="sc">;</span>
while <span class="s">(</span><span class="q"><FOO></span><span class="s">)</span> <span class="s">{</span>
<a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span>
<a class="l_k" href="functions/chmod.html">chmod</a> <span class="n">0644</span><span class="cm">,</span> <span class="i">$_</span><span class="sc">;</span>
<span class="s">}</span></pre>
<p>except that the globbing is actually done internally using the standard
<code class="inline"><span class="w">File::Glob</span></code>
extension. Of course, the shortest way to do the above is:</p>
<pre class="verbatim"> <a class="l_k" href="functions/chmod.html">chmod</a> <span class="n">0644</span><span class="cm">,</span> <span class="q"><*.c></span><span class="sc">;</span></pre>
<p>A (file)glob evaluates its (embedded) argument only when it is
starting a new list. All values must be read before it will start
over. In list context, this isn't important because you automatically
get them all anyway. However, in scalar context the operator returns
the next value each time it's called, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> when the list has
run out. As with filehandle reads, an automatic <code class="inline"><a class="l_k" href="functions/defined.html">defined</a></code> is
generated when the glob occurs in the test part of a <code class="inline">while</code>
,
because legal glob returns (e.g. a file called <i>0</i>) would otherwise
terminate the loop. Again, <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned only once. So if
you're expecting a single value from a glob, it is much better to
say</p>
<pre class="verbatim"> <span class="s">(</span><span class="i">$file</span><span class="s">)</span> = <span class="q"><blurch*></span><span class="sc">;</span></pre>
<p>than</p>
<pre class="verbatim"> <span class="i">$file</span> = <span class="q"><blurch*></span><span class="sc">;</span></pre>
<p>because the latter will alternate between returning a filename and
returning false.</p>
<p>If you're trying to do variable interpolation, it's definitely better
to use the glob() function, because the older notation can cause people
to become confused with the indirect filehandle notation.</p>
<pre class="verbatim"> <span class="i">@files</span> = <a class="l_k" href="functions/glob.html">glob</a><span class="s">(</span><span class="q">"$dir/*.[ch]"</span><span class="s">)</span><span class="sc">;</span>
<span class="i">@files</span> = <a class="l_k" href="functions/glob.html">glob</a><span class="s">(</span><span class="i">$files</span>[<span class="i">$i</span>]<span class="s">)</span><span class="sc">;</span></pre>
<a name="Constant-Folding"></a><h2>Constant Folding
</h2>
<p>Like C, Perl does a certain amount of expression evaluation at
compile time whenever it determines that all arguments to an
operator are static and have no side effects. In particular, string
concatenation happens at compile time between literals that don't do
variable substitution. Backslash interpolation also happens at
compile time. You can say</p>
<pre class="verbatim"> <span class="q">'Now is the time for all'</span> . <span class="q">"\n"</span> .
<span class="q">'good men to come to.'</span></pre>
<p>and this all reduces to one string internally. Likewise, if
you say</p>
<pre class="verbatim"> foreach <span class="i">$file</span> <span class="s">(</span><span class="i">@filenames</span><span class="s">)</span> <span class="s">{</span>
if <span class="s">(</span>-s <span class="i">$file</span> > <span class="n">5</span> + <span class="n">100</span> * <span class="n">2</span>**<span class="n">16</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>
<span class="s">}</span></pre>
<p>the compiler will precompute the number which that expression
represents so that the interpreter won't have to.</p>
<a name="No-ops"></a><h2>No-ops
</h2>
<p>Perl doesn't officially have a no-op operator, but the bare constants
<code class="inline"><span class="n">0</span></code>
and <code class="inline"><span class="n">1</span></code>
are special-cased to not produce a warning in a void
context, so you can for example safely do</p>
<pre class="verbatim"> <span class="n">1</span> while <span class="i">foo</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></pre>
<a name="Bitwise-String-Operators"></a><h2>Bitwise String Operators
</h2>
<p>Bitstrings of any size may be manipulated by the bitwise operators
(<code class="inline">~ | & ^</code>).</p>
<p>If the operands to a binary bitwise op are strings of different
sizes, <b>|</b> and <b>^</b> ops act as though the shorter operand had
additional zero bits on the right, while the <b>&</b> op acts as though
the longer operand were truncated to the length of the shorter.
The granularity for such extension or truncation is one or more
bytes.</p>
<pre class="verbatim"> <span class="c"># ASCII-based examples</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"j p \n"</span> ^ <span class="q">" a h"</span><span class="sc">;</span> <span class="c"># prints "JAPH\n"</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"JA"</span> | <span class="q">" ph\n"</span><span class="sc">;</span> <span class="c"># prints "japh\n"</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">"japh\nJunk"</span> & <span class="q">'_____'</span><span class="sc">;</span> <span class="c"># prints "JAPH\n";</span>
<a class="l_k" href="functions/print.html">print</a> <span class="q">'p N$'</span> ^ <span class="q">" E<H\n"</span><span class="sc">;</span> <span class="c"># prints "Perl\n";</span></pre>
<p>If you are intending to manipulate bitstrings, be certain that
you're supplying bitstrings: If an operand is a number, that will imply
a <b>numeric</b> bitwise operation. You may explicitly show which type of
operation you intend by using <code class="inline"><span class="q">""</span></code>
or <code class="inline"><span class="n">0</span>+</code>
, as in the examples below.</p>
<pre class="verbatim"> <span class="i">$foo</span> = <span class="n">150</span> | <span class="n">105</span><span class="sc">;</span> <span class="c"># yields 255 (0x96 | 0x69 is 0xFF)</span>
<span class="i">$foo</span> = <span class="q">'150'</span> | <span class="n">105</span><span class="sc">;</span> <span class="c"># yields 255</span>
<span class="i">$foo</span> = <span class="n">150</span> | <span class="q">'105'</span><span class="sc">;</span> <span class="c"># yields 255</span>
<span class="i">$foo</span> = <span class="q">'150'</span> | <span class="q">'105'</span><span class="sc">;</span> <span class="c"># yields string '155' (under ASCII)</span></pre>
<pre class="verbatim"> <span class="i">$baz</span> = <span class="n">0</span>+<span class="i">$foo</span> & <span class="n">0</span>+<span class="i">$bar</span><span class="sc">;</span> <span class="c"># both ops explicitly numeric</span>
<span class="i">$biz</span> = <span class="q">"$foo"</span> ^ <span class="q">"$bar"</span><span class="sc">;</span> <span class="c"># both ops explicitly stringy</span></pre>
<p>See <a href="functions/vec.html">vec</a> for information on how to manipulate individual bits
in a bit vector.</p>
<a name="Integer-Arithmetic"></a><h2>Integer Arithmetic
</h2>
<p>By default, Perl assumes that it must do most of its arithmetic in
floating point. But by saying</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></pre>
<p>you may tell the compiler that it's okay to use integer operations
(if it feels like it) from here to the end of the enclosing BLOCK.
An inner BLOCK may countermand this by saying</p>
<pre class="verbatim"> <a class="l_k" href="functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></pre>
<p>which lasts until the end of that BLOCK. Note that this doesn't
mean everything is only an integer, merely that Perl may use integer
operations if it is so inclined. For example, even under <code class="inline"><a class="l_k" href="functions/use.html">use</a>
<span class="w">integer</span></code>
, if you take the <code class="inline"><a class="l_k" href="functions/sqrt.html">sqrt(2)</a></code>, you'll still get <code class="inline"><span class="n">1.4142135623731</span></code>
or so.</p>
<p>Used on numbers, the bitwise operators ("&", "|", "^", "~", "<<",
and ">>") always produce integral results. (But see also
<a href="#Bitwise-String-Operators">"Bitwise String Operators"</a>.) However, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
still has meaning for
them. By default, their results are interpreted as unsigned integers, but
if <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
is in effect, their results are interpreted
as signed integers. For example, <code class="inline">~<span class="n">0</span></code>
usually evaluates to a large
integral value. However, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span> ~<span class="n">0</span></code>
is <code class="inline"><span class="n">-1</span></code>
on two's-complement
machines.</p>
<a name="Floating-point-Arithmetic"></a><h2>Floating-point Arithmetic
</h2>
<p>While <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
provides integer-only arithmetic, there is no
analogous mechanism to provide automatic rounding or truncation to a
certain number of decimal places. For rounding to a certain number
of digits, sprintf() or printf() is usually the easiest route.
See <a href="perlfaq4.html">perlfaq4</a>.</p>
<p>Floating-point numbers are only approximations to what a mathematician
would call real numbers. There are infinitely more reals than floats,
so some corners must be cut. For example:</p>
<pre class="verbatim"> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">"%.20g\n"</span><span class="cm">,</span> <span class="n">123456789123456789</span><span class="sc">;</span>
<span class="c"># produces 123456789123456784</span></pre>
<p>Testing for exact equality of floating-point equality or inequality is
not a good idea. Here's a (relatively expensive) work-around to compare
whether two floating-point numbers are equal to a particular number of
decimal places. See Knuth, volume II, for a more robust treatment of
this topic.</p>
<pre class="verbatim"><a name="fp_equal"></a> sub <span class="m">fp_equal</span> <span class="s">{</span>
<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$X</span><span class="cm">,</span> <span class="i">$Y</span><span class="cm">,</span> <span class="i">$POINTS</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>
<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$tX</span><span class="cm">,</span> <span class="i">$tY</span><span class="s">)</span><span class="sc">;</span>
<span class="i">$tX</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">"%.${POINTS}g"</span><span class="cm">,</span> <span class="i">$X</span><span class="s">)</span><span class="sc">;</span>
<span class="i">$tY</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">"%.${POINTS}g"</span><span class="cm">,</span> <span class="i">$Y</span><span class="s">)</span><span class="sc">;</span>
<a class="l_k" href="functions/return.html">return</a> <span class="i">$tX</span> eq <span class="i">$tY</span><span class="sc">;</span>
<span class="s">}</span></pre>
<p>The POSIX module (part of the standard perl distribution) implements
ceil(), floor(), and other mathematical and trigonometric functions.
The Math::Complex module (part of the standard perl distribution)
defines mathematical functions that work on both the reals and the
imaginary numbers. Math::Complex not as efficient as POSIX, but
POSIX can't work with complex numbers.</p>
<p>Rounding in financial applications can have serious implications, and
the rounding method used should be specified precisely. In these
cases, it probably pays not to trust whichever system rounding is
being used by Perl, but to instead implement the rounding function you
need yourself.</p>
<a name="Bigger-Numbers"></a><h2>Bigger Numbers
</h2>
<p>The standard Math::BigInt and Math::BigFloat modules provide
variable-precision arithmetic and overloaded operators, although
they're currently pretty slow. At the cost of some space and
considerable speed, they avoid the normal pitfalls associated with
limited-precision representations.</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">Math::BigInt</span><span class="sc">;</span>
<span class="i">$x</span> = <span class="w">Math::BigInt</span><span class="w">->new</span><span class="s">(</span><span class="q">'123456789123456789'</span><span class="s">)</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span> * <span class="i">$x</span><span class="sc">;</span></pre>
<pre class="verbatim"> <span class="c"># prints +15241578780673678515622620750190521</span></pre>
<p>There are several modules that let you calculate with (bound only by
memory and cpu-time) unlimited or fixed precision. There are also
some non-standard modules that provide faster implementations via
external C libraries.</p>
<p>Here is a short, but incomplete summary:</p>
<pre class="verbatim"> Math::Fraction big, unlimited fractions like 9973 / 12967
Math::String treat string sequences like numbers
Math::FixedPrecision calculate with a fixed precision
Math::Currency for currency calculations
Bit::Vector manipulate bit vectors fast (uses C)
Math::BigIntFast Bit::Vector wrapper for big numbers
Math::Pari provides access to the Pari C library
Math::BigInteger uses an external C library
Math::Cephes uses external Cephes C library (no big numbers)
Math::Cephes::Fraction fractions via the Cephes library
Math::GMP another one using an external C library</pre><p>Choose wisely.</p>
</div>
<div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
</div>
</div>
<div id="right">
<div id="rightContent">
<div id="leftClose">
<a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
</div>
<h1>Search:</h1>
<p>
<form action="search.html" name="perldoc_search">
<input type="text" name="q" size="10" class="grey"><br>
<!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
</form>
</p>
<script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
<h2>Labels:</h2>
<p>
<a href="#" onClick="addLabel('perlop','perlop.html')">Add this page</a>
</p>
<div class="labels" id="labels">
</div>
</div>
</div>
</div>
</body>
</html>
|