perl-begin / lib / tutorials / perl-for-newbies / lect1-all-in-one / index.html

   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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
         PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="./style.css" type="text/css" />




<title>
"Perl for Perl Newbies" - Part 1
</title>
</head>
<body><div class="page" id="page--DIR">

<div class="center"><script type="text/javascript">
google_ad_client = "pub-2480595666283917";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "336699";
google_color_text = "000000";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<b>Up</b></td>
<td>
<b>Prev</b></td>
<td>
<a href="#page--intro--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a>
</div>
<h1>
"Perl for Perl Newbies" - Part 1
</h1>
<h2>Contents</h2>
<ul class="contentsmain">
<li><a href="#page--intro--DIR" class="contents">1. Introduction</a>
<ul class="contents">
<li><a href="#page--intro--capabilities--PAGE" class="contents">1.1. The capabilities of Perl</a>
</li><li><a href="#page--intro--history--PAGE" class="contents">1.2. A brief history of Perl</a>
</li><li><a href="#page--intro--devel_cycle--PAGE" class="contents">1.3. The perl development cycle</a>
</li></ul>
</li><li><a href="#page--output--DIR" class="contents">2. Basic Output (The "Hello World" program)</a>
<ul class="contents">
<li><a href="#page--output--semicolons--PAGE" class="contents">2.1. More about semicolons</a>
</li></ul>
</li><li><a href="#page--expressions--DIR" class="contents">3. Expressions</a>
<ul class="contents">
<li><a href="#page--expressions--operators--PAGE" class="contents">3.1. Operators and Precendence</a>
</li><li><a href="#page--expressions--functions--PAGE" class="contents">3.2. Functions</a>
</li><li><a href="#page--expressions--strings--DIR" class="contents">3.3. More about strings</a>
<ul class="contents">
<li><a href="#page--expressions--strings--escape--PAGE" class="contents">3.3.1. Escape Sequences</a>
</li></ul>
</li></ul>
</li><li><a href="#page--variables--DIR" class="contents">4. Variables</a>
<ul class="contents">
<li><a href="#page--variables--plus-equal--PAGE" class="contents">4.1. "+=" and friends</a>
</li></ul>
</li><li><a href="#page--input--PAGE" class="contents">5. Input</a>
</li><li><a href="#page--for_loop--PAGE" class="contents">6. The For Loop</a>
</li><li><a href="#page--conditionals--DIR" class="contents">7. Conditionals</a>
<ul class="contents">
<li><a href="#page--conditionals--numerical--PAGE" class="contents">7.1. Numerical Comparison Operators</a>
</li><li><a href="#page--conditionals--string--PAGE" class="contents">7.2. String Comparison Operators</a>
</li><li><a href="#page--conditionals--boolean--PAGE" class="contents">7.3. Boolean Operators</a>
</li><li><a href="#page--conditionals--true_vs_false--PAGE" class="contents">7.4. True Expressions vs. False Expressions</a>
</li><li><a href="#page--conditionals--elsif--PAGE" class="contents">7.5. elsif</a>
</li></ul>
</li><li><a href="#page--while--DIR" class="contents">8. The While Loop</a>
<ul class="contents">
<li><a href="#page--while--last_and_next--PAGE" class="contents">8.1. last and next</a>
</li></ul>
</li><li><a href="#page--arrays--DIR" class="contents">9. Arrays</a>
<ul class="contents">
<li><a href="#page--arrays--comma--PAGE" class="contents">9.1. The ',' operator</a>
</li><li><a href="#page--arrays--negative_indexes--PAGE" class="contents">9.2. Negative Indexes</a>
</li><li><a href="#page--arrays--foreach--DIR" class="contents">9.3. The foreach loop</a>
<ul class="contents">
<li><a href="#page--arrays--foreach--for_and_dotdot--PAGE" class="contents">9.3.1. The for keyword and the .. operator</a>
</li></ul>
</li><li><a href="#page--arrays--functions--PAGE" class="contents">9.4. Built-In Array Functions</a>
</li><li><a href="#page--arrays--x--PAGE" class="contents">9.5. The x operator</a>
</li></ul>
</li></ul>
<h2 id="licence">Licence</h2>
<p xmlns:dct="http://purl.org/dc/terms/" xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#">
  <a rel="license" href="http://creativecommons.org/publicdomain/zero/1.0/" style="text-decoration:none;">
    <img src="http://i.creativecommons.org/l/zero/1.0/80x15.png" border="0" alt="CC0" />
  </a>
  <br />
  To the extent possible under law, <a href="http://www.shlomifish.org/" rel="dct:publisher"><span property="dct:title">Shlomi Fish</span></a>
  has waived all copyright and related or neighboring rights to
  <span property="dct:title">Perl for Perl Newbies</span>.
This work is published from
<span about="http://www.shlomifish.org/" property="vcard:Country" datatype="dct:ISO3166" content="IL">Israel</span>.
</p>
<hr />

</div>
<div class="page" id="page--intro--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--intro--capabilities--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--intro--DIR">Introduction</a>
</div>
<h1>
1. Introduction
</h1>
<p>
<i style="font-size:large">"There's more than one way to do it."</i>
<b class="plain">(The Perl Motto)</b>
</p>
<ul class="contentsmain">
<li><a href="#page--intro--capabilities--PAGE" class="contents">1.1. The capabilities of Perl</a>
</li><li><a href="#page--intro--history--PAGE" class="contents">1.2. A brief history of Perl</a>
</li><li><a href="#page--intro--devel_cycle--PAGE" class="contents">1.3. The perl development cycle</a>
</li></ul>
<hr />

</div>
<div class="page" id="page--intro--capabilities--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--intro--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--intro--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--intro--history--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--intro--DIR">Introduction</a><a href="#page--intro--capabilities--PAGE">The capabilities of Perl</a>
</div>
<h1>
1.1. The capabilities of Perl
</h1>
<ul class="point">
<li>
Strings and data structures that are unlimited in size,
nested to any depth.
</li>
<li>
Powerful syntax and built-in functions.
</li>
<li>
Extended, built-in support for regular expressions.
</li>
<li>
Support for namespaces, classes, and objects.
</li>
<li>
Functional Programming capabilities such as closures
and continuations.
</li>
<li>
<a href="http://www.cpan.org/">CPAN</a> - a comprehensive on-line archive of
easily installable and re-usable modules.
</li>
<li>
Powerful debugger with optional visual front-ends.
</li>
<li>
Runs on almost any platform imaginable.
</li>
<li>
Relatively portable and secure code.
</li>
</ul>
<hr />

</div>
<div class="page" id="page--intro--history--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--intro--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--intro--capabilities--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--intro--devel_cycle--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--intro--DIR">Introduction</a><a href="#page--intro--history--PAGE">A brief history of Perl</a>
</div>
<h1>
1.2. A brief history of Perl
</h1>
<p>
Perl was introduced in 1987 (4 years before Linux itself), when the
author, <b class="plain">Larry Wall</b>, released version 1.000 of it.
The reason for its creation was that Wall was unhappy by the
functionality that sed, C, awk and the Bourne Shell offered him. He
looked for a language that will combine all of their best features,
while having as few disadvantages of its own.
</p>
<p>
Since then, perl has seen several versions, each adding additional
functionality. perl version 5, which was released in 1994, was a
complete re-write of the perl interpreter, and introduced such things
as hard references, modules, objects and lexical scoping. Several minor
versions of perl appeared since then, and the most up-to-date stable
version (as of October 2005) is 5.8.x.
</p>
<p>
Perl became especially popular as a language for writing server-side
scripts for web-servers. But that's not the only use of perl, as it is
commonly used for system administration tasks, managing database data, as
well as writing GUI applications.
</p>
<h3>Links:</h3>
<p>
<a href="http://history.perl.org/PerlTimeline.html">
http://history.perl.org/PerlTimeline.html
</a>
- The perl TimeLine
</p>
<p>
<a href="http://www.weblint.org/~neilb/perl/VHLL/slide01.html">
http://www.weblint.org/~neilb/perl/VHLL/slide01.html
</a> -
<i>The Taming of the Camel</i>:
a lecture by Larry Wall about the Evolution of Perl
</p>
<hr />

</div>
<div class="page" id="page--intro--devel_cycle--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--intro--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--intro--history--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--output--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--intro--DIR">Introduction</a><a href="#page--intro--devel_cycle--PAGE">The perl development cycle</a>
</div>
<h1>
1.3. The perl development cycle
</h1>
<p>
Perl is interpreted, so no compilation is needed. To use perl, one should
create a text file that contains the Perl program. This file can be written
using any text editor available on your system. It is recommended that
you end the filenames of your perl scripts with ".pl" in order to designate
them as perl scripts.
</p>
<p>
After you are done, you should invoke the perl interpreter with the name of
the file you created. Assuming your file is name "myscript.pl", you should
type:
</p>
<div class="code_block"><pre>
$ perl myscript.pl
</pre></div>
<p>
At the command line (and press Enter). If your program contains errors or
warnings they will be displayed. If your program does not contain errors, it
will be executed.
</p>
<p>
The fact that the program was executed does not mean it does what you want it
to do. We will learn how to debug Perl programs, later on in this series.
</p>
<hr />

</div>
<div class="page" id="page--output--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--intro--devel_cycle--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--output--semicolons--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--output--DIR">Basic Output (The "Hello World" program)</a>
</div>
<h1>
2. Basic Output (The "Hello World" program)
</h1>
<p>
In Perl we use the <tt>print</tt> command to echo strings and expressions
to the screen.
</p>
<p>
The neophyte "Hello World!" program can be written in perl as follows:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Hello, World!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>Now here's some explanation (in case you need any):</p>
<ol>
<li>
The string is enclosed in double-quotes(<tt>" ... "</tt>) because that's how
string constants are represented in perl.
</li>
<li>
<tt>\n</tt> is a special character that is called "newline". When you print a
newline you will see that your output started on a new line.
</li>
<li>
The semi-colon (<tt>;</tt>) at the end indicates that this is the end of
a perl command. Every perl command should be terminated with a semicolon.
</li>
</ol>
<hr />

</div>
<div class="page" id="page--output--semicolons--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--output--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--output--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--expressions--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--output--DIR">Basic Output (The "Hello World" program)</a><a href="#page--output--semicolons--PAGE">More about semicolons</a>
</div>
<h1>
2.1. More about semicolons
</h1>
<p>
A perl program can naturally include more than one command. To do so, you
need to place a semicolon at the end of each command or otherwise the
interpreter will be confused.
</p>
<p>
It is not enough to put each command on a separate line. In fact it's not
even necessary. The following two programs are equivalent:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">One Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Two Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Red Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Blue Fish.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
and
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">One Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>; <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Two Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Red Fish,</span><span class="Special">\n</span><span class="Constant">&quot;</span>; <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Blue Fish.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
However, for readability and easy debugging it is recommended that each
statement will be on a separate line. And sometimes it's helpful to span
one on more than one line.
</p>
<hr />

</div>
<div class="page" id="page--expressions--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--output--semicolons--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--expressions--operators--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--expressions--DIR">Expressions</a>
</div>
<h1>
3. Expressions
</h1>
<p>
Perl supports such mathematical operators as <tt>+</tt>, <tt>-</tt>,
<tt>*</tt> (multiplication), and parenthesis (<tt>( ... )</tt>). An example
is worth a thousand words:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">5 + 6 = </span><span class="Constant">&quot;</span>, <span class="Constant">5+6</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">(2 + 3) * 6 = </span><span class="Constant">&quot;</span>, (<span class="Constant">2+3</span>)*<span class="Constant">6</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2 + 3 * 6 = </span><span class="Constant">&quot;</span>, <span class="Constant">2+3</span>*<span class="Constant">6</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2 raised to the power of 8 is </span><span class="Constant">&quot;</span>, <span class="Constant">2</span>**<span class="Constant">8</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">10-5 = </span><span class="Constant">&quot;</span>, <span class="Constant">10-5</span>, <span class="Constant">&quot;</span><span class="Constant">. 5-10 = </span><span class="Constant">&quot;</span>, <span class="Constant">5-10</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2/3 = </span><span class="Constant">&quot;</span>, <span class="Constant">2</span>/<span class="Constant">3</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
The output of this program is:
</p>
<div class="code_block"><pre>
5 + 6 = 11
(2 + 3) * 6 = 30
2 + 3 * 6 = 20
2 raised to the power of 8 is 256
10-5 = 5. 5-10 = -5
2/3 = 0.666666666666667
</pre></div>
<p>
The operators have the same precendence as their mathemtical equivalents. The
parenthesis are useful for making sure a sub-expression will be evaluated
before all others do.
</p>
<table border="1" summary="">
<tr>
<td>
<h3>Note:</h3>
You can use commas to print more than one expression at once. It beats
writing a separate <tt>print</tt> command for every expression you wish to
output. However, for better readability, it is recommended that you will
separate your expressions among several prints.
</td>
</tr>
</table>
<hr />

</div>
<div class="page" id="page--expressions--operators--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--expressions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--expressions--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--expressions--functions--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--expressions--DIR">Expressions</a><a href="#page--expressions--operators--PAGE">Operators and Precendence</a>
</div>
<h1>
3.1. Operators and Precendence
</h1>
<p>
Here are some perl operators of interest.
</p>
<h2>+ , - , * , /</h2>
<p>
Respectively adds, subtracts, multiplies and divides two floating point
numbers.
</p>
<h2>a ** b</h2>
<p>
Raises "a" to the power of "b". Works on floating point numbers too.
</p>
<h2>a . b</h2>
<p>
Concatenates two strings. The comma (<tt>,</tt>) as used by print does not
really concatenates two strings, but rather prints them one after the other.
(There's a subtle difference in functionality of the print command too, but we
won't get into that, now).
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Hello,</span><span class="Constant">&quot;</span> . <span class="Constant">&quot;</span><span class="Constant"> </span><span class="Constant">&quot;</span> . <span class="Constant">&quot;</span><span class="Constant">World!</span><span class="Constant">&quot;</span> . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span> .
    <span class="Constant">&quot;</span><span class="Constant">And this is the second line.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<h2>a % b</h2>
<p>
Returns the modulo (remainder) of "b" from "a". If "a" and "b" are not
integers they are rounded to an integral value.
</p>
<h2>( sub-expr )</h2>
<p>
Makes sure that <tt>sub-expr</tt> is evaluated as a separate sub-expression
, an operation that could override the defualt operator precendence.
</p>
<hr />
<p>
There are many more, but they will be covered later. For a complete list and
more detailed information about the various perl operators consult the
<a href="http://perldoc.perl.org/perlop.html">"perlop" document</a>
on your system.
</p>
<hr />

</div>
<div class="page" id="page--expressions--functions--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--expressions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--expressions--operators--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--expressions--strings--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--expressions--DIR">Expressions</a><a href="#page--expressions--functions--PAGE">Functions</a>
</div>
<h1>
3.2. Functions
</h1>
<p>
How many characters are in the perl motto? Perl can tell that right away:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Statement">length</span>(<span class="Constant">&quot;</span><span class="Constant">There's more than one way to do it</span><span class="Constant">&quot;</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
<tt>length()</tt> is a built-in function that tells how many characters are in
a string. A function is a named sub-routine that accepts several arguments
and returns a value that can be further evaluated as part of a greater
expression, or used directly.
</p>
<p>
To help us understand functions further, let's inspect the perl function
<tt>substr</tt> (short for "substring"). <tt>substr</tt> retrieves sub-strings
of a given string. The first argument to it is a string, the second is the
offset from which to take the substring, and the third is the length of the
substring to be taken. The third one is optional and if unspecified, returns
the substring till the end of the string.
</p>
<p>
An example will illustrate it best:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Statement">substr</span>(<span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">3</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Statement">substr</span>(<span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">1</span>, <span class="Constant">4</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Statement">substr</span>(<span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">0</span>, <span class="Constant">6</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
The output of this program is:
</p>
<div class="code_block"><pre>
ong string
 lon
A long
</pre></div>
<p>( You may notice that the position of the first character is 0. )</p>
<p>
The commas are used to separate the arguments to the function and they
are mandatory in perl. The parenthesis that enclose them are optional,
though. The above program could have been re-written as:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> ((<span class="Statement">substr</span> <span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">3</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
<span class="Statement">print</span> ((<span class="Statement">substr</span> <span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">1</span>, <span class="Constant">4</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
<span class="Statement">print</span> ((<span class="Statement">substr</span> <span class="Constant">&quot;</span><span class="Constant">A long string</span><span class="Constant">&quot;</span>, <span class="Constant">0</span>, <span class="Constant">6</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
</pre></div>
<p>
We need an extra set of parenthesis so <tt>print</tt> (which is also a function)
would not be confused and consider only the result of the <tt>substr</tt>
operation as its argument. If it makes no sense, then it shouldn't; however,
remember that a set of parenthesis, that wraps up the argument list
of a function, can do you no harm.
</p>
<h2>The int() function</h2>
<p>
Another useful function is int(). This function takes a number and rounds it
down to a near integer (= whole number). Here's an example:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The whole part of 5.67 is </span><span class="Constant">&quot;</span> . <span class="Statement">int</span>(<span class="Constant">5.67</span>) . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="page" id="page--expressions--strings--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--expressions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--expressions--functions--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--expressions--strings--escape--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--expressions--DIR">Expressions</a><a href="#page--expressions--strings--DIR">More about strings</a>
</div>
<h1>
3.3. More about strings
</h1>
<p>
In perl, strings and numbers are seamlessly converted into each other
depending on the context in which they are used.
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> ((<span class="Constant">&quot;</span><span class="Constant">5</span><span class="Constant">&quot;</span> + <span class="Constant">&quot;</span><span class="Constant">6</span><span class="Constant">&quot;</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
<span class="Statement">print</span> ((<span class="Constant">56</span> . <span class="Constant">23</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
</pre></div>
<p>
The output of this program is:
</p>
<div class="code_block"><pre>
11
5623
</pre></div>
<hr />

</div>
<div class="page" id="page--expressions--strings--escape--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--expressions--strings--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--expressions--strings--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--variables--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--expressions--DIR">Expressions</a><a href="#page--expressions--strings--DIR">More about strings</a><a href="#page--expressions--strings--escape--PAGE">Escape Sequences</a>
</div>
<h1>
3.3.1. Escape Sequences
</h1>
<p>
We have already encountered the <tt>\n</tt> "escape sequence" which can come
inside strings and designates a newline character. There are many others in
perl. Here is a list of the most important ones:
</p>
<ul>
<li>
<tt>\\</tt> - designates an actual backslash (<tt>\</tt>)
</li>
<li>
<tt>\"</tt> - designates an actual double-quote character (<tt>"</tt>)
</li>
<li>
<tt>\$</tt> - an actual dollar sign (a real <tt>$</tt> does something else)
</li>
<li>
<tt>\@</tt> - an actual at-sign (a non-escaped <tt>@</tt> does something else)
</li>
<li>
<tt>\n</tt> - a newline character
</li>
<li>
<tt>\r</tt> - a carriage return sign
</li>
<li>
<tt>\t</tt> - a tab character
</li>
<li>
<tt>\xDD</tt> - where "DD" are two hexadecimal digits - gives the character
whose ASCII code is "DD".
</li>
</ul>
<p>
Here's an example to illustrate some of them:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">I said </span><span class="Special">\&quot;</span><span class="Constant">hi!</span><span class="Special">\&quot;</span><span class="Constant"> to myself, and received no reply.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">This program will cost you </span><span class="Special">\$</span><span class="Constant">100 dollars.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The KDE</span><span class="Special">\\</span><span class="Constant">GNOME holy war makes life in the Linux world </span><span class="Constant">&quot;</span> .
      <span class="Constant">&quot;</span><span class="Constant">more interesting.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
whose output is:
</p>
<div class="code_block"><pre>
I said "hi!" to myself, and received no reply.
This program will cost you $100 dollars.
The KDE\GNOME holy war makes life in the Linux world more interesting.
</pre></div>
<hr />

</div>
<div class="page" id="page--variables--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--expressions--strings--escape--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--variables--plus-equal--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--variables--DIR">Variables</a>
</div>
<h1>
4. Variables
</h1>
<p>
Variables are named cells stored in the computer memory that can hold any
single Perl value. One can change the value that a variable holds, and one can
later retrieve the last value assigned as many times as wanted.
</p>
<p>Variables in Perl start with a dollar sign (<tt>$</tt>) and proceed with any
number of letters, digits and underscores (<tt>_</tt>) as long as the first
letter after the dollar is a letter or underscore.</p>
<p>To retrieve the value of a variable one simply places the variable name
(again including the dollar sign) inside an expression.</p>
<p>To assign value to a variable, one places the full variable name (including
the dollar sign) in front of an equal sign (<tt>=</tt>) and places the value
to the right of the equal sign. This form is considered a statement and should
be followed by a semicolon. The value assigned may be an experssion that may
contain other variables (including the assigned variable itself!).</p>
<p>An example will illustrate it:</p>
<div class="code_block"><pre>
<span class="Identifier">$myvar</span> = <span class="Constant">17</span>;
<span class="Identifier">$a</span> = <span class="Constant">2</span>;
<span class="Statement">print</span> <span class="Identifier">$myvar</span>, <span class="Constant">&quot;</span><span class="Constant"> * </span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Constant"> = </span><span class="Constant">&quot;</span> , (<span class="Identifier">$myvar</span>*<span class="Identifier">$a</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> = <span class="Constant">10</span>;
<span class="Statement">print</span> <span class="Identifier">$myvar</span>, <span class="Constant">&quot;</span><span class="Constant"> * </span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Constant"> = </span><span class="Constant">&quot;</span> , (<span class="Identifier">$myvar</span>*<span class="Identifier">$a</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> = <span class="Constant">75</span>;
<span class="Statement">print</span> <span class="Identifier">$myvar</span>, <span class="Constant">&quot;</span><span class="Constant"> * </span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Constant"> = </span><span class="Constant">&quot;</span> , (<span class="Identifier">$myvar</span>*<span class="Identifier">$a</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> = <span class="Constant">24</span>;
<span class="Statement">print</span> <span class="Identifier">$myvar</span>, <span class="Constant">&quot;</span><span class="Constant"> * </span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Constant"> = </span><span class="Constant">&quot;</span> , (<span class="Identifier">$myvar</span>*<span class="Identifier">$a</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
The output of this program is:
</p>
<div class="code_block"><pre>
17 * 2 = 34
17 * 10 = 170
17 * 75 = 1275
17 * 24 = 408
</pre></div>
<p>
Several things can be noticed:
</p>
<ol>
<li>
The value of <tt>$a</tt> changes throughout the program. It's perfectly fine,
and usually even necessary to modify the value of a variable.
</li>
<li>
By using <tt>$myvar</tt> we can ensure, that assuming we wish to change its
value, we will only have to change it in one place, not in every place
it appears.
</li>
</ol>
<hr />

</div>
<div class="page" id="page--variables--plus-equal--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--variables--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--variables--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--input--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--variables--DIR">Variables</a><a href="#page--variables--plus-equal--PAGE">"+=" and friends</a>
</div>
<h1>
4.1. "+=" and friends
</h1>
<p>
Perl provides a shortcut for writing "$myvar = $myvar + $value", or
"$myvar = $myvar / $value" and similar operations. Here's an example:
</p>
<div class="code_block"><pre>
<span class="Identifier">$a</span> = <span class="Constant">1</span>;
<span class="Identifier">$b</span> = <span class="Constant">0</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span> += <span class="Constant">1</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span> += <span class="Constant">1</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span> += <span class="Constant">1</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span> += <span class="Constant">1</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span> += <span class="Constant">1</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Since the operations <tt>$a += 1</tt> and <tt>$a -= 1</tt> are so commonly used,
they were also assigned a separate operator. One can use <tt>$a++</tt> and
<tt>$a--</tt> to perform them. For example, the above program could have
been written as:
</p>
<div class="code_block"><pre>
<span class="Identifier">$a</span> = <span class="Constant">1</span>;
<span class="Identifier">$b</span> = <span class="Constant">0</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span>++;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span>++;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span>++;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span>++;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$a</span> *= <span class="Constant">2</span>; <span class="Identifier">$b</span>++;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">2^</span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Constant">=</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="page" id="page--input--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--variables--plus-equal--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--for_loop--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--input--PAGE">Input</a>
</div>
<h1>
5. Input
</h1>
<p>
In order to receive a value from the user, perl supplies the <tt>&lt;&gt;</tt>
operator. When entered, this operator reads a line from the command line, and
returns it (along with the newline character).
</p>
<p>Here's an example:</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter your name:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$name</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$name</span>);
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Hello, </span><span class="Constant">&quot;</span>, <span class="Identifier">$name</span>, <span class="Constant">&quot;</span><span class="Constant">!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Notice the <tt>chomp</tt> function which strips off the trailing
newline character from the variable. You would usually want to use it,
when getting input from the user.
</p>
<p>
Here's another example:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter a string:</span><span class="Constant">&quot;</span>;
<span class="Identifier">$string</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$string</span>);
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string you entered contains </span><span class="Constant">&quot;</span>, <span class="Statement">length</span>(<span class="Identifier">$string</span>), <span class="Constant">&quot;</span><span class="Constant"> characters.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="page" id="page--for_loop--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--input--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--for_loop--PAGE">The For Loop</a>
</div>
<h1>
6. The For Loop
</h1>
<p>
The for loop enables us to iterate over a sequence of numbers and repeat
the same set of operations for each number.
</p>
<p>
For example, the following program prints all the numbers from 1 to 100:
</p>
<div class="code_block"><pre>
<span class="Statement">for</span> <span class="Identifier">$i</span> (<span class="Constant">1.</span>.<span class="Constant">100</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$i</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
Some explanations about the syntax:
</p>
<ol>
<li>
<tt>$i</tt> is the iteration variable. It receives the value 1, then the
value 2, then 3 and so forth until it is equal to 100, afterwards the
loop terminates.
</li>
<li>
The curly brackets (<tt>{ ... }</tt>) encapsulate the loop block. The loop
block is executed once for each value <tt>$i</tt> accepts. Within that
block, called the loop body, you can use <tt>$i</tt> and you'll get its
current value.
</li>
</ol>
<p>
We can nest loops, so for example the following program prints the
multiplication board:
</p>
<div class="code_block"><pre>
<span class="Statement">for</span> <span class="Identifier">$y</span> (<span class="Constant">1</span> .. <span class="Constant">10</span>)
{
    <span class="Statement">for</span> <span class="Identifier">$x</span> (<span class="Constant">1</span> .. <span class="Constant">10</span>)
    {
        <span class="Identifier">$product</span> = <span class="Identifier">$y</span>*<span class="Identifier">$x</span>;
        <span class="Comment"># Add as much whitespace as needed so the number will occupy</span>
        <span class="Comment"># exactly 4 characters.</span>
        <span class="Statement">for</span> <span class="Identifier">$whitespace</span> (<span class="Constant">1</span> .. (<span class="Constant">4</span>-<span class="Statement">length</span>(<span class="Identifier">$product</span>)))
        {
            <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant"> </span><span class="Constant">&quot;</span>;
        }
        <span class="Statement">print</span> <span class="Identifier">$product</span>;
    }
    <span class="Comment"># Move to the next line</span>
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
You may have noticed the program's comments. In perl comments start with the
sharp sign (<tt>#</tt>) and extend to the end of the line. Writing the
multiplication boards with the labels that indicate which numbers are being
multiplied is left as an exercise to the reader.
</p>
<hr />

</div>
<div class="page" id="page--conditionals--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--for_loop--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--numerical--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a>
</div>
<h1>
7. Conditionals
</h1>
<p>
Conditionals enable us to execute a group of statements if a
certain condition is met. For example the following program, reports to the
user whether or not his name starts with the letter "A":
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter your name:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$name</span> = &lt;&gt;;
<span class="Statement">if</span> (<span class="Statement">substr</span>(<span class="Identifier">$name</span>,<span class="Constant">0</span>,<span class="Constant">1</span>) <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">A</span><span class="Constant">&quot;</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name starts with 'A'!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name does not start with 'A'!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
The code <tt>substr($name,0,1) eq "A"</tt> is a condition expression which uses
the perl <tt>eq</tt> operator, which returns true if and only if the strings
are the same. There are more such operators and they will be explained shortly.
</p>
<p>
Inside the curly brackets following the <tt>if</tt> there is the code to
be executed by the conditional. That code can be as long as you like. The else
part is executed assuming the conditional was found to be false, and is
optional.
</p>
<hr />

</div>
<div class="page" id="page--conditionals--numerical--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--string--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a><a href="#page--conditionals--numerical--PAGE">Numerical Comparison Operators</a>
</div>
<h1>
7.1. Numerical Comparison Operators
</h1>
<p>
Perl supplies the user with 6 numerical comparison operators which test
for the comparison of two numbers. They are:
</p>
<table border="1" summary="">
<tr>
<td>
<tt>$a == $b</tt>
</td>
<td>
<tt>$a</tt> and <tt>$b</tt> are equal.
</td>
</tr>
<tr>
<td>
<tt>$a &gt; $b</tt>
</td>
<td>
<tt>$a</tt> is greater than <tt>$b</tt>
</td>
</tr>
<tr>
<td>
<tt>$a &lt; $b</tt>
</td>
<td>
<tt>$a</tt> is lesser than <tt>$b</tt>
</td>
</tr>
<tr>
<td>
<tt>$a &gt;= $b</tt>
</td>
<td>
<tt>$a</tt> is greater or equal to <tt>$b</tt>
</td>
</tr>
<tr>
<td>
<tt>$a &lt;= $b</tt>
</td>
<td>
<tt>$a</tt> is lesser or equal to <tt>$b</tt>
</td>
</tr>
<tr>
<td>
<tt>$a != $b</tt>
</td>
<td>
<tt>$a</tt> is not equal to <tt>$b</tt>
</td>
</tr>
</table>
<p>
Those operators can be used inside conditionals and also outside,
as part of a normal expression. The following program prints all the numbers
between 1 and 100 that are not divisable by 3:
</p>
<div class="code_block"><pre>
<span class="Statement">for</span> <span class="Identifier">$a</span> (<span class="Constant">1</span> .. <span class="Constant">100</span>)
{
    <span class="Statement">if</span> ((<span class="Identifier">$a</span> % <span class="Constant">3</span>) != <span class="Constant">0</span>)
    {
        <span class="Statement">print</span> <span class="Identifier">$a</span>,<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    }
}
</pre></div>
<hr />

</div>
<div class="page" id="page--conditionals--string--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--numerical--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--boolean--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a><a href="#page--conditionals--string--PAGE">String Comparison Operators</a>
</div>
<h1>
7.2. String Comparison Operators
</h1>
<p>
In order to compare for string equality, or if one string is alphabetically
bigger than another, you can use the six string comparison operators. Here
are the string operators together with the numerical operators they correspond
too:
</p>
<table border="1" summary="">
<tr>
<td>
<b>String Operator</b>
</td>
<td>
<b>Numerical Operator</b>
</td>
</tr>
<tr>
<td>
<tt>eq</tt>
</td>
<td>
<tt>==</tt>
</td>
</tr>
<tr>
<td>
<tt>ne</tt>
</td>
<td>
<tt>!=</tt>
</td>
</tr>
<tr>
<td>
<tt>gt</tt>
</td>
<td>
<tt>&gt;</tt>
</td>
</tr>
<tr>
<td>
<tt>lt</tt>
</td>
<td>
<tt>&lt;</tt>
</td>
</tr>
<tr>
<td>
<tt>ge</tt>
</td>
<td>
<tt>&gt;=</tt>
</td>
</tr>
<tr>
<td>
<tt>le</tt>
</td>
<td>
<tt>&lt;=</tt>
</td>
</tr>
</table>
<p>
Notice that the string operators are built from the initials of their
abbreviated names. (E.g: eq = equal, gt = greater than). Perl's string
comparison is case-sensitive. If you want a case insensitive
string comparison, use the <tt>lc</tt> function to convert the strings to
lowercase beforehand.
</p>
<p>Example:</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter your private name:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$name</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$name</span>);
<span class="Statement">if</span> (<span class="Statement">lc</span>(<span class="Identifier">$name</span>) <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">rachel</span><span class="Constant">&quot;</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name is Rachel!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name is not Rachel!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<hr />

</div>
<div class="page" id="page--conditionals--boolean--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--string--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--true_vs_false--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a><a href="#page--conditionals--boolean--PAGE">Boolean Operators</a>
</div>
<h1>
7.3. Boolean Operators
</h1>
<p>
Sometimes it is useful to check for the validation of more than one condition.
For doing this, Perl supplies boolean operators.
</p>
<table border="1" summary="">
<tr>
<td>
<h2>&amp;&amp;</h2>
<p>
<tt>$a &amp;&amp; $b</tt> evaluates to true if both <tt>$a</tt> and <tt>$b</tt> are true.
It is called the logical and of the two operands <tt>$a</tt> and <tt>$b</tt>.
</p>
</td>
</tr>
<tr>
<td>
<h2>||</h2>
<p>
<tt>$a || $b</tt> (called the logical or of <tt>$a</tt> and <tt>$b</tt>) is
evaluated to true if one or both of its operands are true.
</p>
</td>
</tr>
<tr>
<td>
<h2>!</h2>
<p>
<tt>! $a</tt> (pronounced "not <tt>$a</tt>") evaluates to true
if <tt>$a</tt> is false.
</p>
</td>
</tr>
</table>
<p>
Note that if the first operand is evaluated to false in an
<tt>&amp;&amp;</tt> operation, the second operand will not be evaluated at
all. Similiarly, if the first operand of <tt>||</tt> was found to be true,
the second one will not be evaluated either.
</p>
<p>
If you wish both operands to be evaluated at all times you need to assign
them to variables first.
</p>
<p>Here are some examples:</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter the lower bound of the range:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$lower</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$lower</span>);
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter the upper bound of the range:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$upper</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$upper</span>);
<span class="Statement">if</span> (<span class="Identifier">$lower</span> &gt; <span class="Identifier">$upper</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">This is not a valid range!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter a number:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    <span class="Identifier">$number</span> = &lt;&gt;;
    <span class="Statement">chomp</span>(<span class="Identifier">$number</span>);
    <span class="Statement">if</span> ((<span class="Identifier">$lower</span> &lt;= <span class="Identifier">$number</span>) &amp;&amp; (<span class="Identifier">$number</span> &lt;= <span class="Identifier">$upper</span>))
    {
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The number is in the range!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    }
    <span class="Statement">else</span>
    {
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The number is not in the range!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    }
}
</pre></div>
<hr />
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter your name:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$name</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$name</span>);
<span class="Identifier">$fl</span> = <span class="Statement">lc</span>(<span class="Statement">substr</span>(<span class="Identifier">$name</span>, <span class="Constant">0</span>, <span class="Constant">1</span>));
<span class="Statement">if</span> ((<span class="Identifier">$fl</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">a</span><span class="Constant">&quot;</span>)||(<span class="Identifier">$fl</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">b</span><span class="Constant">&quot;</span>)||(<span class="Identifier">$fl</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">c</span><span class="Constant">&quot;</span>))
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name starts with one of the </span><span class="Constant">&quot;</span> .
        <span class="Constant">&quot;</span><span class="Constant">first three letters of the ABC.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your name does not start with one of the </span><span class="Constant">&quot;</span> .
        <span class="Constant">&quot;</span><span class="Constant">first three letters of the ABC.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
Note: The function <tt>lc()</tt> converts a string to lowercase.
</p>
<hr />

</div>
<div class="page" id="page--conditionals--true_vs_false--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--boolean--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--conditionals--elsif--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a><a href="#page--conditionals--true_vs_false--PAGE">True Expressions vs. False Expressions</a>
</div>
<h1>
7.4. True Expressions vs. False Expressions
</h1>
<p>
In Perl every expression is considered true except for the following three
cases:
</p>
<ol>
<li>
The number <tt>0</tt>.
</li>
<li>
The empty string (<tt>""</tt>).
</li>
<li>
A special value called <tt>undef</tt>. This is the default value of every
variable that was not initialized before it was accessed.
</li>
</ol>
<p>
One of the most convenient ways of cancelling code is to wrap it in an
<tt>if (0) { ... }</tt> block. It usually is faster than adding comments to
the beginning of each line. Of course, you should not distribute code that
way, but it is useful for testing conjectures.
</p>
<hr />

</div>
<div class="page" id="page--conditionals--elsif--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--conditionals--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--true_vs_false--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--while--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--conditionals--DIR">Conditionals</a><a href="#page--conditionals--elsif--PAGE">elsif</a>
</div>
<h1>
7.5. elsif
</h1>
<p>
The full syntax of the if statement is as follows:
</p>
<div class="code_block"><pre>
<span class="Statement">if</span> (<span class="Identifier">$condition0</span>)
{
}
<span class="Statement">elsif</span> (<span class="Identifier">$condition1</span>)
{
}
<span class="Statement">elsif</span> (<span class="Identifier">$condition2</span>)
{
}
.
.
.
<span class="Statement">else</span>
{
}
</pre></div>
<p>
The N-th <tt>elsif</tt> block will be executed only if all the conditions
in the previous blocks were not met <b>and</b> its own condition was met.
</p>
<p>
You can do the same with nesting <tt>if</tt> statements inside <tt>else</tt>
statements, but isn't it nice of perl to save you all those extra brackets
and indentations?
</p>
<hr />

</div>
<div class="page" id="page--while--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--conditionals--elsif--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--while--last_and_next--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--while--DIR">The While Loop</a>
</div>
<h1>
8. The While Loop
</h1>
<p>
The <tt>while</tt> loop enables you to repeat a sequence of statements
an arbitrary number of times for as long a condition is met. The syntax
is very similar to the if statement and will be introduced in the following
example:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter a number:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$number</span>=&lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$number</span>);
<span class="Identifier">$power_of_2</span> = <span class="Constant">1</span>;
<span class="Statement">while</span> (<span class="Identifier">$power_of_2</span> &lt; <span class="Identifier">$number</span>)
{
    <span class="Identifier">$power_of_2</span> *= <span class="Constant">2</span>;
}
<span class="Statement">print</span> (<span class="Constant">&quot;</span><span class="Constant">The first power of 2 that is </span><span class="Constant">&quot;</span> .
    <span class="Constant">&quot;</span><span class="Constant">greater than this number is </span><span class="Constant">&quot;</span> , <span class="Identifier">$power_of_2</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>);
</pre></div>
<p>
It is possible that a <tt>while</tt> loop will not be executed at all, if the
condition is not met right on the start.
</p>
<hr />
<p>
The following program checks if a given string is made entirely of "a"
characters:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter a string:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$string</span>=&lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$string</span>);

<span class="Comment"># Initialize all_as to TRUE</span>
<span class="Identifier">$all_as</span> = <span class="Constant">1</span>;

<span class="Comment"># The first position in the string.</span>
<span class="Identifier">$position</span> = <span class="Constant">0</span>;

<span class="Statement">while</span> (<span class="Identifier">$all_as</span> &amp;&amp; (<span class="Identifier">$position</span> &lt; <span class="Statement">length</span>(<span class="Identifier">$string</span>)))
{
    <span class="Identifier">$char</span> = <span class="Statement">lc</span>(<span class="Statement">substr</span>(<span class="Identifier">$string</span>, <span class="Identifier">$position</span>, <span class="Constant">1</span>));

    <span class="Statement">if</span> (<span class="Identifier">$char</span> <span class="Statement">ne</span> <span class="Constant">&quot;</span><span class="Constant">a</span><span class="Constant">&quot;</span>)
    {
        <span class="Identifier">$all_as</span> = <span class="Constant">0</span>;
    }

    <span class="Comment"># Increment the position</span>
    <span class="Identifier">$position</span>++;
}

<span class="Statement">if</span> (<span class="Identifier">$all_as</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string you entered is all A's!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">At least one of the characters in the string </span><span class="Constant">&quot;</span> .
        <span class="Constant">&quot;</span><span class="Constant">you entered is not </span><span class="Special">\&quot;</span><span class="Constant">A</span><span class="Special">\&quot;</span><span class="Constant">.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<hr />

</div>
<div class="page" id="page--while--last_and_next--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--while--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--while--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--while--DIR">The While Loop</a><a href="#page--while--last_and_next--PAGE">last and next</a>
</div>
<h1>
8.1. last and next
</h1>
<p>
Within the <tt>while</tt> and <tt>for</tt> loops one can use two special
commands, called <tt>last</tt> and <tt>next</tt>. <tt>last</tt> terminates a
loop prematurely while <tt>next</tt> skips the rest of the remaining loop
body, skips to the loop condition and if it is met, executes the loop again.
</p>
<p>
By default, <tt>last</tt> and <tt>next</tt> operate on the most innermost loop.
However, the loop to which they relate can be controlled by labeling
the requested loop and specifying this label as a parameter to <tt>last</tt>
or <tt>next</tt>.
</p>
<p>
The following example is a rewrite of the "All A's" program using
<tt>last</tt>:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter a string:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$string</span>=&lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$string</span>);

<span class="Comment"># The first position in the string.</span>
<span class="Identifier">$position</span> = <span class="Constant">0</span>;

<span class="Statement">while</span> (<span class="Identifier">$position</span> &lt; <span class="Statement">length</span>(<span class="Identifier">$string</span>))
{
    <span class="Identifier">$char</span> = <span class="Statement">lc</span>(<span class="Statement">substr</span>(<span class="Identifier">$string</span>, <span class="Identifier">$position</span>, <span class="Constant">1</span>));

    <span class="Statement">if</span> (<span class="Identifier">$char</span> <span class="Statement">ne</span> <span class="Constant">&quot;</span><span class="Constant">a</span><span class="Constant">&quot;</span>)
    {
        <span class="Statement">last</span>;
    }

    <span class="Comment"># Increment the position</span>
    <span class="Identifier">$position</span>++;
}

<span class="Comment"># If the position is the end of the string it means the loop was not</span>
<span class="Comment"># terminated prematurally, so an &quot;a&quot; was not encountered.</span>
<span class="Statement">if</span> (<span class="Identifier">$position</span> == <span class="Statement">length</span>(<span class="Identifier">$string</span>))
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string you entered is all A's!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">At least one of the characters in the string </span><span class="Constant">&quot;</span> .
        <span class="Constant">&quot;</span><span class="Constant">you entered is not </span><span class="Special">\&quot;</span><span class="Constant">A</span><span class="Special">\&quot;</span><span class="Constant">.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<hr />
<p>This program prints a left-tilted pyramide:</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter the length of the pyramide:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$size</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$size</span>);

ROW_LOOP: <span class="Statement">for</span> <span class="Identifier">$row</span> (<span class="Constant">1</span> .. <span class="Identifier">$size</span>)
{
    <span class="Statement">for</span> <span class="Identifier">$column</span> (<span class="Constant">1</span> .. (<span class="Identifier">$size</span><span class="Constant">+1</span>))
    {
        <span class="Statement">if</span> (<span class="Identifier">$column</span> &gt; <span class="Identifier">$row</span>)
        {
            <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
            <span class="Statement">next</span> ROW_LOOP;
        }
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">#</span><span class="Constant">&quot;</span>;
    }
}
</pre></div>
<p>
"ROW_LOOP" is the label for the outer loop, and it can be seen that
<tt>next</tt> uses it as a parameter. All in all, <tt>next</tt> and
<tt>last</tt> are sometimes very convenient (but don't tell it to
<a href="http://en.wikipedia.org/wiki/Edsger_Dijkstra">Edsgar W.
Dijkstra</a>'s face!), so you will see them being used often.
</p>
<hr />

</div>
<div class="page" id="page--arrays--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--while--last_and_next--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--comma--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a>
</div>
<h1>
9. Arrays
</h1>
<p>
Arrays are a sequence of variables, whose members can be retrieved and
assigned to by using their indices. An index passed to an array may well be,
and usually is, another variable.
</p>
<p>
To refer to the <tt>$i</tt>'th element of the array <tt>@myarray</tt>, one uses
the syntax <tt>$myarray[$i]</tt>. This element can be assigned to or its
value can be retrieved, with the same notation.
</p>
<p>
Array indices are whole numbers and the first index is 0. As in the
length of a string, the number of elements in an array is bounded only by
the amount of available memory the computer has.
</p>
<p>
The following program prints the primes up to 200:
</p>
<div class="code_block"><pre>
<span class="Identifier">$num_primes</span> = <span class="Constant">0</span>;

<span class="Comment"># Put 2 as the first prime so we won't have an empty array,</span>
<span class="Comment"># what might confuse the interpreter</span>
<span class="Identifier">$primes</span>[<span class="Identifier">$num_primes</span>] = <span class="Constant">2</span>;
<span class="Identifier">$num_primes</span>++;

MAIN_LOOP:
<span class="Statement">for</span> <span class="Identifier">$number_to_check</span> (<span class="Constant">3</span> .. <span class="Constant">200</span>)
{
    <span class="Statement">for</span> <span class="Identifier">$p</span> (<span class="Constant">0</span> .. (<span class="Identifier">$num_primes</span><span class="Constant">-1</span>))
    {
        <span class="Statement">if</span> (<span class="Identifier">$number_to_check</span> % <span class="Identifier">$primes</span>[<span class="Identifier">$p</span>] == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Comment"># If we reached this point it means $number_to_check is not</span>
    <span class="Comment"># divisable by any prime number that came before it.</span>
    <span class="Identifier">$primes</span>[<span class="Identifier">$num_primes</span>] = <span class="Identifier">$number_to_check</span>;
    <span class="Identifier">$num_primes</span>++;
}

<span class="Statement">for</span> <span class="Identifier">$p</span> (<span class="Constant">0</span> .. (<span class="Identifier">$num_primes</span><span class="Constant">-1</span>))
{
    <span class="Statement">print</span> <span class="Identifier">$primes</span>[<span class="Identifier">$p</span>], <span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>;
}
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
The notation <tt>scalar(@myarray)</tt> can be used to refer to the number
of elements in an array. This number is equal to the maximal index which
was assigned in the array plus one. You will also see the notation
<tt>$#myarray</tt> which is equal to the maximal index itself (or -1 if the
array is empty).
</p>
<p>
Thus, for example, the above program could have been written as follows:
</p>
<div class="code_block"><pre>
<span class="Comment"># Put 2 as the first prime so we won't have an empty array,</span>
<span class="Comment"># what might confuse the interpreter</span>
<span class="Identifier">$primes</span>[<span class="Constant">0</span>] = <span class="Constant">2</span>;

MAIN_LOOP:
<span class="Statement">for</span> <span class="Identifier">$number_to_check</span> (<span class="Constant">3</span> .. <span class="Constant">200</span>)
{
    <span class="Statement">for</span> <span class="Identifier">$p</span> (<span class="Constant">0</span> .. <span class="Identifier">$#primes</span>)
    {
        <span class="Statement">if</span> (<span class="Identifier">$number_to_check</span> % <span class="Identifier">$primes</span>[<span class="Identifier">$p</span>] == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Comment"># If we reached this point it means $number_to_check is not</span>
    <span class="Comment"># divisable by any prime number that came before it.</span>
    <span class="Identifier">$primes</span>[<span class="Statement">scalar</span>(<span class="Identifier">@primes</span>)] = <span class="Identifier">$number_to_check</span>;
}

<span class="Statement">for</span> <span class="Identifier">$p</span> (<span class="Constant">0</span> .. <span class="Identifier">$#primes</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$primes</span>[<span class="Identifier">$p</span>], <span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>;
}
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="page" id="page--arrays--comma--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--negative_indexes--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--comma--PAGE">The ',' operator</a>
</div>
<h1>
9.1. The ',' operator
</h1>
<p>
In perl the comma (<tt>,</tt>) is an operator, which we encountered before
in function calls. The comma concatenates two arrays. We can use it to
initialize an array in one call:
</p>
<div class="code_block"><pre>
<span class="Identifier">@lines</span> = (<span class="Constant">&quot;</span><span class="Constant">One fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Two fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Red fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Blue fish</span><span class="Constant">&quot;</span>);

<span class="Statement">for</span> <span class="Identifier">$a</span> (<span class="Constant">0</span> .. <span class="Identifier">$#lines</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$lines</span>[<span class="Identifier">$a</span>], <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
We can also use it to concatenate two existing arrays:
</p>
<div class="code_block"><pre>
<span class="Identifier">@primes1</span> = (<span class="Constant">2</span>,<span class="Constant">3</span>,<span class="Constant">5</span>);
<span class="Identifier">@primes2</span> = (<span class="Constant">7</span>,<span class="Constant">11</span>,<span class="Constant">13</span>);
<span class="Identifier">@primes</span> = (<span class="Identifier">@primes1</span>,<span class="Identifier">@primes2</span>,<span class="Constant">17</span>);
<span class="Identifier">@primes</span> = (<span class="Identifier">@primes</span>,<span class="Constant">19</span>);

<span class="Statement">for</span> <span class="Identifier">$a</span> (<span class="Constant">0</span> .. <span class="Identifier">$#primes</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$primes</span>[<span class="Identifier">$a</span>], <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
So why it is used in function calls? In perl every function accepts an array
of arguments and returns an array of return values. That's why the comma is
useful for calling functions which accept more than one argument.
</p>
<hr />

</div>
<div class="page" id="page--arrays--negative_indexes--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--comma--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--foreach--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--negative_indexes--PAGE">Negative Indexes</a>
</div>
<h1>
9.2. Negative Indexes
</h1>
<p>
The expression <tt>$myarray[-$n]</tt> is equivalent to
<tt>$myarray[scalar(@myarray)-$n]</tt>. I.e: subscripts with negative indexes
return the <tt>$n</tt>'th element from the end of the array. So to get the
value of the last element you can write <tt>$myarray[-1]</tt> and for the
second last <tt>$myarray[-2]</tt>,
etc.
</p>
<p>
Note that one should also make sure that array subscripts that are continuously
decremented will not underflow below 0, or else one will start getting the
elements from the end of the array.
</p>
<hr />

</div>
<div class="page" id="page--arrays--foreach--DIR">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--negative_indexes--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--foreach--for_and_dotdot--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--foreach--DIR">The foreach loop</a>
</div>
<h1>
9.3. The foreach loop
</h1>
<p>
By using the <tt>foreach</tt> loop we can iterate over all the elements of
an array, and perform the same set of operations on each one of them. Here's
an example:
</p>
<div class="code_block"><pre>
<span class="Identifier">@numbers</span> = (<span class="Constant">15</span>,<span class="Constant">5</span>,<span class="Constant">7</span>,<span class="Constant">3</span>,<span class="Constant">9</span>,<span class="Constant">1</span>,<span class="Constant">20</span>,<span class="Constant">13</span>,<span class="Constant">9</span>,<span class="Constant">8</span>,
    <span class="Constant">15</span>,<span class="Constant">16</span>,<span class="Constant">2</span>,<span class="Constant">6</span>,<span class="Constant">12</span>,<span class="Constant">90</span>);

<span class="Identifier">$max</span> = <span class="Identifier">$numbers</span>[<span class="Constant">0</span>];
<span class="Identifier">$min</span> = <span class="Identifier">$numbers</span>[<span class="Constant">0</span>];

<span class="Statement">foreach</span> <span class="Identifier">$i</span> (<span class="Identifier">@numbers</span>[<span class="Constant">1.</span>.<span class="Identifier">$#numbers</span>])
{
    <span class="Statement">if</span> (<span class="Identifier">$i</span> &gt; <span class="Identifier">$max</span>)
    {
        <span class="Identifier">$max</span> = <span class="Identifier">$i</span>;
    }
    <span class="Statement">elsif</span> (<span class="Identifier">$i</span> &lt; <span class="Identifier">$min</span>)
    {
        <span class="Identifier">$min</span> = <span class="Identifier">$i</span>;
    }
}

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The maximum is </span><span class="Constant">&quot;</span> . <span class="Identifier">$max</span> . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The minimum is </span><span class="Constant">&quot;</span> . <span class="Identifier">$min</span> . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
The <tt>foreach</tt> loop in the example assigns each of the elements of the
array which was passed to it to <tt>$i</tt> in turn, and executes the same
set of commands for each value.
</p>
<hr />

</div>
<div class="page" id="page--arrays--foreach--for_and_dotdot--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--foreach--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--foreach--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--functions--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--foreach--DIR">The foreach loop</a><a href="#page--arrays--foreach--for_and_dotdot--PAGE">The for keyword and the .. operator</a>
</div>
<h1>
9.3.1. The for keyword and the .. operator
</h1>
<p>
The <tt>for</tt> keyword in Perl means exactly the same as <tt>foreach</tt> and
you can use either one interchangeably.
</p>
<p>
<tt>$a .. $b</tt> is a special operator that returns an array containing the
sequence of consecutive integers from <tt>$a</tt> up to and including
<tt>$b</tt>. Now one can fully understand the <tt>for $i (1 .. 10)</tt>
construct that we used in the beginning of this lecture.
</p>
<hr />

</div>
<div class="page" id="page--arrays--functions--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--foreach--for_and_dotdot--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--arrays--x--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--functions--PAGE">Built-In Array Functions</a>
</div>
<h1>
9.4. Built-In Array Functions
</h1>
<h2>push</h2>
<p>
The <tt>push</tt> function appends an element or an entire array to the
end of an array variable. The syntax is <tt>push @array_to_append_to, @array_to_append</tt> or <tt>push @array, $elem1</tt>. For example, the primes program from earlier could be written as:
</p>
<div class="code_block"><pre>
<span class="Comment"># Put 2 as the first prime so we won't have an empty array,</span>
<span class="Comment"># what might confuse the interpreter</span>
<span class="Identifier">@primes</span> = (<span class="Constant">2</span>);

MAIN_LOOP:
<span class="Statement">for</span> <span class="Identifier">$number_to_check</span> (<span class="Constant">3</span> .. <span class="Constant">200</span>)
{
    <span class="Statement">foreach</span> <span class="Identifier">$p</span> (<span class="Identifier">@primes</span>)
    {
        <span class="Statement">if</span> (<span class="Identifier">$number_to_check</span> % <span class="Identifier">$p</span> == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Comment"># If we reached this point it means $number_to_check is not</span>
    <span class="Comment"># divisable by any prime number that came before it.</span>
    <span class="Statement">push</span> <span class="Identifier">@primes</span>, <span class="Identifier">$number_to_check</span>;
}

<span class="Statement">foreach</span> <span class="Identifier">$p</span> (<span class="Identifier">@primes</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$p</span>, <span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>;
}
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Notice that <tt>push</tt> is equivalent to typing
<tt>@array = (@array, $extra_elem)</tt>, but it is recommended
to use it, because it minimizes error and it executes faster.
</p>
<h2>pop</h2>
<p>
<tt>pop</tt> extracts the last element from an array and returns it. Here's a
short example to demonstrate it:
</p>
<div class="code_block"><pre>
<span class="Comment"># This program prints the numbers from 10 down to 1.</span>
<span class="Identifier">@numbers</span> = (<span class="Constant">1</span> .. <span class="Constant">10</span>);
<span class="Statement">while</span>(<span class="Statement">scalar</span>(<span class="Identifier">@numbers</span>) &gt; <span class="Constant">0</span>)
{
    <span class="Identifier">$i</span> = <span class="Statement">pop</span>(<span class="Identifier">@numbers</span>);
    <span class="Statement">print</span> <span class="Identifier">$i</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<h2>shift</h2>
<p>
<tt>shift</tt> extracts the <b>first</b> element of an array and returns it. The
array will be changed to contain only the elements that were present there
previously, with the 1 to <tt>scalar(@array)-1</tt> indexes.
</p>
<p>
Here's the above example, while using <tt>shift</tt> instead of <tt>pop</tt>:
</p>
<div class="code_block"><pre>
<span class="Comment"># This program prints the numbers 1 to 10.</span>
<span class="Identifier">@numbers</span> = (<span class="Constant">1</span> .. <span class="Constant">10</span>);
<span class="Statement">while</span>(<span class="Statement">scalar</span>(<span class="Identifier">@numbers</span>) &gt; <span class="Constant">0</span>)
{
    <span class="Identifier">$i</span> = <span class="Statement">shift</span>(<span class="Identifier">@numbers</span>);
    <span class="Statement">print</span> <span class="Identifier">$i</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<h2>join</h2>
<p>
The syntax is <tt>join($separator, @array)</tt> and what it does is
concatenates the elements of <tt>@array</tt> while putting <tt>$seperator</tt> in
between. Here's an example:
</p>
<div class="code_block"><pre>
<span class="Identifier">@myarray</span> = (<span class="Constant">&quot;</span><span class="Constant">One fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Two fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Red Fish</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">Blue Fish</span><span class="Constant">&quot;</span>);

<span class="Statement">print</span> <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Identifier">@myarray</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<h2>reverse</h2>
<p>
The <tt>reverse</tt> function returns the array which contains the elements
of the array passed to it as argument in reverse. Here's an example:
</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Enter some lines:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Identifier">$line</span> = &lt;&gt;;
<span class="Statement">chomp</span>(<span class="Identifier">$line</span>);
<span class="Statement">while</span> (<span class="Identifier">$line</span>)
{
    <span class="Statement">push</span> <span class="Identifier">@mylines</span>, <span class="Identifier">$line</span>;
    <span class="Identifier">$line</span> = &lt;&gt;;
    <span class="Statement">chomp</span>(<span class="Identifier">$line</span>);
}

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Your lines in reverse are:</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Statement">reverse</span>(<span class="Identifier">@mylines</span>)), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Note that by typing <tt>scalar(reverse($scalar))</tt> you get the string that
contains the characters of <tt>$scalar</tt> in reverse.
<tt>scalar(reverse(@array))</tt> concatenates the array into one string and then
reverses its characters.
</p>
<hr />

</div>
<div class="page" id="page--arrays--x--PAGE">
<table class="page-nav-bar top" summary=""><tr><td>
<a href="#page--DIR" class="nav" accesskey="c">Contents</a></td>
<td>
<a href="#page--arrays--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--arrays--functions--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<b>Next</b></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 1</a><a href="#page--arrays--DIR">Arrays</a><a href="#page--arrays--x--PAGE">The x operator</a>
</div>
<h1>
9.5. The x operator
</h1>
<p>
The expression <tt>(@array) x $num_times</tt> returns an array that is composed
of <tt>$num_times</tt> copies of <tt>@array</tt> one after the other. The
expression <tt>$scalar x $num_times</tt>, on the other hand, returns
a string containing <tt>$num_times</tt> copies of <tt>$scalar</tt>
concatenated together string-wise.
</p>
<p>
Therefore it is important whether the left operand is wrapped in parenthesis
or not. It is usually a good idea to assign the left part to a variable
before using x so you'll have the final expression ready.
</p>
<p>Here's an example to illustrate the use:</p>
<div class="code_block"><pre>
<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Test 1:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">@myarray</span> = (<span class="Constant">&quot;</span><span class="Constant">Hello</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">World</span><span class="Constant">&quot;</span>);
<span class="Identifier">@array2</span> = ((<span class="Identifier">@myarray</span>) x <span class="Constant">5</span>);
<span class="Statement">print</span> <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>, <span class="Identifier">@array2</span>), <span class="Constant">&quot;</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Test 2:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">@array3</span> = (<span class="Identifier">@myarray</span> x <span class="Constant">5</span>);
<span class="Statement">print</span> <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>, <span class="Identifier">@array3</span>), <span class="Constant">&quot;</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Test 3:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Identifier">$string</span> = <span class="Constant">&quot;</span><span class="Constant">oncatc</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> ((<span class="Identifier">$string</span> x <span class="Constant">6</span>), <span class="Constant">&quot;</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>);

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Test 4:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, ((<span class="Constant">&quot;</span><span class="Constant">hello</span><span class="Constant">&quot;</span>) x <span class="Constant">5</span>)), <span class="Constant">&quot;</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Test 5:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, (<span class="Constant">&quot;</span><span class="Constant">hello</span><span class="Constant">&quot;</span> x <span class="Constant">5</span>)), <span class="Constant">&quot;</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Can you guess what the output of this program will be?
</p>
<p>Here's a spoiler</p>
<div class="code_block"><pre>
Test 1:
Hello, World, Hello, World, Hello, World, Hello, World, Hello, World

Test 2:
22222

Test 3:
oncatconcatconcatconcatconcatconcatc

Test 4:
hello
hello
hello
hello
hello

Test 5:
hellohellohellohellohello
</pre></div>
<hr />

</div>

</body></html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.