Source

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

Full commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
<?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 2
</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--for--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a>
</div>
<h1>
"Perl for Perl Newbies" - Part 2
</h1>
<h2 id="contents">Contents</h2>
<ul class="contentsmain">
<li><a href="#page--for--DIR" class="contents">1. The for Loop</a>
<ul class="contents">
<li><a href="#page--for--next--PAGE" class="contents">1.1. Behaviour of next in the for Loop</a>
</li><li><a href="#page--for--whence_for--PAGE" class="contents">1.2. Whence for?</a>
</li></ul>
</li><li><a href="#page--hashes--DIR" class="contents">2. Hashes</a>
<ul class="contents">
<li><a href="#page--hashes--functions--PAGE" class="contents">2.1. Hash-Related Functions</a>
</li></ul>
</li><li><a href="#page--my--DIR" class="contents">3. Declaring Local Variables with "my"</a>
<ul class="contents">
<li><a href="#page--my--use_strict--PAGE" class="contents">3.1. "use strict", Luke!</a>
</li><li><a href="#page--my--use_warnings--PAGE" class="contents">3.2. "use warnings", Luke!</a>
</li></ul>
</li><li><a href="#page--functions--DIR" class="contents">4. Functions</a>
<ul class="contents">
<li><a href="#page--functions--recursion--PAGE" class="contents">4.1. Function Recursion</a>
</li><li><a href="#page--functions--shift--PAGE" class="contents">4.2. Use of "shift" in Functions</a>
</li></ul>
</li><li><a href="#page--files--DIR" class="contents">5. File Input/Output</a>
<ul class="contents">
<li><a href="#page--files--print--PAGE" class="contents">5.1. Using "print" with Files</a>
</li><li><a href="#page--files--readline--PAGE" class="contents">5.2. The &lt;FILEHANDLE&gt; Operator</a>
</li></ul>
</li><li><a href="#page--argv--PAGE" class="contents">6. The @ARGV Array</a>
</li><li><a href="#page--regexps--DIR" class="contents">7. Regular Expressions</a>
<ul class="contents">
<li><a href="#page--regexps--syntax--PAGE" class="contents">7.1. Syntax</a>
</li><li><a href="#page--regexps--multi_char--PAGE" class="contents">7.2. ".", "[ ... ]"</a>
</li><li><a href="#page--regexps--asterisk--PAGE" class="contents">7.3. The "*" and Friends</a>
</li><li><a href="#page--regexps--grouping--PAGE" class="contents">7.4. Grouping</a>
</li><li><a href="#page--regexps--alternation--PAGE" class="contents">7.5. Alternation with "|"</a>
</li><li><a href="#page--regexps--binding--PAGE" class="contents">7.6. Binding to the Beginning or the End of a String</a>
</li><li><a href="#page--regexps--substitute--DIR" class="contents">7.7. Substituting using Regular Expressions</a>
<ul class="contents">
<li><a href="#page--regexps--substitute--e_switch--PAGE" class="contents">7.7.1. The "e" Switch</a>
</li><li><a href="#page--regexps--substitute--ungreedy--PAGE" class="contents">7.7.2. Ungreedy Matching with *? and Friends</a>
</li></ul>
</li><li><a href="#page--regexps--flags--PAGE" class="contents">7.8. Useful Flags</a>
</li><li><a href="#page--regexps--esc_seqs--PAGE" class="contents">7.9. Useful Escape Sequences</a>
</li><li><a href="#page--regexps--next_step--PAGE" class="contents">7.10. The Next Step</a>
</li></ul>
</li><li><a href="#page--interpolation--PAGE" class="contents">8. String Interpolation</a>
</li><li><a href="#page--useful_funcs--DIR" class="contents">9. Useful Functions</a>
<ul class="contents">
<li><a href="#page--useful_funcs--split--PAGE" class="contents">9.1. split</a>
</li><li><a href="#page--useful_funcs--map--PAGE" class="contents">9.2. map</a>
</li><li><a href="#page--useful_funcs--sort--DIR" class="contents">9.3. sort</a>
<ul class="contents">
<li><a href="#page--useful_funcs--sort--cmp--PAGE" class="contents">9.3.1. &lt;=&gt; and cmp</a>
</li></ul>
</li><li><a href="#page--useful_funcs--grep--PAGE" class="contents">9.4. grep</a>
</li></ul>
</li><li><a href="#page--references--DIR" class="contents">10. References</a>
<ul class="contents">
<li><a href="#page--references--hanoi--PAGE" class="contents">10.1. Example: The Towers of Hanoi</a>
</li><li><a href="#page--references--backslash--PAGE" class="contents">10.2. \ - Taking a Reference to an Existing Variable</a>
</li><li><a href="#page--references--square_brackets--PAGE" class="contents">10.3. [ @array ] - a Dynamic Reference to an Array</a>
</li><li><a href="#page--references--curly_brackets--PAGE" class="contents">10.4. { %hash } - a Dynamic Reference to a Hash</a>
</li><li><a href="#page--references--arrow--PAGE" class="contents">10.5. The Arrow Operators</a>
</li><li><a href="#page--references--dereferencing--PAGE" class="contents">10.6. Dereferencing</a>
</li></ul>
</li><li><a href="#page--debugger--DIR" class="contents">11. Using the perl Debugger</a>
<ul class="contents">
<li><a href="#page--debugger--stepping--PAGE" class="contents">11.1. Stepping over and Stepping in</a>
</li><li><a href="#page--debugger--breakpoints--PAGE" class="contents">11.2. Setting Breakpoints and Continuing</a>
</li><li><a href="#page--debugger--perl_cmds--PAGE" class="contents">11.3. Executing Perl Commands inside the Debugger</a>
</li><li><a href="#page--debugger--more_info--PAGE" class="contents">11.4. Getting More Information</a>
</li></ul>
</li><li><a href="#page--finale--PAGE" class="contents">12. Finale</a>
</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--for--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--for--next--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--for--DIR">The for Loop</a>
</div>
<h1>
1. The for Loop
</h1>
<p>
The <tt>for</tt> loop has another syntax which can be used to encapsulate
<tt>while</tt>-like loops in a more compact way, which also has some
advantages.
</p>
<p>
The syntax for this <tt>for</tt> loop is
<tt>for({Initial Statements} ; {Condition} ; {Iteration Commands}) { ... }</tt>,
where {Condition} is a boolean condition expression, and
{Initial Statements} and {Iteration Commands} may contain one or more
commands separated by commas.
</p>
<p>
When The interpreter encounters a <tt>for</tt> loop it executes its initial
statements, and then executes the commands within its block for as long
as the condition is met. {Iteration Commands} are executed at the end
of each iteration.
</p>
<p>
The following simple example prints the multiplication board:
</p>
<div class="code_block"><pre>
<span class="Statement">for</span>(<span class="Identifier">$row</span> = <span class="Constant">1</span> ; <span class="Identifier">$row</span> &lt;= <span class="Constant">10</span> ; <span class="Identifier">$row</span>++)
{
    <span class="Statement">for</span>(<span class="Identifier">$column</span> = <span class="Constant">1</span> ; <span class="Identifier">$column</span> &lt;= <span class="Constant">10</span> ; <span class="Identifier">$column</span>++)
    {
        <span class="Identifier">$result</span> = <span class="Identifier">$row</span>*<span class="Identifier">$column</span>;
        <span class="Identifier">$pad</span> = ( <span class="Constant">&quot;</span><span class="Constant"> </span><span class="Constant">&quot;</span>  x  (<span class="Constant">4</span>-<span class="Statement">length</span>(<span class="Identifier">$result</span>)) );
        <span class="Statement">print</span> <span class="Identifier">$pad</span>, <span class="Identifier">$result</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--for--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--for--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--for--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--for--whence_for--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--for--DIR">The for Loop</a> → <a href="#page--for--next--PAGE">Behaviour of next in the for Loop</a>
</div>
<h1>
1.1. Behaviour of next in the for Loop
</h1>
<p>
An important difference between the <tt>for</tt> loop and the <tt>while</tt>
loop is the behaviour of <tt>next</tt> inside it. In a <tt>for</tt> loop,
<tt>next</tt> skips the rest of the iteration but still executes the
iteration commands which were passed to the loop as arguments.
</p>
<p>
For example, the following program prints the first 200 primes:
</p>
<div class="code_block"><pre>
MAIN_LOOP: <span class="Statement">for</span>(
    <span class="Identifier">@primes</span>=(<span class="Constant">2</span>), <span class="Identifier">$i</span>=<span class="Constant">3</span> ;
    <span class="Statement">scalar</span>(<span class="Identifier">@primes</span>) &lt; <span class="Constant">200</span> ;
    <span class="Identifier">$i</span>++
    )
{
    <span class="Statement">foreach</span> <span class="Identifier">$p</span> (<span class="Identifier">@primes</span>)
    {
        <span class="Statement">if</span> (<span class="Identifier">$i</span> % <span class="Identifier">$p</span> == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Statement">push</span> <span class="Identifier">@primes</span>, <span class="Identifier">$i</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">@primes</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Now, the equivalent program using a while loop:
</p>
<div class="code_block"><pre>
<span class="Identifier">@primes</span> = (<span class="Constant">2</span>);
<span class="Identifier">$i</span> = <span class="Constant">3</span>;
MAIN_LOOP:
<span class="Statement">while</span> (<span class="Statement">scalar</span>(<span class="Identifier">@primes</span>) &lt; <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">$i</span> % <span class="Identifier">$p</span> == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Statement">push</span> <span class="Identifier">@primes</span>, <span class="Identifier">$i</span>;
    <span class="Identifier">$i</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">@primes</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
gets stuck. (<b>why?</b>)
</p>
<hr />

</div>
<div class="page" id="page--for--whence_for--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--for--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--for--next--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--hashes--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--for--DIR">The for Loop</a> → <a href="#page--for--whence_for--PAGE">Whence for?</a>
</div>
<h1>
1.2. Whence for?
</h1>
<p>
So far we have learned about three different kinds of for loops, so
you are probably wondering when to use each one.
</p>
<p>
A subtantial difference between the notation
<tt>for($a=0 ; $a&lt;$b ; $a++)</tt> and between
<tt>for $a (0 .. ($b-1))</tt> is that the first accommodates
itself to changes in <tt>$b</tt>, while the second will iterate for a
constant number of times. Of course, you can make the first one
similar to the second by assigning the value of <tt>$b</tt> to a third
variable that will remain constant. Still, the second notation is
usually safer from that sense.
</p>
<p>
The <tt>foreach $item (@array) </tt> loop is quite handy, but sometimes
when iterating over an array of items the index of the item is of importance.
In that case, one may prefer to use a normal <tt>for</tt> loop.
</p>
<hr />

</div>
<div class="page" id="page--hashes--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--whence_for--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--hashes--functions--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--hashes--DIR">Hashes</a>
</div>
<h1>
2. Hashes
</h1>
<p>
Hashes can be used to map a set of keys, each to his own value. Using a
hash one can retrieve the value associated with each key, as well as get
a list of all the keys present in the hash.
</p>
<p>
To assign or retrieve the value of the key <tt>$mykey</tt> in the hash
<tt>$myhash</tt> one uses the <tt>$myhash{$mykey}</tt> convention. To check if a
key exists in a hash one should type <tt>exists($myhash{$mykey})</tt>
which in turn returns a boolean value that corresponds to its existence.
</p>
<p>
An array whose elements are the keys present in the hash can be retrieved
by typing <tt>keys(%myhash)</tt>. Here's a short example, that runs a simple
bank, that will illustrate this functionality:
</p>
<div class="code_block"><pre>
<span class="Comment"># Initialize the valid operations collection</span>
<span class="Identifier">$ops</span>{<span class="Constant">'</span><span class="Constant">create</span><span class="Constant">'</span>} = <span class="Constant">1</span>;
<span class="Identifier">$ops</span>{<span class="Constant">'</span><span class="Constant">deposit</span><span class="Constant">'</span>} = <span class="Constant">1</span>;
<span class="Identifier">$ops</span>{<span class="Constant">'</span><span class="Constant">status</span><span class="Constant">'</span>} = <span class="Constant">1</span>;
<span class="Identifier">$ops</span>{<span class="Constant">'</span><span class="Constant">exit</span><span class="Constant">'</span>} = <span class="Constant">2</span>;

<span class="Statement">while</span> (<span class="Constant">1</span>)
{
    <span class="Comment"># Get a valid input from the user.</span>
    <span class="Statement">while</span> (<span class="Constant">1</span>)
    {
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Please enter what you want to do:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">(</span><span class="Constant">&quot;</span> . <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Constant">, </span><span class="Constant">&quot;</span>, <span class="Statement">keys</span>(<span class="Identifier">%ops</span>)) . <span class="Constant">&quot;</span><span class="Constant">)</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

        <span class="Identifier">$function</span> = &lt;&gt;;
        <span class="Statement">chomp</span>(<span class="Identifier">$function</span>);

        <span class="Statement">if</span> (<span class="Statement">exists</span>(<span class="Identifier">$ops</span>{<span class="Identifier">$function</span>}))
        {
            <span class="Statement">last</span>;
        }
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Unknown function!</span><span class="Special">\n</span><span class="Constant"> Please try again.</span><span class="Special">\n\n</span><span class="Constant">&quot;</span>
    }

    <span class="Statement">if</span> (<span class="Identifier">$function</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">exit</span><span class="Constant">&quot;</span>)
    {
        <span class="Statement">last</span>;
    }

    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Enter the name of the account:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    <span class="Identifier">$account</span> = &lt;&gt;;
    <span class="Statement">chomp</span>(<span class="Identifier">$account</span>);
    <span class="Statement">if</span> (<span class="Identifier">$function</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">create</span><span class="Constant">&quot;</span>)
    {
        <span class="Statement">if</span> (! <span class="Statement">exists</span>(<span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>}))
        {
            <span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>} = <span class="Constant">0</span>;
        }
    }
    <span class="Statement">elsif</span> (<span class="Identifier">$function</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">status</span><span class="Constant">&quot;</span>)
    {
        <span class="Statement">if</span> (! <span class="Statement">exists</span> (<span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>}) )
        {
            <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Error! The account does not exist.</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">There are </span><span class="Constant">&quot;</span> . <span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>} .
                <span class="Constant">&quot;</span><span class="Constant"> NIS in the account.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
        }
    }
    <span class="Statement">elsif</span> (<span class="Identifier">$function</span> <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">deposit</span><span class="Constant">&quot;</span>)
    {
        <span class="Statement">if</span> (<span class="Statement">exists</span>(<span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>}) )
        {
            <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">How much do you wish to deposit?</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
            <span class="Identifier">$how_much</span> = &lt;&gt;;
            <span class="Statement">chomp</span>(<span class="Identifier">$how_much</span>);
            <span class="Identifier">$bank_accounts</span>{<span class="Identifier">$account</span>} += <span class="Identifier">$how_much</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 following example, which is considerably shorter, uses a hash to
find out if a list of strings contains only unique strings:
</p>
<div class="code_block"><pre>
<span class="Statement">while</span>(<span class="Identifier">$string</span> = &lt;&gt;)
{
    <span class="Statement">chomp</span>(<span class="Identifier">$string</span>);
    <span class="Statement">if</span> (<span class="Statement">exists</span>(<span class="Identifier">$myhash</span>{<span class="Identifier">$string</span>}))
    {
        <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string </span><span class="Special">\&quot;</span><span class="Constant">&quot;</span> . <span class="Identifier">$string</span> . <span class="Constant">&quot;</span><span class="Special">\&quot;</span><span class="Constant"> was already encountered!</span><span class="Constant">&quot;</span>;
        <span class="Statement">last</span>;
    }
    <span class="Statement">else</span>
    {
        <span class="Identifier">$myhash</span>{<span class="Identifier">$string</span>} = <span class="Constant">1</span>;
    }
}
</pre></div>
<hr />

</div>
<div class="page" id="page--hashes--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--hashes--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--hashes--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--my--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--hashes--DIR">Hashes</a> → <a href="#page--hashes--functions--PAGE">Hash-Related Functions</a>
</div>
<h1>
2.1. Hash-Related Functions
</h1>
<h2>delete</h2>
<p>
delete can be used to remove a key or a set of keys out of a hash.
For example:
</p>
<div class="code_block"><pre>
<span class="Identifier">$myhash</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">$myhash</span>{<span class="Constant">&quot;</span><span class="Constant">perl</span><span class="Constant">&quot;</span>} = <span class="Constant">&quot;</span><span class="Constant">TMTOWTDI</span><span class="Constant">&quot;</span>;
<span class="Identifier">$myhash</span>{<span class="Constant">&quot;</span><span class="Constant">shlomi</span><span class="Constant">&quot;</span>} = <span class="Constant">&quot;</span><span class="Constant">fish</span><span class="Constant">&quot;</span>;

<span class="Statement">if</span> (<span class="Statement">exists</span>(<span class="Identifier">$myhash</span>{<span class="Constant">&quot;</span><span class="Constant">perl</span><span class="Constant">&quot;</span>}))
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The key perl exists!</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 key perl does not exist!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}

<span class="Statement">delete</span>(<span class="Identifier">$myhash</span>{<span class="Constant">&quot;</span><span class="Constant">perl</span><span class="Constant">&quot;</span>});

<span class="Statement">if</span> (<span class="Statement">exists</span>(<span class="Identifier">$myhash</span>{<span class="Constant">&quot;</span><span class="Constant">perl</span><span class="Constant">&quot;</span>}))
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The key perl exists!</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 key perl does not exist!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<h2>The Comma Regarding Hashes</h2>
<p>
The comma can be used to combine two arrays into one larger one. Given the
fact that a mini-hash with one key and one value can be specified using the
<tt>$key =&gt; $value</tt> notation (which is essentially equivalent to
<tt>$key, $value</tt>) a hash can be initialized in one statement.</p>
<p>
Here's an example:
</p>
<div class="code_block"><pre>
<span class="Identifier">%hash1</span> = (
    <span class="Constant">&quot;</span><span class="Constant">shlomi</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">fish</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">orr</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">dunkelman</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">guy</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">keren</span><span class="Constant">&quot;</span>
    );

<span class="Identifier">%hash2</span> = (
    <span class="Constant">&quot;</span><span class="Constant">george</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">washington</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">jules</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">verne</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">isaac</span><span class="Constant">&quot;</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">newton</span><span class="Constant">&quot;</span>
    );

<span class="Identifier">%combined</span> = (<span class="Identifier">%hash1</span>, <span class="Identifier">%hash2</span>);

<span class="Statement">foreach</span> <span class="Identifier">$key</span> (<span class="Statement">keys</span>(<span class="Identifier">%combined</span>))
{
    <span class="Statement">print</span> <span class="Identifier">$key</span>, <span class="Constant">&quot;</span><span class="Constant"> = </span><span class="Constant">&quot;</span>, <span class="Identifier">$combined</span>{<span class="Identifier">$key</span>}, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
If the two combined hashes contain several identical keys, then the values
of the latter hash will win.
</p>
<hr />

</div>
<div class="page" id="page--my--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--hashes--functions--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--my--use_strict--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--my--DIR">Declaring Local Variables with "my"</a>
</div>
<h1>
3. Declaring Local Variables with "my"
</h1>
<p>
Variables can be made local to their own scope by declaring them
using a keyword called <tt>my</tt>. When the interpreter leaves the
scope, the variable will be restored to its original value.
</p>
<p>
Here's an example:
</p>
<div class="code_block"><pre>
<span class="Identifier">$a</span> = <span class="Constant">5</span>;
<span class="Identifier">$b</span> = <span class="Constant">1000</span>;
{
    <span class="Statement">my</span> (<span class="Identifier">$b</span>);
    <span class="Statement">for</span>(<span class="Identifier">$b</span>=<span class="Constant">0</span>;<span class="Identifier">$b</span>&lt;<span class="Constant">10</span>;<span class="Identifier">$b</span>++)
    {
        <span class="Statement">print</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Constant">*</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="Identifier">$b</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">Now, </span><span class="Special">\$</span><span class="Constant">b is </span><span class="Constant">&quot;</span>, <span class="Identifier">$b</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
If you wish to declare more than one variable as local you should use
a set of parenthesis surrounding the variable list. For example:
<tt>my (@array, $scalar1, %hash, $scalar2);</tt>. If you want to declare only
one variable, then they are optional.
</p>
<hr />

</div>
<div class="page" id="page--my--use_strict--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--my--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--my--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--my--use_warnings--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--my--DIR">Declaring Local Variables with "my"</a> → <a href="#page--my--use_strict--PAGE">"use strict", Luke!</a>
</div>
<h1>
3.1. "use strict", Luke!
</h1>
<p>
Perl has a pragma (= an interpreter-related directive) known as
<tt>use strict;</tt>, which among other things, makes sure all the variables
you use will be declared with <tt>my</tt>. If you reference a variable
that was not declared with <tt>my</tt> it will generate an error.
</p>
<p>
Using <tt>use strict</tt> is in most cases a good idea, because it minimizes
the number of errors because of typos. Just type <tt>use strict;</tt> at the
beginning of your program and you can sleep better at nights.
</p>
<p>
As an example, here is the primes program, <tt>use strict</tt>-ed:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;

<span class="Statement">my</span> (<span class="Identifier">@primes</span>, <span class="Identifier">$i</span>);

MAIN_LOOP: <span class="Statement">for</span>(
    <span class="Identifier">@primes</span>=(<span class="Constant">2</span>), <span class="Identifier">$i</span>=<span class="Constant">3</span> ;
    <span class="Statement">scalar</span>(<span class="Identifier">@primes</span>) &lt; <span class="Constant">200</span> ;
    <span class="Identifier">$i</span>++
    )
{
    <span class="Statement">foreach</span> <span class="Statement">my</span> <span class="Identifier">$p</span> (<span class="Identifier">@primes</span>)
    {
        <span class="Statement">if</span> (<span class="Identifier">$i</span> % <span class="Identifier">$p</span> == <span class="Constant">0</span>)
        {
            <span class="Statement">next</span> MAIN_LOOP;
        }
    }

    <span class="Statement">push</span> <span class="Identifier">@primes</span>, <span class="Identifier">$i</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">@primes</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Notice the use of <tt>my</tt> in the declaration of the <tt>foreach</tt> loop.
Such just-in-time declarations, inspired by C++, are possible in perl.
</p>
<hr />

</div>
<div class="page" id="page--my--use_warnings--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--my--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--my--use_strict--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--functions--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--my--DIR">Declaring Local Variables with "my"</a> → <a href="#page--my--use_warnings--PAGE">"use warnings", Luke!</a>
</div>
<h1>
3.2. "use warnings", Luke!
</h1>
<p>
There is another pragma which is called and typed as <tt>use warnings;</tt>,
that causes the interpreter to emit many warnings to the command window in
case you're doing wrong things (like using <tt>undef</tt> in an expression).
It is also very useful to declare it at the beginning of the program, in
addition to <tt>use strict;</tt>. So, from now on, our programs will
include it as well.
</p>
<hr />

</div>
<div class="page" id="page--functions--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--my--use_warnings--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--functions--recursion--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--functions--DIR">Functions</a>
</div>
<h1>
4. Functions
</h1>
<p>
We already encountered some of Perl's built-in functions. Perl enables us to
define our own functions using Perl code. Whenever you use a piece of code
more than once in a program, it is a good idea to make it into a function.
That way, you won't have to change it in more than one place.
</p>
<p>
In Perl, every function accepts an array of arguments and returns an array of
return values. The arguments (also known as "parameters") can be found in the
<tt>@_</tt> variable. This variable is magical and need not and should not be
declared with <tt>my</tt>. In order to return values from a function, one can
use the <tt>return</tt> keyword.
</p>
<p>
To declare a function use type <tt>sub function_name {</tt> at the beginning
and a <tt>}</tt> at the end. Everything in between, is the function body.
</p>
<p>
Here's an example, for a function that returns the maximum and the
minimum of an array of numbers.
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">min_and_max</span>
{
    <span class="Statement">my</span> (<span class="Identifier">@numbers</span>);

    <span class="Identifier">@numbers</span> = <span class="Identifier">@_</span>;

    <span class="Statement">my</span> (<span class="Identifier">$min</span>, <span class="Identifier">$max</span>);

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

    <span class="Statement">foreach</span> <span class="Statement">my</span> <span class="Identifier">$i</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">return</span> (<span class="Identifier">$min</span>, <span class="Identifier">$max</span>);
}

<span class="Statement">my</span> (<span class="Identifier">@test_array</span>, <span class="Identifier">@ret</span>);
<span class="Identifier">@test_array</span> = (<span class="Constant">123</span>, <span class="Constant">23</span> , <span class="Constant">-6</span>, <span class="Constant">7</span> , <span class="Constant">80</span>, <span class="Constant">300</span>, <span class="Constant">45</span>, <span class="Constant">2</span>, <span class="Constant">9</span>, <span class="Constant">1000</span>, <span class="Constant">-90</span>, <span class="Constant">3</span>);

<span class="Identifier">@ret</span> = min_and_max(<span class="Identifier">@test_array</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">$ret</span>[<span class="Constant">0</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 maximum is </span><span class="Constant">&quot;</span>, <span class="Identifier">$ret</span>[<span class="Constant">1</span>], <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
And here's another one for a function that calculates the hypotenuse of a
right triangle according to its other sides:
</p>
<div class="code_block"><pre>
<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">pythagoras</span>
{
    <span class="Statement">my</span> (<span class="Identifier">$x</span>, <span class="Identifier">$y</span>);

    (<span class="Identifier">$x</span>, <span class="Identifier">$y</span>) = <span class="Identifier">@_</span>;

    <span class="Statement">return</span> <span class="Statement">sqrt</span>(<span class="Identifier">$x</span>**<span class="Constant">2</span>+<span class="Identifier">$y</span>**<span class="Constant">2</span>);
}

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The hypotenuse of a right triangle with sides 3 and 4 is </span><span class="Constant">&quot;</span>,
    pythagoras(<span class="Constant">3</span>,<span class="Constant">4</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--functions--recursion--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--functions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--functions--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--functions--shift--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--functions--DIR">Functions</a> → <a href="#page--functions--recursion--PAGE">Function Recursion</a>
</div>
<h1>
4.1. Function Recursion
</h1>
<p>
Functions can call other functions. In fact, a function can call itself,
either directly or indirectly. When a function calls itself it is known as
<b>recursion</b>, a Computer Science methodology which can be implemented
with or without functions.
</p>
<p>
Here's an example where recursion is used to find all the permutations of
splitting 10 among three numbers:
</p>
<div class="code_block"><pre>
<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">mysplit</span>
{
    <span class="Statement">my</span> (<span class="Identifier">$total</span>, <span class="Identifier">$num_elems</span>, <span class="Identifier">@accum</span>) = <span class="Identifier">@_</span>;
    <span class="Statement">my</span> (<span class="Identifier">@ret</span>, <span class="Identifier">@new_accum</span>);


    <span class="Statement">if</span> (<span class="Identifier">$num_elems</span> == <span class="Constant">1</span>)
    {
        <span class="Statement">push</span> <span class="Identifier">@accum</span>, <span class="Identifier">$total</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">@accum</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

        <span class="Statement">return</span>;
    }

    <span class="Statement">for</span>(<span class="Statement">my</span> <span class="Identifier">$a</span>=<span class="Constant">0</span>;<span class="Identifier">$a</span>&lt;=<span class="Identifier">$total</span>;<span class="Identifier">$a</span>++)
    {
        <span class="Identifier">@new_accum</span> = (<span class="Identifier">@accum</span>, <span class="Identifier">$a</span>);
        mysplit(<span class="Identifier">$total</span>-<span class="Identifier">$a</span>, <span class="Identifier">$num_elems</span><span class="Constant">-1</span>, <span class="Identifier">@new_accum</span>);
    }
}

mysplit(<span class="Constant">10</span>,<span class="Constant">3</span>);
</pre></div>
<p>
A couple of notes are in place. First of all, perl does not handle tail
recursion very well, at least not in the current incarnation of the compiler.
If your recursion can be done using a simple loop, do it with it.
</p>
<p>
Secondly, some systems (such as Microsft Windows) limit an executable to a
certain amount of stack, as far as such langauges as Assembler or C are
concerned. This should not be of any concern to perl hackers, because the
perl interpreter does not translate a perl function call into a C function
call. (not to mention that the perl interpreter on those systems is compiled
with enough stack for itself).
</p>
<p>
Sometimes, recursion is helpful, but if you see that your recursion is getting
too deep, you should consider using your own dedicated stack (which can be
implemented as an array) instead. It's a good programming practice.
</p>
<hr />

</div>
<div class="page" id="page--functions--shift--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--functions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--functions--recursion--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--files--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--functions--DIR">Functions</a> → <a href="#page--functions--shift--PAGE">Use of "shift" in Functions</a>
</div>
<h1>
4.2. Use of "shift" in Functions
</h1>
<p>
One can use the <tt>shift</tt> function to extract arguments out of the
argument array. Since this use is so common, then simply typing <tt>shift</tt>
without any arguments will do exactly that.
</p>
<p>
Here is the split program from the previous slide, which was
re-written using <tt>shift</tt>:
</p>
<div class="code_block"><pre>
<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">mysplit</span>
{
    <span class="Statement">my</span> <span class="Identifier">$total</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">$num_elems</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">@accum</span> = <span class="Identifier">@_</span>;
    <span class="Statement">my</span> (<span class="Identifier">@ret</span>, <span class="Identifier">@new_accum</span>);


    <span class="Statement">if</span> (<span class="Identifier">$num_elems</span> == <span class="Constant">1</span>)
    {
        <span class="Statement">push</span> <span class="Identifier">@accum</span>, <span class="Identifier">$total</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">@accum</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

        <span class="Statement">return</span>;
    }

    <span class="Statement">for</span>(<span class="Statement">my</span> <span class="Identifier">$a</span>=<span class="Constant">0</span>;<span class="Identifier">$a</span>&lt;=<span class="Identifier">$total</span>;<span class="Identifier">$a</span>++)
    {
        <span class="Identifier">@new_accum</span> = (<span class="Identifier">@accum</span>, <span class="Identifier">$a</span>);
        mysplit(<span class="Identifier">$total</span>-<span class="Identifier">$a</span>, <span class="Identifier">$num_elems</span><span class="Constant">-1</span>, <span class="Identifier">@new_accum</span>);
    }
}

mysplit(<span class="Constant">10</span>,<span class="Constant">3</span>);
</pre></div>
<hr />

</div>
<div class="page" id="page--files--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--functions--shift--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--files--print--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--files--DIR">File Input/Output</a>
</div>
<h1>
5. File Input/Output
</h1>
<p>
By now you are probably wondering how perl can be used to interact with
the external world, and this is where <b>File Input/Output</b> enters the
frame.
</p>
<p>
In Perl, file I/O is handled by using sessions: you are opening a file for
reading or writing (or both), do with it what you want, and then close it.
In Perl, filehandles implemented as the so-called <tt>globs</tt> are placed on
a separate namespace than that of the variables. It is generally marked with
a starting asterik (<tt>*</tt>), which can be omitted if the first letter is a
capital one.
</p>
<p>
To open a file use the <tt>open MyFileHandle, $mode, $file_path;</tt>
notation, and to close a file use the <tt>close(MyFileHandle);</tt> notation.
The <tt>$mode</tt> determines whether the file will be
open for reading, writing, appending or some of them. The following table
should give you a quick reference:
</p>
<table border="1" summary="">
<tr>
<td>
&gt;
</td>
<td>
Writing (the original file will be erased before the function starts).
</td>
</tr>
<tr>
<td>
&lt; (or nothing)
</td>
<td>
Reading
</td>
</tr>
<tr>
<td>
&gt;&gt;
</td>
<td>
Appending (the file pointer will start at the end and the file will not be
overriden)
</td>
</tr>
<tr>
<td>
+&lt;
</td>
<td>
Read-write, or just write without truncating.
</td>
</tr>
</table>
<p>
<tt>$file_path</tt> is the pathname of the file to open relative
to the script current working directory (<b>CWD</b>). For instance, the
command <tt>open I, "&lt;", "../hello.txt";</tt> opens the file "hello.txt"
found a directory below the current directory to reading.
</p>
<hr />

</div>
<div class="page" id="page--files--print--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--files--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--files--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--files--readline--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--files--DIR">File Input/Output</a> → <a href="#page--files--print--PAGE">Using "print" with Files</a>
</div>
<h1>
5.1. Using "print" with Files
</h1>
<p>
The print command which we encountered before can also be used to send output
to files. In fact, the screen itself is a special filehandle, whose name is
<tt>STDOUT</tt>, but since its use is so common, perl allows it to be omitted.
</p>
<p>
The syntax of printing to a file is
<tt>print File $string1, $string2, ... </tt>. Here's a short example that
prepares a file with a pyramid in it:
</p>
<div class="code_block"><pre>
<span class="PreProc">#!/usr/bin/perl</span>

<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$pyramid_side</span> = <span class="Constant">20</span>;

<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;</span><span class="Constant">&gt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">pyramid.txt</span><span class="Constant">&quot;</span>;
<span class="Statement">for</span>(<span class="Identifier">$a</span>=<span class="Constant">1</span> ; <span class="Identifier">$a</span> &lt;= <span class="Identifier">$pyramid_side</span> ; <span class="Identifier">$a</span>++)
{
    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Constant">&quot;</span><span class="Constant">X</span><span class="Constant">&quot;</span> x <span class="Identifier">$a</span>;
    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">close</span>(<span class="Identifier">O</span>);
</pre></div>
<p>
In order to print to more than one file at once, one needs to use two
separate print statements. Here's an example, that prints to one file the
sequnce 1, 1.1, 2, 2.1, 3, 3.1... and to the other the sequence 1, 1.5, 2, 2.5
, 3, 3.5...
</p>
<div class="code_block"><pre>
<span class="Statement">open</span> <span class="Identifier">SEQ1</span>, <span class="Constant">&quot;</span><span class="Constant">&gt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">seq1.txt</span><span class="Constant">&quot;</span>;
<span class="Statement">open</span> <span class="Identifier">SEQ2</span>, <span class="Constant">&quot;</span><span class="Constant">&gt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">seq2.txt</span><span class="Constant">&quot;</span>;

<span class="Statement">for</span>(<span class="Identifier">$a</span>=<span class="Constant">0</span>;<span class="Identifier">$a</span>&lt;<span class="Constant">100</span>;<span class="Identifier">$a</span>++)
{
    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> (<span class="Identifier">$a</span><span class="Constant">+0.1</span>);
    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> (<span class="Identifier">$a</span><span class="Constant">+0.5</span>);
    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}

<span class="Statement">close</span>(<span class="Identifier">SEQ1</span>);
<span class="Statement">close</span>(<span class="Identifier">SEQ2</span>);
</pre></div>
<hr />

</div>
<div class="page" id="page--files--readline--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--files--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--files--print--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--argv--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--files--DIR">File Input/Output</a> → <a href="#page--files--readline--PAGE">The &lt;FILEHANDLE&gt; Operator</a>
</div>
<h1>
5.2. The &lt;FILEHANDLE&gt; Operator
</h1>
<p>
Just like <tt>print</tt> can be generalized to files, so can the
<tt>&lt;&gt;</tt> which we encountered before. If you place the name of the
filehandle inside the operator, it will read a line from the file opened by
that filehandle.
</p>
<p>
Here's an example, let's append the line numbers to a given file:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> (<span class="Identifier">$line_num</span>, <span class="Identifier">$line</span>);

<span class="Identifier">$line_num</span> = <span class="Constant">0</span>;
<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;</span><span class="Constant">&lt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">input.txt</span><span class="Constant">&quot;</span>;
<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;</span><span class="Constant">&gt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">output.txt</span><span class="Constant">&quot;</span>;

<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;I&gt;)
{
    <span class="Comment"># We aren't chomping it so we won't lose the newline.</span>
    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Identifier">$line_num</span>, <span class="Constant">&quot;</span><span class="Constant">: </span><span class="Constant">&quot;</span>, <span class="Identifier">$line</span>;
    <span class="Identifier">$line_num</span>++;
}

<span class="Statement">close</span> (<span class="Identifier">I</span>);
<span class="Statement">close</span> (<span class="Identifier">O</span>);
</pre></div>
<p>
And the following example counts the number of lines in a file that start with
"A":
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> (<span class="Identifier">$filename</span>, <span class="Identifier">$lines_num</span>, <span class="Identifier">$line</span>, <span class="Identifier">$c</span>);

<span class="Identifier">$lines_num</span> = <span class="Constant">0</span>;
<span class="Identifier">$filename</span> = <span class="Constant">&quot;</span><span class="Constant">input.txt</span><span class="Constant">&quot;</span>;
<span class="Statement">open</span> <span class="Identifier">I</span>,  <span class="Constant">&quot;</span><span class="Constant">&lt;</span><span class="Constant">&quot;</span>, <span class="Identifier">$filename</span>;
<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;I&gt;)
{
    <span class="Identifier">$c</span> = <span class="Statement">substr</span>(<span class="Identifier">$line</span>, <span class="Constant">0</span>, <span class="Constant">1</span>);
    <span class="Statement">if</span> (<span class="Statement">lc</span>(<span class="Identifier">$c</span>) <span class="Statement">eq</span> <span class="Constant">&quot;</span><span class="Constant">a</span><span class="Constant">&quot;</span>)
    {
        <span class="Identifier">$lines_num</span>++;
    }
}
<span class="Statement">close</span>(<span class="Identifier">I</span>);

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">In </span><span class="Constant">&quot;</span> , <span class="Identifier">$filename</span>, <span class="Constant">&quot;</span><span class="Constant"> there are </span><span class="Constant">&quot;</span>,
    <span class="Identifier">$lines_num</span>, <span class="Constant">&quot;</span><span class="Constant"> lines that start with </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>
<p>
The <tt>join("", &lt;FILEHANDLE&gt;)</tt> command returns the entire contents
of the file from the current position onwards, and may prove to be useful.
Examples for it will be given in the next section where regular expressions
will be taught.
</p>
<hr />

</div>
<div class="page" id="page--argv--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--files--readline--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--argv--PAGE">The @ARGV Array</a>
</div>
<h1>
6. The @ARGV Array
</h1>
<p>
One can use the <tt>@ARGV</tt> variable to access the command line arguments
passed to the perl script at the command line. Here's an example, that makes a
backup of the file specified as its argument:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Identifier">$ARGV</span>[<span class="Constant">0</span>];

<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;</span><span class="Constant">&lt;</span><span class="Constant">&quot;</span>, <span class="Identifier">$filename</span>;
<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;</span><span class="Constant">&gt;</span><span class="Constant">&quot;</span>, <span class="Identifier">$filename</span>.<span class="Constant">&quot;</span><span class="Constant">.bak</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">O</span> <span class="Statement">join</span>(<span class="Constant">&quot;&quot;</span>,&lt;I&gt;);
<span class="Statement">close</span>(<span class="Identifier">I</span>);
<span class="Statement">close</span>(<span class="Identifier">O</span>);
</pre></div>
<p>
Using the command-line for specifying parameters to the program is usually
more handy than using files, or modifying the script each time.
</p>
<p>
Note that it is often convenient to use <tt>shift</tt> to draw arguments
out of <tt>@ARGV</tt> one at a time. When used without parameters outside of
functions, shift extract arguments out of <tt>@ARGV</tt>.
</p>
<hr />

</div>
<div class="page" id="page--regexps--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--argv--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--syntax--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a>
</div>
<h1>
7. Regular Expressions
</h1>
<p>
Regular expressions are one of the most powerful features of Perl, but are
also one of those that beginners find most difficult to understand. Don't let
that fact discourage you: they are definitely worth your time.
</p>
<p>
Basically, a regular expression tells the interpreter to search a string
for a pattern. The pattern can be a simple one like the word "hello" or a
complex one, such as the word "hello" followed by any number of instances of
the word "one", followed by the word "triple". The latter will be
written as <tt>hello(?:one)*triple</tt> inside a regular expression.
</p>
<p>
The perl operators allow you to do three things with a regular expression:
</p>
<ol>
<li>
Check if it exists in the string at all.
</li>
<li>
Retrieve various captures out of it.
</li>
<li>
Retrieve all of its occurences.
</li>
<li>
Substitute the first occurence or all of its occurences with some other string
or (perl) expression.
</li>
</ol>
<p>
Some perl functions such as <tt>split</tt> and <tt>grep</tt> (which will be
covered later) can utilize regular expressions to do other tasks.
</p>
<hr />

</div>
<div class="page" id="page--regexps--syntax--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--multi_char--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--syntax--PAGE">Syntax</a>
</div>
<h1>
7.1. Syntax
</h1>
<p>
The syntax for checking if a string contains the regular expression is
<tt>$string =~ /$regexp/</tt>. For instance the following program checks if
a given string contains the word hello (in all lowercase):
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>(<span class="Identifier">@ARGV</span>);

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Constant">hello</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string contains the word </span><span class="Special">\&quot;</span><span class="Constant">hello</span><span class="Special">\&quot;</span><span class="Constant">.</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 string does not contain the word </span><span class="Special">\&quot;</span><span class="Constant">hello</span><span class="Special">\&quot;</span><span class="Constant">.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
Note that alphanumeric characters in regular expressions stand for themselves,
and that behaviour is guaranteed not to change in further versions of perl.
Other characters may need to be prefixed with a backslash (<tt>\</tt>) if
placed inside a regular expression.
</p>
<hr />

</div>
<div class="page" id="page--regexps--multi_char--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--syntax--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--asterisk--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--multi_char--PAGE">".", "[ ... ]"</a>
</div>
<h1>
7.2. ".", "[ ... ]"
</h1>
<p>
In this slide we will learn how to specify any character or that a character
will be one of a range of several possible characters.
</p>
<h2>The "." stands for any character</h2>
<p>
By putting a <tt>.</tt> character inside a regular expression, it means
that it can match any character, excluding a newline. For example,
the following snippet matches 5 letter words that start with 'l' and end with
'x':
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">lc</span>(<span class="Statement">shift</span>(<span class="Identifier">@ARGV</span>));

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Constant">l</span><span class="Special">...</span><span class="Constant">x</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">True</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">False</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<h2>The [ ... ] specifies more than one option for a character</h2>
<p>
When square brackets appear, one can specify more than one character inside
them as option for matching. If the first character is <tt>^</tt> then they
will match everything that is <b>not</b> one of the characters.
</p>
<p>
One can specify a range of characters with the hyphen. For example the pattern
<tt>[a-zA-Z0-9_]</tt> matches every alpha-numeric character.
</p>
<p>
Here's an example that checks if a valid identifier for a perl variable is
present in the string:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">lc</span>(<span class="Statement">shift</span>(<span class="Identifier">@ARGV</span>));

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Special">\$</span><span class="Special">[A-Za-z_]</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">True</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">False</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<hr />

</div>
<div class="page" id="page--regexps--asterisk--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--multi_char--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--grouping--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--asterisk--PAGE">The "*" and Friends</a>
</div>
<h1>
7.3. The "*" and Friends
</h1>
<p>
In order to repeat a character more than one time several repetition
paterns can be used. The asterisk (<tt>*</tt>) can be used to indicate that
the character should repeat zero or more times. The plus sign (<tt>+</tt>)
can be used to indicate that the character should repeat one or more
times. As for the question mark (<tt>?</tt>) it indicates an appearance of
zero or one time.
</p>
<p>
The following pattern <tt>/ab*c/</tt> matches "a" followed by as many
times of b as we like and then c. The pattern <tt>/you k?now/</tt> matches
both "you know" and "you now". The pattern <tt>/hello +world/</tt> matches
the word "hello" followed by the word "world". (with some whitespace in
between).
</p>
<hr />

</div>
<div class="page" id="page--regexps--grouping--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--asterisk--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--alternation--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--grouping--PAGE">Grouping</a>
</div>
<h1>
7.4. Grouping
</h1>
<p>
One can use the cluster grouping notation (<tt>(?: ... )</tt>) or the
capture grouping notation (<tt>( ... )</tt>) to group several characters so
the entire group can be repeated with <tt>+</tt>, <tt>*</tt> or <tt>?</tt>.
</p>
<p>
The difference between clustering and capturing, is that with capturing the
Perl interpreter also keeps track of the text matched by the captures in the
<tt>$1</tt>, <tt>$2</tt>, <tt>$3</tt>, etc. variables. Clustering as a result
is faster and less intrusive.
</p>
<p>
For example, the following perl program sees if its argument is an entire
sentence that starts with "the" and ends with "there":
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">lc</span>(<span class="Statement">shift</span>(<span class="Identifier">@ARGV</span>));

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Constant">the</span><span class="Special">(?:</span><span class="Constant"> </span><span class="Special">+[a-z]+)*</span><span class="Constant"> </span><span class="Special">+</span><span class="Constant">there</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">True</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">False</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
It is possible to nest groupings, so for example the following matches a
square brackets-enclosed semicolon separated list of curly braces-enclosed
comma-separated lists of numbers:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">lc</span>(<span class="Statement">shift</span>(<span class="Identifier">@ARGV</span>));

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Special">\[\{</span><span class="Special">[0-9]+(?:</span><span class="Constant">,</span><span class="Special">[0-9]+)+</span><span class="Special">\}</span><span class="Special">(?:</span><span class="Constant">;</span><span class="Special">\{</span><span class="Special">(?:[0-9]+(?:</span><span class="Constant">,</span><span class="Special">[0-9]+)+)</span><span class="Special">\}</span><span class="Special">)+</span><span class="Special">\]</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">True</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">False</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
So it matches strings like <tt>[{54,129};{236,78}]</tt> and
<tt>[{54,129};{236,78};{78,100,808};{72,1009,8}]</tt>
</p>
<hr />

</div>
<div class="page" id="page--regexps--alternation--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--grouping--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--binding--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--alternation--PAGE">Alternation with "|"</a>
</div>
<h1>
7.5. Alternation with "|"
</h1>
<p>
One can specify several possible matching for an expression to match with
<tt>|</tt>. For instance, <tt>dog|cat|mouse</tt> will match either "dog" or
"cat" or "mouse". But naturally the options can themselves be regular
expression which are as complex as perl supports.
</p>
<p>
The alternation operator will try to match as much of the expression as it can,
so it is recommended to use grouping to bound it.
(e.g: <tt>(?:dog|cat|mouse)</tt>)
</p>
<p>
The following pattern matches a sentence that may contain numbers:
</p>
<div class="code_block"><pre>
(?:[a-zA-Z]+|[0-9]+)(?: +(?:[a-zA-Z]+|[0-9]+))*
</pre></div>
<hr />

</div>
<div class="page" id="page--regexps--binding--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--alternation--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--substitute--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--binding--PAGE">Binding to the Beginning or the End of a String</a>
</div>
<h1>
7.6. Binding to the Beginning or the End of a String
</h1>
<p>
In a regular expression a caret (<tt>^</tt>) at the beginning of the expression
matches the beginning of the string and a dollar sign (<tt>$</tt>) at the
<b>end</b> of the expression matches the end of the string.
</p>
<p>
For example, the following program checks if a string is composed entirely of
the letter A:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Constant">^</span><span class="Special">[Aa]*</span><span class="Constant">$</span><span class="Statement">/</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">The string you entered is not all A's.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
It's much shorter than with using a loop and also much faster.
</p>
<p>
Here's another example. Let's check if a string ends with three periods:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Statement">if</span> (<span class="Identifier">$string</span> =~<span class="Statement"> /</span><span class="Special">\.\.\.</span><span class="Constant"> </span><span class="Special">*</span><span class="Constant">$</span><span class="Statement">/</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">The string you entered ends with an ellipsis.</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 string you entered does not end with an ellipsis.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<hr />

</div>
<div class="page" id="page--regexps--substitute--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--binding--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--substitute--e_switch--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--substitute--DIR">Substituting using Regular Expressions</a>
</div>
<h1>
7.7. Substituting using Regular Expressions
</h1>
<p>
One can use regular expression to substitute an occurence
of a regular expression found inside a string to something else. The operator
used for this is <tt>s/$exp_to_search/$exp_to_subs_for/</tt>. The following
simple program replaces a number with the string "Some Number":
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Identifier">$string</span> =~ <span class="Statement">s/</span><span class="Special">[0-9]+</span><span class="Statement">/</span><span class="Constant">Some Number</span><span class="Statement">/</span>;

<span class="Statement">print</span> <span class="Identifier">$string</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Notice that in this case, only the first occurence of the string will be
substituted. The <tt>g</tt> switch which will be covered soon, enables us to
substitute <b>all</b> of the occurences.
</p>
<p>
One can include the captures that are matched in the expression as part of
the substitution string. The first capture is <tt>$1</tt>, the
second <tt>$2</tt> etc.
</p>
<p>
Here's an example which reverses the order of two words at the beginning of a
sentence:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Identifier">$string</span> =~ <span class="Statement">s/</span><span class="Constant">^</span><span class="Special">([A-Za-z]+)</span><span class="Constant"> </span><span class="Special">*([A-Za-z]+)</span><span class="Statement">/</span><span class="Identifier">$2</span><span class="Constant"> </span><span class="Identifier">$1</span><span class="Statement">/</span>;

<span class="Statement">print</span> <span class="Identifier">$string</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<table class="note" summary="">
<tr class="note">
<td class="note">
<h3 class="note">Note</h3>
<p>
The <tt>$1</tt>, <tt>$2</tt> variables are also available after the regular
expression match or substitution is performed, and can be used inside
your perl code.
</p>
</td>
</tr>
</table>
<hr />

</div>
<div class="page" id="page--regexps--substitute--e_switch--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--regexps--substitute--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--substitute--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--substitute--ungreedy--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--substitute--DIR">Substituting using Regular Expressions</a> → <a href="#page--regexps--substitute--e_switch--PAGE">The "e" Switch</a>
</div>
<h1>
7.7.1. The "e" Switch
</h1>
<p>
If an <tt>e</tt> is appended to the end of the substitution command,
then the right side is treated as a normal Perl expression, giving you
the ability to use operators and functions.
</p>
<p>
The following example, replaces the first word in the sentence with its length:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Identifier">$string</span> =~ <span class="Statement">s/</span><span class="Constant">^</span><span class="Special">([A-Za-z]+)</span><span class="Statement">/</span><span class="Constant">length(</span><span class="Identifier">$1</span><span class="Constant">)</span><span class="Statement">/e</span>;

<span class="Statement">print</span> <span class="Identifier">$string</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
And the following example, converts the first word that starts with the letter
"S" into uppercase:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Identifier">$string</span> =~ <span class="Statement">s/</span><span class="Special">([Ss][A-Za-z]*)</span><span class="Statement">/</span><span class="Constant">'&lt;'.uc(</span><span class="Identifier">$1</span><span class="Constant">).'&gt;'</span><span class="Statement">/e</span>;

<span class="Statement">print</span> <span class="Identifier">$string</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--regexps--substitute--ungreedy--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--regexps--substitute--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--substitute--e_switch--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--flags--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--substitute--DIR">Substituting using Regular Expressions</a> → <a href="#page--regexps--substitute--ungreedy--PAGE">Ungreedy Matching with *? and Friends</a>
</div>
<h1>
7.7.2. Ungreedy Matching with *? and Friends
</h1>
<p>
By defualt, the regular expression repetition operators such as
<tt>*</tt>, <tt>+</tt> and friends are greedy. Hence, they will try to
match as much as possible of the string as they can.
</p>
<p>
If you wish them to match as little as possible, append a question mark
(<tt>?</tt>) after them. Here's an example:
</p>
<div class="code_block"><pre>
<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Constant">&quot;</span><span class="Constant">&lt;html&gt;Hello&lt;/html&gt;You&lt;/html&gt;</span><span class="Constant">&quot;</span>;

<span class="Statement">my</span> <span class="Identifier">$greedy</span> = <span class="Identifier">$string</span>;
<span class="Identifier">$greedy</span> =~ <span class="Statement">s/</span><span class="Constant">&lt;html&gt;</span><span class="Special">.*</span><span class="Constant">&lt;</span><span class="Special">\/</span><span class="Constant">html&gt;</span><span class="Statement">/</span><span class="Constant">REPLACED</span><span class="Statement">/</span>;

<span class="Statement">my</span> <span class="Identifier">$ungreedy</span> = <span class="Identifier">$string</span>;
<span class="Identifier">$ungreedy</span> =~ <span class="Statement">s/</span><span class="Constant">&lt;html&gt;</span><span class="Special">.*?</span><span class="Constant">&lt;</span><span class="Special">\/</span><span class="Constant">html&gt;</span><span class="Statement">/</span><span class="Constant">REPLACED</span><span class="Statement">/</span>;

<span class="Statement">print</span> <span class="Identifier">$string</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>,
    <span class="Identifier">$greedy</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>,
    <span class="Identifier">$ungreedy</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--regexps--flags--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--substitute--ungreedy--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--esc_seqs--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--flags--PAGE">Useful Flags</a>
</div>
<h1>
7.8. Useful Flags
</h1>
<p>
There are other flags besides <tt>e</tt> that can be appended to the end
of the match or substitution.
</p>
<p>
An <tt>i</tt> following the regular expression call, causes a case-insensitive
operation on the string. Thus, for example, an "A" will match both "a" and "A".
Note that the strings placed in <tt>$1</tt>, <tt>$2</tt> and friends will still
retain their original case.
</p>
<p>
A <tt>g</tt> causes the match or substitution to match <b>all</b> occurences,
not just one. If used with a match in an array context
(e.g: <tt>@occurences = ($string =~ /$regexp/g);</tt>) it retrieves all
the matches and if used with a substitution it substitutes all the
occurences with the string.
</p>
<p>
This example replaces all the occurences of the word "hello" by the index of
their occurence:
</p>
<div class="code_block"><pre>
<span class="PreProc">use strict</span>;
<span class="PreProc">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$index</span> = <span class="Constant">0</span>;
<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">put_index</span>
{
    <span class="Identifier">$index</span>++;
    <span class="Statement">return</span> <span class="Identifier">$index</span>;
}

<span class="Statement">my</span> <span class="Identifier">$string</span> = <span class="Statement">shift</span>;

<span class="Identifier">$string</span> =~ <span class="Statement">s/</span><span class="Constant">hello</span><span class="Statement">/</span><span class="Constant">put_index()</span><span class="Statement">/gei</span>;

<span class="Statement">print</span> <span class="Identifier">$string</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--regexps--esc_seqs--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--flags--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--regexps--next_step--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--esc_seqs--PAGE">Useful Escape Sequences</a>
</div>
<h1>
7.9. Useful Escape Sequences
</h1>
<p>
Perl has several escape sequences which are specific to regular expressions.
Here is a list of some of the more useful ones:
</p>
<ul>
<li>
<tt>\w</tt> - matches a word character, equivalent to <tt>[A-Za-z_]</tt>. The
corresponding sequence - <tt>\W</tt> matches a non-word character.
</li>
<li>
<tt>\s</tt> - matches a whitespace character, usually space and tab. <tt>\S</tt>
matches a non-whitespace character.
</li>
<li>
<tt>\d</tt> - matches a digit. Equivalent to <tt>[0-9]</tt>. <tt>\D</tt> matches a
non-digit.
</li>
<li>
<tt>\b</tt> - matches a word boundary. This sequence in fact has a zero width
but it is nevertheless highly useful. <tt>\B</tt> matches something that is not
a word boundary.
</li>
</ul>
<hr />

</div>
<div class="page" id="page--regexps--next_step--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--regexps--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--regexps--esc_seqs--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--interpolation--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--regexps--DIR">Regular Expressions</a> → <a href="#page--regexps--next_step--PAGE">The Next Step</a>
</div>
<h1>
7.10. The Next Step
</h1>
<p>
This was just an introduction to perl's regular expressions. There is much
more available functionality, and I am referring you to the following sources
for more information:
</p>
<ul>
<li>
<a href="http://perldoc.perl.org/perlsyn.html">The perlsyn document</a>.
</li>
<li>
<a href="http://perldoc.perl.org/perlrequick.html">perlrequick</a> (the regular
expressions' quick start),
<a href="http://perldoc.perl.org/perlretut.html">perlretut</a> (regular
expressions tutorial) and
<a href="http://perldoc.perl.org/perlreref.html">perlreref</a> (regular
expressions reference).
</li>
<li>
<a href="http://perldoc.perl.org/perlre.html">The perlre document</a>. The
bugs section of it says that it "varies from difficult to understand to
completely and utterly opaque".
</li>
<li>
<a href="http://perldoc.perl.org/perlfaq6.html">The perlfaq6 document</a>
that deals with regular expressions.
</li>
<li>
The book <a href="http://www.oreilly.com/catalog/regex2/"><i>Mastering Regular Expressions</i> by Jeffrey Friedl</a>, published
by <a href="http://www.oreilly.com/">O'Reilly and Associates</a>.
</li>
</ul>
<hr />

</div>
<div class="page" id="page--interpolation--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--regexps--next_step--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--interpolation--PAGE">String Interpolation</a>
</div>
<h1>
8. String Interpolation
</h1>
<p>
Perl supports inserting variables into string constants simply by
placing their name along with the dollars inside them. Here's an example:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$name</span>;
<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="Identifier">$name</span><span class="Constant">!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Note that perl will try to match as much as possible from the variable name,
even if a variable by that name does not exist. Thus if you write <tt>$ab</tt>
inside a string, it will not take <tt>$a</tt> and append "b" to it! To overcome
this limitation, you can limit the variable name using curly braces:
<tt>"Hello ${boy}and${girl}"</tt>.
</p>
<p>
In any case, interpolation is especially useful for building regular
expression, since the string may contain control characters.
</p>
<hr />

</div>
<div class="page" id="page--useful_funcs--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--interpolation--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--split--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a>
</div>
<h1>
9. Useful Functions
</h1>
<p>
Perl has many functions that allow batch operations on entire arrays. Those
functions are not only convenient, but are also faster than unrolling them
into <tt>for</tt> and <tt>foreach</tt> loops.
</p>
<p>
This behaviour is sometimes referred to as the <b>perl pipeline</b> because
it resembles the pipeline used by the UNIX shells.
</p>
<ul class="contentsmain">
<li><a href="#page--useful_funcs--split--PAGE" class="contents">9.1. split</a>
</li><li><a href="#page--useful_funcs--map--PAGE" class="contents">9.2. map</a>
</li><li><a href="#page--useful_funcs--sort--DIR" class="contents">9.3. sort</a>
<ul class="contents">
<li><a href="#page--useful_funcs--sort--cmp--PAGE" class="contents">9.3.1. &lt;=&gt; and cmp</a>
</li></ul>
</li><li><a href="#page--useful_funcs--grep--PAGE" class="contents">9.4. grep</a>
</li></ul>
<hr />

</div>
<div class="page" id="page--useful_funcs--split--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--useful_funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--useful_funcs--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--map--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a> → <a href="#page--useful_funcs--split--PAGE">split</a>
</div>
<h1>
9.1. split
</h1>
<p>
The <tt>split</tt> function can be used to split a string according to
a regular expression. The syntax of the split function is as follows:
</p>
<p>
<tt>@components = split(/$regexp/, $string);</tt>
</p>
<p>
Here's a simple example that retrieves the user-id of a given
username:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> (<span class="Identifier">$line</span>, <span class="Identifier">@parts</span>);

<span class="Statement">my</span> <span class="Identifier">$user_name</span> = <span class="Statement">shift</span>;

<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;</span><span class="Constant">&lt;</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;</span><span class="Constant">/etc/passwd</span><span class="Constant">&quot;</span>;
<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;I&gt;)
{
    <span class="Identifier">@parts</span> = <span class="Statement">split</span>(<span class="Statement">/</span><span class="Constant">:</span><span class="Statement">/</span>, <span class="Identifier">$line</span>);
    <span class="Statement">if</span> (<span class="Identifier">$parts</span>[<span class="Constant">0</span>] <span class="Statement">eq</span> <span class="Identifier">$user_name</span>)
    {
        <span class="Statement">print</span> <span class="Identifier">$user_name</span> . <span class="Constant">&quot;</span><span class="Constant">'s user ID is </span><span class="Constant">&quot;</span> . <span class="Identifier">$parts</span>[<span class="Constant">2</span>] . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
        <span class="Statement">exit</span>(<span class="Constant">0</span>);
    }
}
<span class="Statement">close</span>(<span class="Identifier">I</span>);

<span class="Statement">print</span> <span class="Identifier">$user_name</span> . <span class="Constant">&quot;</span><span class="Constant">'s user ID was not found!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">exit</span>(<span class="Constant">-1</span>);
</pre></div>
<p>
The following code splits a sentence into words and prints them:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$sentence</span> = <span class="Statement">shift</span>;

<span class="Statement">my</span> <span class="Identifier">@words</span> = <span class="Statement">split</span>(<span class="Statement">/</span><span class="Special">\s</span><span class="Special">+</span><span class="Statement">/</span>, <span class="Identifier">$sentence</span>);

<span class="Statement">my</span> <span class="Identifier">$i</span>;
<span class="Statement">for</span>(<span class="Identifier">$i</span>=<span class="Constant">0</span>;<span class="Identifier">$i</span>&lt;<span class="Statement">scalar</span>(<span class="Identifier">@words</span>);<span class="Identifier">$i</span>++)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Identifier">$i</span><span class="Constant">: </span><span class="Constant">&quot;</span> . <span class="Identifier">$words</span>[<span class="Identifier">$i</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--useful_funcs--map--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--useful_funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--useful_funcs--split--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--sort--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a> → <a href="#page--useful_funcs--map--PAGE">map</a>
</div>
<h1>
9.2. map
</h1>
<p>
The map function traverses an array and maps each element to one or more (
or zero) elements in a new array. It has a rather unorthodox syntax in that
it is written as
<tt>@new_array = (map { &lt;Some Expression with $_&gt; } @array)</tt>
.
</p>
<p>
For each element of the array @array, the variable <tt>$_</tt> is assigned
its value, and within the curly brackets one can put an expression that is
dependant on it.
</p>
<p>
The following example multiplies each element of an array by two:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">@array</span> = (<span class="Constant">20</span>, <span class="Constant">3</span>, <span class="Constant">1</span>, <span class="Constant">9</span>, <span class="Constant">100</span>, <span class="Constant">88</span>, <span class="Constant">75</span>);

<span class="Statement">my</span> <span class="Identifier">@new_array</span> = (<span class="Statement">map</span> { <span class="Identifier">$_</span>*<span class="Constant">2</span>; } <span class="Identifier">@array</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">@new_array</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Using map is usually faster than using a
<tt>foreach $elem (@array) { ... push @new_array, $new_elem; }</tt>
loop, at least when the function performed is a relatively uncomplex one.
</p>
<p>
The following program decodes a run-length encoding compression, in which each
element of the array is a number followed by its number of times:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$default_input_string</span> = <span class="Constant">&quot;</span><span class="Constant">2-5,3-9,1-2,8-1,4-7,5-9,20-3,16-9</span><span class="Constant">&quot;</span>;

<span class="Statement">my</span> <span class="Identifier">$input_string</span> = <span class="Statement">shift</span> || <span class="Identifier">$default_input_string</span>;

<span class="Comment"># RLE stands for Run-Length Encoding</span>
<span class="Statement">my</span> <span class="Identifier">@rle_components</span> = <span class="Statement">split</span>(<span class="Statement">/</span><span class="Constant">,</span><span class="Statement">/</span>, <span class="Identifier">$input_string</span>);
<span class="Statement">my</span> <span class="Identifier">@expanded_sequence</span> = (<span class="Statement">map</span>
    {
        <span class="Statement">my</span> (<span class="Identifier">$what</span>, <span class="Identifier">$times</span>) = <span class="Statement">split</span>(<span class="Statement">/</span><span class="Constant">-</span><span class="Statement">/</span>, <span class="Identifier">$_</span>);
        ((<span class="Identifier">$what</span>) x <span class="Identifier">$times</span>);
    }
    <span class="Identifier">@rle_components</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">@expanded_sequence</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
As you can see, the expression at the end of the map iterator can be an
array with more than one element. It can also be an empty array, which means
that some elements can be filtered out.
</p>
<hr />

</div>
<div class="page" id="page--useful_funcs--sort--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--useful_funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--useful_funcs--map--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--sort--cmp--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a> → <a href="#page--useful_funcs--sort--DIR">sort</a>
</div>
<h1>
9.3. sort
</h1>
<p>
The <tt>sort</tt> function can sort an array based on a comparison expression.
As with the <tt>map</tt> function, this expression can be as complex as you'd
like and may actually include a call to a dedicated function.
</p>
<p>
Within the comparison block, the variables <tt>$a</tt> and <tt>$b</tt> indicate
the two elements to be compared. If the expression returns a negative value
it means that <tt>$a</tt> should precede <tt>$b</tt>. If it's positive, it means
that <tt>$b</tt> should come before <tt>$a</tt>. If it's zero it indicates that
it does not matter which one will come first.
</p>
<p>
The following example sorts an array of integers numerically:
</p>
<div class="code_block"><pre>
<span class="Statement">my</span> <span class="Identifier">@array</span> = (<span class="Constant">100</span>,<span class="Constant">5</span>,<span class="Constant">8</span>,<span class="Constant">92</span>,-<span class="Constant">7</span>,<span class="Constant">34</span>,<span class="Constant">29</span>,<span class="Constant">58</span>,<span class="Constant">8</span>,<span class="Constant">10</span>,<span class="Constant">24</span>);

<span class="Statement">my</span> <span class="Identifier">@sorted_array</span> =
    (<span class="Statement">sort</span>
        {
            <span class="Statement">if</span> (<span class="Identifier">$a</span> &lt; <span class="Identifier">$b</span>)
            {
                <span class="Statement">return</span> -<span class="Constant">1</span>;
            }
            <span class="Statement">elsif</span> (<span class="Identifier">$a</span> &gt; <span class="Identifier">$b</span>)
            {
                <span class="Statement">return</span> <span class="Constant">1</span>;
            }
            <span class="Statement">else</span>
            {
                <span class="Statement">return</span> <span class="Constant">0</span>;
            }
        }
        <span class="Identifier">@array</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">@sorted_array</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--useful_funcs--sort--cmp--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--useful_funcs--sort--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--useful_funcs--sort--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--useful_funcs--grep--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a> → <a href="#page--useful_funcs--sort--DIR">sort</a> → <a href="#page--useful_funcs--sort--cmp--PAGE">&lt;=&gt; and cmp</a>
</div>
<h1>
9.3.1. &lt;=&gt; and cmp
</h1>
<p>
Perl has two operators <tt>&lt;=&gt;</tt> and <tt>cmp</tt>, which are very
useful when wishing to sort arrays. <tt>$a &lt;=&gt; $b</tt> returns -1 if
<tt>$a</tt> is numerically lesser than <tt>$b</tt>, 1 if it's greater, and
zero if they are equal.
</p>
<p>
<tt>cmp</tt> does the same for string comparison. For instance the previous
example could be re-written as:
</p>
<div class="code_block"><pre>
<span class="Statement">my</span> <span class="Identifier">@array</span> = (<span class="Constant">100</span>,<span class="Constant">5</span>,<span class="Constant">8</span>,<span class="Constant">92</span>,-<span class="Constant">7</span>,<span class="Constant">34</span>,<span class="Constant">29</span>,<span class="Constant">58</span>,<span class="Constant">8</span>,<span class="Constant">10</span>,<span class="Constant">24</span>);

<span class="Statement">my</span> <span class="Identifier">@sorted_array</span> = (<span class="Statement">sort {</span> <span class="Identifier">$a</span> &lt;=&gt; <span class="Identifier">$b</span> <span class="Statement">}</span> <span class="Identifier">@array</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">@sorted_array</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Much more civil, isn't it? The following example, sorts an array of strings
in reverse:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;

<span class="Statement">my</span> <span class="Identifier">@input</span> = (
    <span class="Constant">&quot;</span><span class="Constant">Hello World!</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">You is all I need.</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">To be or not to be</span><span class="Constant">&quot;</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="Constant">Absolutely Fabulous</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">Ci vis pacem, para belum</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">Give me liberty or give me death.</span><span class="Constant">&quot;</span>,
    <span class="Constant">&quot;</span><span class="Constant">Linux - Because software problems should not cost money</span><span class="Constant">&quot;</span>,
);

<span class="Comment"># Do a case-insensitive sort</span>
<span class="Statement">my</span> <span class="Identifier">@sorted</span> = (<span class="Statement">sort {</span> <span class="Statement">lc</span>(<span class="Identifier">$b</span>) <span class="Statement">cmp</span> <span class="Statement">lc</span>(<span class="Identifier">$a</span>); <span class="Statement">}</span> <span class="Identifier">@input</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">@sorted</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--useful_funcs--grep--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--useful_funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--useful_funcs--sort--cmp--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--useful_funcs--DIR">Useful Functions</a> → <a href="#page--useful_funcs--grep--PAGE">grep</a>
</div>
<h1>
9.4. grep
</h1>
<p>
The <tt>grep</tt> function can be used to filter items out of an array based
on a boolean expression or a regular expression. The syntax for the block
usage is similar to <tt>map</tt> while the syntax for the regexp usage is
similar to <tt>split</tt>.
</p>
<p>
Here is an example that takes a file and filters only the perl comments whose
length is lesser than 80 characters:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> (<span class="Identifier">@lines</span>, <span class="Identifier">$l</span>);

<span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Statement">shift</span>;

<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;</span><span class="Constant">&lt;</span><span class="Constant">&quot;</span>, <span class="Identifier">$filename</span>;
<span class="Statement">while</span> (<span class="Identifier">$l</span> = &lt;I&gt;)
{
    <span class="Statement">chomp</span>(<span class="Identifier">$l</span>);
    <span class="Statement">push</span> <span class="Identifier">@lines</span>, <span class="Identifier">$l</span>;
}
<span class="Statement">close</span>(<span class="Identifier">I</span>);

<span class="Comment"># Filter the comments</span>
<span class="Statement">my</span> <span class="Identifier">@comments</span> = <span class="Statement">grep</span>(<span class="Statement">/</span><span class="Constant">^#</span><span class="Statement">/</span>, <span class="Identifier">@lines</span>);
<span class="Comment"># Filter out the long comments</span>
<span class="Statement">my</span> <span class="Identifier">@short_comments</span> = (<span class="Statement">grep</span> { <span class="Statement">length</span>(<span class="Identifier">$_</span>) &lt;= <span class="Constant">80</span> ; } <span class="Identifier">@comments</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">@short_comments</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
And here's how <tt>grep</tt> can help us find the first 100 primes:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">@primes</span> = (<span class="Constant">2</span>);
<span class="Statement">for</span>(<span class="Statement">my</span> <span class="Identifier">$n</span>=<span class="Constant">3</span> ; <span class="Statement">scalar</span>(<span class="Identifier">@primes</span>) &lt; <span class="Constant">100</span> ; <span class="Identifier">$n</span>++)
{
    <span class="Statement">if</span> (<span class="Statement">scalar</span>(<span class="Statement">grep</span> { <span class="Identifier">$n</span> % <span class="Identifier">$_</span> == <span class="Constant">0</span> ; } <span class="Identifier">@primes</span>) == <span class="Constant">0</span>)
    {
        <span class="Statement">push</span> <span class="Identifier">@primes</span>, <span class="Identifier">$n</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">@primes</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--references--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--useful_funcs--grep--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--hanoi--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a>
</div>
<h1>
10. References
</h1>
<p>
Perl allows one to refer to the location of a certain variable in memory. An
expression that holds such location is called a <b>reference</b>. Those that
are familiar with C's or Pascal's pointers may think of references as
pointers. There are however, two fundamental differences:
</p>
<ol>
<li>
There is no reference arithmetics in perl. If for example, a reference points
to the fifth element of an array, then adding 1 to it will not refer you to the
sixth element. In fact, adding or substracting integers from references is
possible but quite meaningless.
</li>
<li>
A reference is guaranteed to remain the same even if an array or a string are
resized. In C, <tt>realloc</tt>ing an array yields a completely different
pointer.
</li>
</ol>
<p>
Perl distinguishes between an array or a hash and a reference of it.
The reference of any array may be taken, and a reference to an array
may always be converted to its elements, but there is still a difference
in functionality.
</p>
<p>
The best way to change a variable in a different scope
(such as inside a different function) is to pass its reference to the
function. The called function can then dereference the variable to access
or modify its value.
</p>
<hr />

</div>
<div class="page" id="page--references--hanoi--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--backslash--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--hanoi--PAGE">Example: The Towers of Hanoi</a>
</div>
<h1>
10.1. Example: The Towers of Hanoi
</h1>
<p>
In this example, which is intended to give a taste of the capabilities of
references, we will solve the well-known <a href="http://www.cut-the-knot.org/recurrence/hanoi.shtml">Towers of Hanoi</a> problem. (Refer to the link to learn
more about the problem.) The number of disks can be inputted from the command-line.
The towers themselves will be represented as an array of three elements, each
of which is a reference to an array.
</p>
<p>
We will use the recursive solution in which in order to move a column of $N
disks, we first move the upper column with $N-1 disks and then move the
bottom most disk, and then move the $N-1 disks-long column on top of it.
</p>
<p>
Here goes:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$num_disks</span> = <span class="Statement">shift</span> || <span class="Constant">9</span>;

<span class="Statement">my</span> <span class="Identifier">@towers</span> = (
    [ <span class="Statement">reverse</span>(<span class="Constant">1</span> .. <span class="Identifier">$num_disks</span>) ],  <span class="Comment"># A [ ... ] is a dynamic reference to </span>
    [ ],                           <span class="Comment"># an array</span>
    [ ]
    );

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">print_towers</span>
{
    <span class="Statement">for</span>(<span class="Statement">my</span> <span class="Identifier">$a</span>=<span class="Constant">0</span>;<span class="Identifier">$a</span>&lt;<span class="Constant">3</span>;<span class="Identifier">$a</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="Statement">join</span>(<span class="Constant">&quot;</span><span class="Constant"> </span><span class="Constant">&quot;</span>, @{<span class="Identifier">$towers</span>[<span class="Identifier">$a</span>]}); <span class="Comment"># We de-reference the tower</span>
        <span class="Statement">print</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="Special">\n\n</span><span class="Constant">&quot;</span>;
}

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">move_column</span>
{
    <span class="Statement">my</span> <span class="Identifier">$source</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">$dest</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">$how_many</span> = <span class="Statement">shift</span>;

    <span class="Statement">if</span> (<span class="Identifier">$how_many</span> == <span class="Constant">0</span>)
    {
        <span class="Statement">return</span>;
    }
    <span class="Comment"># Find the third column </span>
    <span class="Statement">my</span> <span class="Identifier">$intermediate</span> = <span class="Constant">0+1+2</span>-<span class="Identifier">$source</span>-<span class="Identifier">$dest</span>;
    move_column(<span class="Identifier">$source</span>, <span class="Identifier">$intermediate</span>, <span class="Identifier">$how_many</span><span class="Constant">-1</span>);
    <span class="Comment"># Print the current state</span>
    print_towers();
    <span class="Comment"># Move one disk. Notice the dereferncing of the arrays</span>
    <span class="Comment"># using @{$ref}.</span>
    <span class="Statement">push</span> @{<span class="Identifier">$towers</span>[<span class="Identifier">$dest</span>]}, <span class="Statement">pop</span>(@{<span class="Identifier">$towers</span>[<span class="Identifier">$source</span>]});
    move_column(<span class="Identifier">$intermediate</span>, <span class="Identifier">$dest</span>, <span class="Identifier">$how_many</span><span class="Constant">-1</span>);
}

<span class="Comment"># Move the entire column</span>
move_column(<span class="Constant">0</span>,<span class="Constant">1</span>,<span class="Identifier">$num_disks</span>);
print_towers();
</pre></div>
<hr />

</div>
<div class="page" id="page--references--backslash--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--hanoi--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--square_brackets--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--backslash--PAGE">\ - Taking a Reference to an Existing Variable</a>
</div>
<h1>
10.2. \ - Taking a Reference to an Existing Variable
</h1>
<p>
In Perl the backslash (<tt>\</tt>) is an operator that returns the
reference to an existing variable. It can also return a dynamic reference
to a constant.
</p>
<p>
Here is an example that uses a reference to update a sum:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$sum</span> = <span class="Constant">0</span>;

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">update_sum</span>
{
    <span class="Statement">my</span> <span class="Identifier">$ref_to_sum</span> = <span class="Statement">shift</span>;
    <span class="Statement">foreach</span> <span class="Statement">my</span> <span class="Identifier">$item</span> (<span class="Identifier">@_</span>)
    {
        <span class="Comment"># The ${ ... } dereferences the variable </span>
        ${<span class="Identifier">$ref_to_sum</span>} += <span class="Identifier">$item</span>;
    }
}

update_sum(<span class="Identifier">\$sum</span>, <span class="Constant">5</span>, <span class="Constant">4</span>, <span class="Constant">9</span>, <span class="Constant">10</span>, <span class="Constant">11</span>);
update_sum(<span class="Identifier">\$sum</span>, <span class="Constant">100</span>, <span class="Constant">80</span>, <span class="Constant">7</span>, <span class="Constant">24</span>);

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\$</span><span class="Constant">sum is now </span><span class="Constant">&quot;</span>, <span class="Identifier">$sum</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
As can be seen, because the reference to <tt>$sum</tt> was used, its value
changes throughout the program.
</p>
<hr />

</div>
<div class="page" id="page--references--square_brackets--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--backslash--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--curly_brackets--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--square_brackets--PAGE">[ @array ] - a Dynamic Reference to an Array</a>
</div>
<h1>
10.3. [ @array ] - a Dynamic Reference to an Array
</h1>
<p>
An array surrounded by square brackets (<tt>[ @array ]</tt>) returns a dynamic
reference to an array. This reference does not affect other values directly, which is
why it is called dynamic.
</p>
<p>
We have already encountered such dynamic array references in the Hanoi
example. But their use is not limited to such. Since a reference to an array
is a scalar, it can serve as a hash value and therefore serve as an object
member. (as will be seen later in the series).
</p>
<p>
In this example, a function is given two arrays, and returns an array that
is the element-wise sum of both of them:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">vector_sum</span>
{
    <span class="Statement">my</span> <span class="Identifier">$v1_ref</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">$v2_ref</span> = <span class="Statement">shift</span>;

    <span class="Statement">my</span> <span class="Identifier">@ret</span>;

    <span class="Statement">my</span> <span class="Identifier">@v1</span> = @{<span class="Identifier">$v1_ref</span>};
    <span class="Statement">my</span> <span class="Identifier">@v2</span> = @{<span class="Identifier">$v2_ref</span>};

    <span class="Statement">if</span> (<span class="Statement">scalar</span>(<span class="Identifier">@v1</span>) != <span class="Statement">scalar</span>(<span class="Identifier">@v2</span>))
    {
        <span class="Statement">return</span> <span class="Statement">undef</span>;
    }
    <span class="Statement">for</span>(<span class="Statement">my</span> <span class="Identifier">$i</span>=<span class="Constant">0</span>;<span class="Identifier">$i</span>&lt;<span class="Statement">scalar</span>(<span class="Identifier">@v1</span>);<span class="Identifier">$i</span>++)
    {
        <span class="Statement">push</span> <span class="Identifier">@ret</span>, (<span class="Identifier">$v1</span>[<span class="Identifier">$i</span>] + <span class="Identifier">$v2</span>[<span class="Identifier">$i</span>]);
    }

    <span class="Statement">return</span> [ <span class="Identifier">@ret</span> ];
}

<span class="Statement">my</span> <span class="Identifier">$ret</span> = vector_sum(
    [ <span class="Constant">5</span>, <span class="Constant">9</span>, <span class="Constant">24</span>, <span class="Constant">30</span> ],
    [ <span class="Constant">8</span>, <span class="Constant">2</span>, <span class="Constant">10</span>, <span class="Constant">20</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">$ret</span>}), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<table class="note" summary="">
<tr class="note">
<td class="note">
<h3 class="note">Note</h3>
<p>
The fundamental difference between using <tt>\@myarray</tt> on an existing
variable named <tt>@myarray</tt> to using <tt>[ @myarray ]</tt> is this: the latter
forms creates a dynamic copy of <tt>@myarray</tt> and if this copy changes,
<tt>@myarray</tt> will not change with it. On the other hand, changes made to
a reference generated by backslash, will affect the original variable.
</p>
<p>
Note that if the members of <tt>@myarray</tt> are themselves references, then
the second form will not make a copy of them. Thus, they can be modified too,
even in the second form.
</p>
</td>
</tr>
</table>
<hr />

</div>
<div class="page" id="page--references--curly_brackets--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--square_brackets--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--arrow--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--curly_brackets--PAGE">{ %hash } - a Dynamic Reference to a Hash</a>
</div>
<h1>
10.4. { %hash } - a Dynamic Reference to a Hash
</h1>
<p>
In a similar way to the square brackets, putting a hash inside a pair
of curly braces (<tt>{ ... }</tt>) will make it into a reference to a
hash. Like an array reference, this reference is a scalar and can be
used as an array element or a hash value. Plus, its own values can be
references to other arrays or hashes.
</p>
<p>
To demonstrate this let's see the code of part of the contents table of
this very lecture, to demonstrate the multi-level data structure
capabilities of perl:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$contents</span> =
{
    <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Contents</span><span class="Constant">&quot;</span>,
    <span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span> =&gt;
    [
        {
            <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">for</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">The for loop</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span> =&gt;
            [
                {
                    <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">next.html</span><span class="Constant">&quot;</span>,
                    <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Behaviour of next in the for loop</span><span class="Constant">&quot;</span>,
                },
                {
                    <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">whence_for.html</span><span class="Constant">&quot;</span>,
                    <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Whence for?</span><span class="Constant">&quot;</span>,
                },
            ],
        },
        {
            <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">hashes</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Hashes</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span> =&gt;
            [
                {
                    <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">functions.html</span><span class="Constant">&quot;</span>,
                    <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Hash Functions</span><span class="Constant">&quot;</span>,
                },
            ],
        },
        {
            <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">my</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">Declaring Local Variables with </span><span class="Special">\&quot;</span><span class="Constant">my</span><span class="Special">\&quot;</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span> =&gt;
            [
                {
                    <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">use_strict.html</span><span class="Constant">&quot;</span>,
                    <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Special">\&quot;</span><span class="Constant">use strict</span><span class="Special">\&quot;</span><span class="Constant">, Luke!</span><span class="Constant">&quot;</span>,
                },
            ],
        },
        {
            <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">argv.html</span><span class="Constant">&quot;</span>,
            <span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span> =&gt; <span class="Constant">&quot;</span><span class="Constant">The </span><span class="Special">\@</span><span class="Constant">ARGV array</span><span class="Constant">&quot;</span>,
        },
    ],
    <span class="Constant">'</span><span class="Constant">images</span><span class="Constant">'</span> =&gt;
    [
        {
            <span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span> =&gt; <span class="Constant">'</span><span class="Constant">style.css</span><span class="Constant">'</span>,
            <span class="Constant">'</span><span class="Constant">type</span><span class="Constant">'</span> =&gt; <span class="Constant">'</span><span class="Constant">text/css</span><span class="Constant">'</span>,
        }
    ],
};

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">get_contents</span>
{
    <span class="Statement">return</span> <span class="Identifier">$contents</span>;
}
</pre></div>
<hr />

</div>
<div class="page" id="page--references--arrow--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--curly_brackets--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--references--dereferencing--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--arrow--PAGE">The Arrow Operators</a>
</div>
<h1>
10.5. The Arrow Operators
</h1>
<p>
An arrow (<tt>-&gt;</tt>) followed by a square or curly brackets can be used
to directly access the elements of an array or a hash referenced by a certain
hash. For instance: <tt>$array_ref-&gt;[5]</tt> will retrieve the 5th element
of the array pointed to by <tt>$array_ref</tt>.
</p>
<p>
As an example let's print a tree of the contents of the part of the lecture
that was presented in the previous slide:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">do</span> <span class="Constant">&quot;</span><span class="Constant">lol.pl</span><span class="Constant">&quot;</span>;         <span class="Comment"># Load the other file</span>

<span class="Statement">my</span> <span class="Identifier">$cont</span> = get_contents();

<span class="Statement">print</span> <span class="Identifier">$cont</span>-&gt;{<span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span>}, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">$cont</span>-&gt;{<span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span>}-&gt;[<span class="Constant">0</span>]-&gt;{<span class="Constant">'</span><span class="Constant">url</span><span class="Constant">'</span>}, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">$cont</span>-&gt;{<span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span>}-&gt;[<span class="Constant">0</span>]-&gt;{<span class="Constant">'</span><span class="Constant">subs</span><span class="Constant">'</span>}-&gt;[<span class="Constant">1</span>]-&gt;{<span class="Constant">'</span><span class="Constant">title</span><span class="Constant">'</span>}, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
Note that the arrows following the first arrow are optional as perl sees
that the programmer wishes to access the subseqeunt sub-items. However, the
first one is mandatory because the expression <tt>$array_ref{'elem'}</tt> looks
for the hash <tt>%array_ref</tt>.
</p>
<hr />

</div>
<div class="page" id="page--references--dereferencing--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--references--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--references--arrow--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--debugger--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--references--DIR">References</a> → <a href="#page--references--dereferencing--PAGE">Dereferencing</a>
</div>
<h1>
10.6. Dereferencing
</h1>
<p>
The entire scalar or data structure pointed to by the reference can be
retrieved by dereferncing. Dereferencing is done by using a <tt>$</tt>,
a <tt>@</tt> or a <tt>%</tt> (depending if the reference refers to a scalar
, array or a hash respectively), and then the reference inside curly braces.
</p>
<p>
Here are some simple examples:
</p>
<div class="code_block"><pre>
<span class="Statement">use strict</span>;
<span class="Statement">use warnings</span>;

<span class="Statement">my</span> <span class="Identifier">$ds1</span> =
{
    <span class="Constant">'</span><span class="Constant">h</span><span class="Constant">'</span> =&gt; [<span class="Constant">5</span>,<span class="Constant">6</span>,<span class="Constant">7</span>],
    <span class="Constant">'</span><span class="Constant">y</span><span class="Constant">'</span> =&gt; { <span class="Constant">'</span><span class="Constant">t</span><span class="Constant">'</span> =&gt; <span class="Constant">'</span><span class="Constant">u</span><span class="Constant">'</span>, <span class="Constant">'</span><span class="Constant">o</span><span class="Constant">'</span> =&gt; <span class="Constant">'</span><span class="Constant">p</span><span class="Constant">'</span> },
    <span class="Constant">'</span><span class="Constant">hello</span><span class="Constant">'</span> =&gt; <span class="Constant">'</span><span class="Constant">up</span><span class="Constant">'</span>,
};

<span class="Statement">my</span> <span class="Identifier">$array_ref</span> = [<span class="Constant">5</span>, <span class="Constant">6</span>, <span class="Constant">7</span>, <span class="Constant">10</span>, <span class="Constant">24</span>, <span class="Constant">90</span>, <span class="Constant">14</span>];
<span class="Statement">my</span> <span class="Identifier">$a</span> = <span class="Constant">&quot;</span><span class="Constant">Hello World!</span><span class="Constant">&quot;</span>;
<span class="Statement">my</span> <span class="Identifier">$b</span> = <span class="Identifier">\$a</span>;

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\$</span><span class="Constant">array_ref:</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="Constant">, </span><span class="Constant">&quot;</span>, @{<span class="Identifier">$array_ref</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="Special">\n\n\$</span><span class="Constant">ds1-&gt;{'h'}:</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="Constant">, </span><span class="Constant">&quot;</span>, @{<span class="Identifier">$ds1</span>-&gt;{<span class="Constant">'</span><span class="Constant">h</span><span class="Constant">'</span>}}), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Statement">my</span> <span class="Identifier">%hash</span> = %{<span class="Identifier">$ds1</span>-&gt;{<span class="Constant">'</span><span class="Constant">y</span><span class="Constant">'</span>}};

<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n\n\%</span><span class="Constant">hash:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Statement">foreach</span> <span class="Statement">my</span> <span class="Identifier">$k</span> (<span class="Statement">keys</span>(<span class="Identifier">%hash</span>))
{
    <span class="Statement">print</span> <span class="Identifier">$k</span>,  <span class="Constant">&quot;</span><span class="Constant"> =&gt; </span><span class="Constant">&quot;</span>, <span class="Identifier">$hash</span>{<span class="Identifier">$k</span>};
}


<span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n\n\$\$</span><span class="Constant">b:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Statement">print</span> ${<span class="Identifier">$b</span>}, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<p>
If the expression that yields the reference is a simple one than the curly
brackets can be omitted (e.g: <tt>@$array_ref</tt> or <tt>$$ref</tt>).
However, assuming you use curly brackets - the expression surrounded
inside them can be as complex as you would like.
</p>
<hr />

</div>
<div class="page" id="page--debugger--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--references--dereferencing--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--debugger--stepping--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--debugger--DIR">Using the perl Debugger</a>
</div>
<h1>
11. Using the perl Debugger
</h1>
<p>
Even if your perl code compiles and runs, it doesn't mean it is error free.
Very often you will write programs that when given specific input, will behave
very differently from their intended behaviour. Perl supplies the user with a
debugger that can help in trapping such bugs in the program.
</p>
<p>
The debugger is invoked simply by appending a "-d" switch before the script
name. E.g: <tt>perl -d hello.pl</tt>. After typing "Return" at the shell command
line, you will have an interactive debugging session at your disposal. At this
session, you can control the execution of your program as well as enter any
Perl command you like.
</p>
<p>
The following chapter will cover the basics of using the perl debugger, and
will give you pointers on where to find more information.
</p>
<hr />

</div>
<div class="page" id="page--debugger--stepping--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--debugger--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--debugger--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--debugger--breakpoints--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--debugger--DIR">Using the perl Debugger</a> → <a href="#page--debugger--stepping--PAGE">Stepping over and Stepping in</a>
</div>
<h1>
11.1. Stepping over and Stepping in
</h1>
<p>
The debugger displays the perl statement that is about to be executed in
its display window, and below it the command prompt. It looks something like
this:
</p>
<div class="code_block"><pre>
shlomi:~/bin# perl -d add_cr.pl
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(add_cr.pl:5):	my ($contents);
  DB&lt;1&gt;
</pre></div>
<p>
The most commonly used command in the debugger is to have it execute one
(and only one) perl statement. This is called stepping and there are two types
of it: stepping over and stepping in.
</p>
<p>
The difference is that stepping over does not enter the perl functions
that were called in the expression that was evaluated. Stepping in, on
the other hand, does enter such functions.
</p>
<p>
To step over type <tt>n</tt> (short for "next") followed by enter
, and you'll see the next perl command displayed. To step in type
<tt>s</tt> (short for "step").
</p>
<p>
And here's how the screen will look after a few step overs:
</p>
<div class="code_block"><pre>
shlomi:~/bin# perl -d add_cr.pl test.txt
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(add_cr.pl:5):	my ($contents);
  DB&lt;1&gt; n
main::(add_cr.pl:7):	foreach my $file (@ARGV)
main::(add_cr.pl:8):	{
  DB&lt;1&gt; n
main::(add_cr.pl:9):	    $contents = "";
  DB&lt;1&gt; n
main::(add_cr.pl:11):	    open I, "&lt;$file";
  DB&lt;1&gt;
</pre></div>
<p>
Note that sometimes an expression will take more than one step over to
pass. Operations such as <tt>map</tt>, <tt>sort</tt> and friends are especially
notorious for that. Breakpoints that will be covered in the next slide
offer a solution to this problem.
</p>
<hr />

</div>
<div class="page" id="page--debugger--breakpoints--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--debugger--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--debugger--stepping--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--debugger--perl_cmds--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--debugger--DIR">Using the perl Debugger</a> → <a href="#page--debugger--breakpoints--PAGE">Setting Breakpoints and Continuing</a>
</div>
<h1>
11.2. Setting Breakpoints and Continuing
</h1>
<p>
When one places a breakpoint on a certain line in a program, he indicates
that the execution of the program must be stopped when this line is
reached. Then, one can cause the program to run freely using the continue
command, and have it stop only at this line.
</p>
<p>
Breakpoints generally save a lot of stepping. To set a breakpoint type
<tt>b $line_number</tt> or <tt>b $function_name</tt> where $line_number is the
line number and $function_name is the function name. A breakpoint on a function
will stop the execution of the program as soon as the function is entered.
</p>
<p>
To continue the execution of the program until a breakpoint is reached (or
until the program terminates) type <tt>c</tt>. Here is an example session with
the same program:
</p>
<div class="code_block"><pre>
shlomi:~/bin# perl -d add_cr.pl test.txt
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(add_cr.pl:5):	my ($contents);
  DB&lt;1&gt; b 11
  DB&lt;2&gt; c
main::(add_cr.pl:11):	    open I, "&lt;$file";
  DB&lt;2&gt;
</pre></div>
<p>
One can set a conditional breakpoint by typing
<tt>b [line] [perl expression]</tt>. A conditional expression is one
that stops the execution of the program only if the perl expression
evaluates to true. The perl expression can be as complex as you like
and may include whitespace.
</p>
<p>
To delete a breakpoint type <tt>d [line]</tt>. After a breakpoint is deleted
it will no longer affect the execution of your program.
</p>
<hr />

</div>
<div class="page" id="page--debugger--perl_cmds--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--debugger--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--debugger--breakpoints--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--debugger--more_info--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--debugger--DIR">Using the perl Debugger</a> → <a href="#page--debugger--perl_cmds--PAGE">Executing Perl Commands inside the Debugger</a>
</div>
<h1>
11.3. Executing Perl Commands inside the Debugger
</h1>
<p>
One can execute perl commands inside the debugger. For example, typing
<tt>print $a</tt> at the debugger prompt will print the value of the
<tt>$a</tt> variable. You can also modify the values of variables in this
way, or run functions, etc.
</p>
<p>
One special command that is also useful is <tt>x</tt>. <tt>x $var</tt>
displays <tt>$var</tt> in a hierarchial manner, which is very useful for
lists of lists etc. The <b>Data::Dumper</b> module which is available from
<a href="http://www.cpan.org/">CPAN</a> offers a similiar functionality, from
within your perl programs.
</p>
<hr />

</div>
<div class="page" id="page--debugger--more_info--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--debugger--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--debugger--perl_cmds--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--finale--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 2</a> → <a href="#page--debugger--DIR">Using the perl Debugger</a> → <a href="#page--debugger--more_info--PAGE">Getting More Information</a>
</div>
<h1>
11.4. Getting More Information
</h1>
<p>
The <tt>h</tt> command in the debugger displays the list of available
debugger commands.
</p>
<p>
Usage information about the perl debugger is available at the
<a href="http://perldoc.perl.org/perldebug.html"><b>perldebug</b> document</a>.
</p>
<p>
There are some front-ends to the perl debugger. One of them, which
runs on Linux, is <a href="http://www.gnu.org/software/ddd/">ddd</a>.
</p>
<hr />

</div>
<div class="page" id="page--finale--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--debugger--more_info--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 2</a> → <a href="#page--finale--PAGE">Finale</a>
</div>
<h1>
12. Finale
</h1>
<blockquote>
<p>
I'm reminded of the day my daughter came in, looked over my shoulder at
some Perl 4 code, and said, "What is that, swearing?"
</p>
</blockquote>
<p>
<i>(Larry Wall (the father of Perl) in &lt;199806181642.JAA10629@wall.org&gt;)</i>
</p>
<p>
But luckily the next back-end for Perl 5 -
<a href="http://www.parrotcode.org/">Parrot</a> will support multiple
syntax front-ends, so one will be
able to write Perl in an alternate syntax that does not have all those
pesky sigils. The question is: why would anyone ever want to do that?
<b>:-)</b>
</p>
<p>
No, seriously. We hoped you enjoyed the lecture series, but remember
that it does not teach you everything that is to know about perl,
much less how to understand other people's code.
</p>
<p>
By now, you can probably understand the perl core documents, so it is
recommended
that you read them in case you are looking for more information. You can
also refer to <a href="http://perl-begin.berlios.de/">the Perl Beginners'
Site</a> for more sources of information as well as ways to get help online.
</p>
<p>
We hope to continue this lecture with more presentations covering Perl's
modules, objects and references to functions. Until then, stay cool and may
the dollar sign be with you!
</p>
<hr />

</div>

</body></html>