Source

perl-begin / lib / tutorials / perl-for-newbies / lect4-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
<?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 4
</title>
</head>
<body><div class="page">


<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--cpan--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a>
</div>
<h1 id="page--DIR">
"Perl for Perl Newbies" - Part 4
</h1>
<h2 id="contents">Contents</h2>
<ul class="contentsmain">
<li><a href="#page--cpan--DIR" class="contents">1. CPAN Modules</a>
<ul class="contents">
<li><a href="#page--cpan--manual--PAGE" class="contents">1.1. Manual Compilation</a>
</li><li><a href="#page--cpan--mcpan--PAGE" class="contents">1.2. The -MCPAN Interface</a>
</li></ul>
</li><li><a href="#page--sprintf--DIR" class="contents">2. The sprintf function</a>
<ul class="contents">
<li><a href="#page--sprintf--conversions--PAGE" class="contents">2.1. Supported Conversions</a>
</li><li><a href="#page--sprintf--flags--PAGE" class="contents">2.2. Flags to the conversions</a>
</li><li><a href="#page--sprintf--width--PAGE" class="contents">2.3. Width and Max Width</a>
</li></ul>
</li><li><a href="#page--string-forms--DIR" class="contents">3. Alternate Forms for Writing Strings</a>
<ul class="contents">
<li><a href="#page--string-forms--q_qq--PAGE" class="contents">3.1. q{}, qq{} and Friends</a>
</li><li><a href="#page--string-forms--here_doc--PAGE" class="contents">3.2. Here Document</a>
</li></ul>
</li><li><a href="#page--processes--DIR" class="contents">4. Executing Other Processes</a>
<ul class="contents">
<li><a href="#page--processes--system--PAGE" class="contents">4.1. The system() Command</a>
</li><li><a href="#page--processes--backticks--PAGE" class="contents">4.2. Trapping Command Output with `...`</a>
</li><li><a href="#page--processes--opens--PAGE" class="contents">4.3. open() for Command Execution</a>
</li><li><a href="#page--processes--string-shellquote--PAGE" class="contents">4.4. String::ShellQuote</a>
</li></ul>
</li><li><a href="#page--and_or--DIR" class="contents">5. More about || and &amp;&amp;</a>
<ul class="contents">
<li><a href="#page--and_or--sort--PAGE" class="contents">5.1. For sort()</a>
</li><li><a href="#page--and_or--english_and_or--PAGE" class="contents">5.2. The "and" and "or" Operators</a>
</li></ul>
</li><li><a href="#page--exceptions--DIR" class="contents">6. Exceptions</a>
<ul class="contents">
<li><a href="#page--exceptions--die_and_eval--PAGE" class="contents">6.1. die and eval</a>
</li><li><a href="#page--exceptions--carp--PAGE" class="contents">6.2. The Carp module</a>
</li><li><a href="#page--exceptions--error.pm--PAGE" class="contents">6.3. The Error.pm module</a>
</li></ul>
</li><li><a href="#page--system-funcs--DIR" class="contents">7. More System Functions</a>
<ul class="contents">
<li><a href="#page--system-funcs--dir--PAGE" class="contents">7.1. Directory Input Routines</a>
</li><li><a href="#page--system-funcs--random_io--PAGE" class="contents">7.2. Random File I/O</a>
</li><li><a href="#page--system-funcs--file-tests--PAGE" class="contents">7.3. File Tests (-e, -d...)</a>
</li><li><a href="#page--system-funcs--chdir_getcwd_mkdir--PAGE" class="contents">7.4. chdir(), getcwd() and mkdir().</a>
</li><li><a href="#page--system-funcs--stat--PAGE" class="contents">7.5. The stat() Function</a>
</li></ul>
</li></ul>
<h2 id="licence">Licence</h2>
<p>
  <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" alt="CC0" />
  </a>
  <br />
  To the extent possible under law, <a href="http://www.shlomifish.org/" rel="dct:publisher"><span>Shlomi Fish</span></a>
  has waived all copyright and related or neighbouring rights to
  <span>Perl for Perl Newbies</span>.
This work is published from
<span>Israel</span>.
</p>
<hr />

</div>
<div class="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--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--cpan--manual--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--cpan--DIR">CPAN Modules</a>
</div>
<h1 id="page--cpan--DIR">
1. CPAN Modules
</h1>
<p>
<a href="http://perl.net.au/wiki/CPAN">CPAN</a> stands for the "Comprehensive
Perl Archive Network". It is a mirrored repository of Perl code,
packaged in so-called <b>CPAN distributions</b> (or formerly known
as <b>CPAN modules</b>) that can be installed (along with their dependencies)
by issuing one command.
</p>
<p>
This section will show, how to install a typical CPAN module, either
manually or automatically.
</p>
<ul class="contentsmain">
<li><a href="#page--cpan--manual--PAGE" class="contents">1.1. Manual Compilation</a>
</li><li><a href="#page--cpan--mcpan--PAGE" class="contents">1.2. The -MCPAN Interface</a>
</li></ul>
<hr />

</div>
<div class="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--cpan--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--cpan--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--cpan--mcpan--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--cpan--DIR">CPAN Modules</a><a href="#page--cpan--manual--PAGE">Manual Compilation</a>
</div>
<h1 id="page--cpan--manual--PAGE">
1.1. Manual Compilation
</h1>
<p>
To compile a CPAN distribution manually, you first need to download it. You
can start by browsing your nearest CPAN module and downloading from there. On
UNIX the following command, will download version 2.31 of the XML::Parser
module.
</p>
<div class="code_block"><pre>
$ wget http://www.cpan.org/modules/by-module/XML/XML-Parser-2.31.tar.gz
</pre></div>
<p>
Next unpack it using the "tar -xvf" command:
</p>
<div class="code_block"><pre>
$ tar -xvf XML-Parser-2.31.tar.gz
</pre></div>
<p>
(if you don't have GNU tar use "gunzip" and then "tar -xvf" instead. On
Windows you can use WinZip.)
</p>
<p>
Now cd to its directory and type "perl Makefile.PL" and "make".
</p>
<div class="code_block"><pre>
$ cd XML-Parser-2.31
$ perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for XML::Parser::Expat
Writing Makefile for XML::Parser
$ make
cp Parser/Encodings/x-sjis-cp932.enc blib/lib/XML/Parser/Encodings/x-sjis-cp932.enc
cp Parser/Encodings/iso-8859-7.enc blib/lib/XML/Parser/Encodings/iso-8859-7.enc
cp Parser/Encodings/x-euc-jp-unicode.enc blib/lib/XML/Parser/Encodings/x-euc-jp-unicode.enc
cp Parser/Encodings/iso-8859-9.enc blib/lib/XML/Parser/Encodings/iso-8859-9.enc
cp Parser/Encodings/README blib/lib/XML/Parser/Encodings/README
cp Parser/Encodings/euc-kr.enc blib/lib/XML/Parser/Encodings/euc-kr.enc
cp Parser/Encodings/big5.enc blib/lib/XML/Parser/Encodings/big5.enc
cp Parser/Encodings/windows-1250.enc blib/lib/XML/Parser/Encodings/windows-1250.enc
cp Parser/Encodings/Japanese_Encodings.msg blib/lib/XM
.
.
.
</pre></div>
<p>
After the wait, the module will be compiled. It is preferable to test it first, by invoking <tt>make test</tt>:
</p>
<div class="code_block"><pre>
$ make test
</pre></div>
<p>
Now you can install it, by becoming
a super-user and typing <tt>make install</tt> at the command line.
</p>
<div class="code_block"><pre>
$ su
Password:
# make install
</pre></div>
<h2 id="module-build">Module-Build</h2>
<p>
Note that some distributions are now using
<a href="http://search.cpan.org/dist/Module-Build/">Module-Build</a>. If
your distribution contains a <tt>Build.PL</tt> file, you should run
the following commands instead:
</p>
<div class="code_block"><pre>
perl Build.PL
./Build
./Build test
./Build install
</pre></div>
<p>
More information can be found in
<a href="http://search.cpan.org/perldoc?Module::Build">the main Module-Build
document</a>
</p>
<hr />

</div>
<div class="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--cpan--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--cpan--manual--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--sprintf--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--cpan--DIR">CPAN Modules</a><a href="#page--cpan--mcpan--PAGE">The -MCPAN Interface</a>
</div>
<h1 id="page--cpan--mcpan--PAGE">
1.2. The -MCPAN Interface
</h1>
<p>
Perl has a module called CPAN with which one can install CPAN modules along
with all of the modules they depend on. To invoke it type <tt>perl -MCPAN -e
shell</tt> at the command line while being a super-user, and follow the
instructions it gives you. The <tt>install</tt> command can be used to
automatically install modules. For example: to install the XML::XSLT module,
the following can be done from the CPAN prompt:
</p>
<div class="code_block"><pre>
cpan&gt; install XML::XSLT
</pre></div>
<p>
This will in turn install "XML::Parser" and other modules it needs.
</p>
<h2 id="cpanplus">CPANPLUS.pm</h2>
<p>
CPANPLUS.pm is a more modern, modular, and enhanced alternative to
CPAN.pm. It is part of perl-5.10.x and above, but can be installed separately.
As such, its use is more recommended.
</p>
<h2 id="distro-specific">Operating System/Distribution - Specific Ways</h2>
<p>
Normally, you should look for a suitable native package of the CPAN module
for your operating system or
<a href="http://en.wikipedia.org/wiki/Linux_distribution">distribution</a>. If
that fails, you should consider building your own package. Consult your
distribution's help channels for more information.
</p>
<hr />

</div>
<div class="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--cpan--mcpan--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--sprintf--conversions--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--sprintf--DIR">The sprintf function</a>
</div>
<h1 id="page--sprintf--DIR">
2. The sprintf function
</h1>
<p>
The <tt>sprintf</tt> built-in function can be used to translate a format
string with some conversions embedded inside, and some parameters into a
formatted string. Each conversion is specified by the starting character of the
percent sign (<tt>%</tt>), and can have a type and several parameters that will
dictate how it will be formatted in the output string. The output string is
returned by <tt>sprintf</tt>.
</p>
<p>
Here's an example that illustrates its use:
</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">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;Hello </span><span class="Special">\&quot;</span><span class="Constant">%s</span><span class="Special">\&quot;</span><span class="Constant">! Your lucky number is %i.</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;Nathan&quot;</span>, <span class="Constant">65</span>);
</pre></div>
<p>
The output is:
</p>
<div class="code_block"><pre>
Hello "Nathan"! Your lucky number is 65.
</pre></div>
<p>
As you can see, the first conversion is taken from the first argument, and the
second one from the second argument. That's how sprintf works: processing
the conversions from the arguments in order.
</p>
<ul class="contentsmain">
<li><a href="#page--sprintf--conversions--PAGE" class="contents">2.1. Supported Conversions</a>
</li><li><a href="#page--sprintf--flags--PAGE" class="contents">2.2. Flags to the conversions</a>
</li><li><a href="#page--sprintf--width--PAGE" class="contents">2.3. Width and Max Width</a>
</li></ul>
<hr />

</div>
<div class="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--sprintf--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--sprintf--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--sprintf--flags--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--sprintf--DIR">The sprintf function</a><a href="#page--sprintf--conversions--PAGE">Supported Conversions</a>
</div>
<h1 id="page--sprintf--conversions--PAGE">
2.1. Supported Conversions
</h1>
<p>
Here are some of the supported conversions:
</p>
<table summary="">
<tr>
<td>
<tt>%%</tt>
</td>
<td>
An actual percent sign
</td>
</tr>
<tr>
<td>
<tt>%c</tt>
</td>
<td>
A character with the given ASCII number
</td>
</tr>
<tr>
<td>
<tt>%s</tt>
</td>
<td>
A string
</td>
</tr>
<tr>
<td>
<tt>%d</tt>
</td>
<td>
A signed integer, in decimal (also <tt>%i</tt>)
</td>
</tr>
<tr>
<td>
<tt>%o</tt>
</td>
<td>
An integer in octal
</td>
</tr>
<tr>
<td>
<tt>%x</tt>
</td>
<td>
An integer in hexadecimal. (use <tt>%X</tt> for
uppercase hex)
</td>
</tr>
<tr>
<td>
<tt>%e</tt>
</td>
<td>
A floating point number, in scientific notation.
</td>
</tr>
<tr>
<td>
<tt>%f</tt>
</td>
<td>
A float in fixed decimal notation.
</td>
</tr>
<tr>
<td>
<tt>%b</tt>
</td>
<td>
An integer in binary
</td>
</tr>
</table>
<p>
Here are some examples:
</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">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;There is %i%% of alcohol in this beverage</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">27</span>);
<span class="Statement">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;%s%s</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;This string&quot;</span>, <span class="Constant">&quot; ends here.&quot;</span>);
<span class="Statement">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;650 in hex is 0x%x</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">650</span>);
<span class="Statement">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;650 in binary is 0b%b</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">650</span>);
<span class="Statement">print</span> <span class="Statement">sprintf</span>(<span class="Constant">&quot;3.2 + 1.6 == %f</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">3.2</span>+<span class="Constant">1.6</span>);
</pre></div>
<p>
And their output is:
</p>
<div class="code_block"><pre>
There is 27% of alcohol in this beverage
This string ends here.
650 in hex is 0x28a
650 in binary is 0b1010001010
3.2 + 1.6 == 4.800000
</pre></div>
<hr />

</div>
<div class="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--sprintf--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--sprintf--conversions--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--sprintf--width--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--sprintf--DIR">The sprintf function</a><a href="#page--sprintf--flags--PAGE">Flags to the conversions</a>
</div>
<h1 id="page--sprintf--flags--PAGE">
2.2. Flags to the conversions
</h1>
<p>
One can put various flags between the percent sign and before the
conversion character, which alter the output. Here is a list of them:
</p>
<table class="bordered" summary="">
<tr>
<td>
<tt>space</tt>
</td>
<td>
Prefix positive number with a space
</td>
</tr>
<tr>
<td>
<tt>+</tt>
</td>
<td>
Prefix positive number with a + sign
</td>
</tr>
<tr>
<td>
<tt>-</tt>
</td>
<td>
Left justify the output within the specified field
</td>
</tr>
<tr>
<td>
<tt>0</tt>
</td>
<td>
Use zeros, not spaces to right justify.
</td>
</tr>
<tr>
<td>
<tt>#</tt>
</td>
<td>
Prefix non-zero octal with 0, non-zero hex with "0x", and non-zero
binary with "0b"
</td>
</tr>
</table>
<p>
For example (taken from <tt>perldoc -f sprintf</tt>):
</p>
<div class="code_block"><pre>
<span class="Statement">printf</span> <span class="Constant">'&lt;% d&gt;'</span>, <span class="Constant">12</span>;   <span class="Comment"># prints &quot;&lt; 12&gt;&quot;</span>
<span class="Statement">printf</span> <span class="Constant">'&lt;%+d&gt;'</span>, <span class="Constant">12</span>;   <span class="Comment"># prints &quot;&lt;+12&gt;&quot;</span>
<span class="Statement">printf</span> <span class="Constant">'&lt;%6s&gt;'</span>, <span class="Constant">12</span>;   <span class="Comment"># prints &quot;&lt;    12&gt;&quot;</span>
<span class="Statement">printf</span> <span class="Constant">'&lt;%-6s&gt;'</span>, <span class="Constant">12</span>;  <span class="Comment"># prints &quot;&lt;12    &gt;&quot;</span>
<span class="Statement">printf</span> <span class="Constant">'&lt;%06s&gt;'</span>, <span class="Constant">12</span>;  <span class="Comment"># prints &quot;&lt;000012&gt;&quot;</span>
<span class="Statement">printf</span> <span class="Constant">'&lt;%#x&gt;'</span>, <span class="Constant">12</span>;   <span class="Comment"># prints &quot;&lt;0xc&gt;&quot;</span>
</pre></div>
<p>
Note that <tt>printf</tt> formats its arguments using <tt>sprintf</tt> and
then prints them using <tt>print</tt>.
</p>
<hr />

</div>
<div class="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--sprintf--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--sprintf--flags--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--string-forms--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--sprintf--DIR">The sprintf function</a><a href="#page--sprintf--width--PAGE">Width and Max Width</a>
</div>
<h1 id="page--sprintf--width--PAGE">
2.3. Width and Max Width
</h1>
<p>
One can apply an optional width and max width (or for floating point numbers
- precision), specifier for the conversion flags. This is a number followed
by an optional dot and another number.
</p>
<p>
Here are some examples:
</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">printf</span> <span class="Constant">&quot;&lt;%10s&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;Hello&quot;</span>;       <span class="Comment"># Prints &lt;     Hello&gt;</span>
<span class="Statement">printf</span> <span class="Constant">&quot;&lt;%-10s&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;Hello&quot;</span>;      <span class="Comment"># Prints &lt;Hello     &gt;</span>
<span class="Statement">printf</span> <span class="Constant">&quot;&lt;%3.5s&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">&quot;Longstring&quot;</span>; <span class="Comment"># Prints &lt;Longs&gt;</span>
<span class="Statement">printf</span> <span class="Constant">&quot;&lt;%.2f&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Constant">3.1415926535</span>;  <span class="Comment"># Prints &lt;3.14&gt;</span>
</pre></div>
<hr />

</div>
<div class="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--sprintf--width--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--string-forms--q_qq--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--string-forms--DIR">Alternate Forms for Writing Strings</a>
</div>
<h1 id="page--string-forms--DIR">
3. Alternate Forms for Writing Strings
</h1>
<p>
Perl has several alternate forms to write the various type of strings
it supports. This section will cover the variations on this theme.
</p>
<ul class="contentsmain">
<li><a href="#page--string-forms--q_qq--PAGE" class="contents">3.1. q{}, qq{} and Friends</a>
</li><li><a href="#page--string-forms--here_doc--PAGE" class="contents">3.2. Here Document</a>
</li></ul>
<hr />

</div>
<div class="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--string-forms--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--string-forms--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--string-forms--here_doc--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--string-forms--DIR">Alternate Forms for Writing Strings</a><a href="#page--string-forms--q_qq--PAGE">q{}, qq{} and Friends</a>
</div>
<h1 id="page--string-forms--q_qq--PAGE">
3.1. q{}, qq{} and Friends
</h1>
<table class="bordered" summary="">
<tr>
<th>
<b>Customary</b>
</th>
<th>
<b>Generic</b>
</th>
<th>
<b>Meaning</b>
</th>
<th>
<b>Interpolates</b>
</th>
</tr>
<tr>
<td>
''
</td>
<td>
q{}
</td>
<td>
Literal
</td>
<td>
No.
</td>
</tr>
<tr>
<td>
""
</td>
<td>
qq{}
</td>
<td>
Literal
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>
``
</td>
<td>
qx{}
</td>
<td>
Command
</td>
<td>
Yes (unless the delimiter is '')
</td>
</tr>
<tr>
<td>
(none)
</td>
<td>
qw{}
</td>
<td>
Word List
</td>
<td>
No
</td>
</tr>
<tr>
<td>
//
</td>
<td>
m{}
</td>
<td>
Pattern Match
</td>
<td>
Yes (unless the delimiter is '')
</td>
</tr>
<tr>
<td>
(none)
</td>
<td>
qr{}
</td>
<td>
Declaration of a Regex Pattern
</td>
<td>
Yes (unless the delimiter is '')
</td>
</tr>
<tr>
<td>
(none)
</td>
<td>
s{}{}
</td>
<td>
Substitution
</td>
<td>
Yes (unless the delimiter is '')
</td>
</tr>
<tr>
<td>
(none)
</td>
<td>
tr{}{}
</td>
<td>
Transliteration
</td>
<td>
No
</td>
</tr>
</table>
<p>
What it means, is that you can write an interpolated string as <tt>qq</tt>
followed by a matching wrapping character, inside which the string can
be placed. And likewise for the other strings. Here are some examples:
</p>
<div class="code_block"><pre>
<span class="PreProc">#!/usr/bin/perl -w</span>

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


<span class="Statement">my</span> <span class="Identifier">$h</span> = <span class="Constant">q{Hello There}</span>;
<span class="Statement">print</span> <span class="Constant">qq|</span><span class="Identifier">$h</span><span class="Constant">, world!</span><span class="Special">\n</span><span class="Constant">|</span>;

<span class="Statement">my</span> <span class="Identifier">$t</span> = <span class="Constant">q#Router#</span>;
<span class="Statement">my</span> <span class="Identifier">$y</span> = <span class="Constant">qq(</span><span class="Identifier">$h</span><span class="Constant"> </span><span class="Identifier">$h</span><span class="Constant"> </span><span class="Identifier">$h</span><span class="Constant"> </span><span class="Identifier">$t</span><span class="Constant">)</span>;
<span class="Identifier">$y</span> =~ <span class="Statement">s!</span><span class="Constant">Hello</span><span class="Statement">!</span><span class="Constant">Hi</span><span class="Statement">!</span>;
<span class="Statement">print</span> <span class="Constant">qq#</span><span class="Identifier">$y</span><span class="Special">\n</span><span class="Constant">#</span>;

<span class="Statement">my</span> <span class="Identifier">@arr</span> = <span class="Constant">qw{one two three}</span>;
<span class="Statement">for</span> <span class="Statement">my</span> <span class="Identifier">$i</span> (<span class="Constant">0</span> .. <span class="Identifier">$#a</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Identifier">$i</span><span class="Constant">: </span><span class="Identifier">$arr[$i]</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
The output of this is:
</p>
<div class="code_block"><pre>
Hello There, world!
Hi There Hello There Hello There Router
0: one
1: two
2: three
</pre></div>
<p>
As one can see, the wrapping characters should match assuming they are a
left/right pair (<tt>{</tt> to <tt>}</tt> etc.).
</p>
<hr />

</div>
<div class="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--string-forms--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--string-forms--q_qq--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--processes--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--string-forms--DIR">Alternate Forms for Writing Strings</a><a href="#page--string-forms--here_doc--PAGE">Here Document</a>
</div>
<h1 id="page--string-forms--here_doc--PAGE">
3.2. Here Document
</h1>
<p>
In a here document, one specifies an ending string to end the string
on a separate line, and between it, one can place any string he wishes.
This is useful if your string contains a lot of irregular characters.
</p>
<p>
The syntax for a here document is a <tt>&lt;&lt;</tt> followed by
the string ending sequence, followed by the end of the statement. In
the lines afterwards, one places the string itself followed by its
ending sequence.
</p>
<p>
Here is an example:
</p>
<div class="code_block"><pre>
<span class="PreProc">#!/usr/bin/perl -w</span>

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


<span class="Statement">my</span> <span class="Identifier">$x</span> = <span class="Constant">&quot;Hello&quot;</span>;
<span class="Statement">my</span> <span class="Identifier">$str</span> = <span class="Constant">&quot;There you go.&quot;</span>;
<span class="Statement">my</span> <span class="Identifier">$true</span> = <span class="Constant">&quot;False&quot;</span>;

<span class="Statement">print</span> <span class="Constant">&lt;&lt;&quot;END&quot;;</span>
<span class="Constant">The value of </span><span class="Special">\$</span><span class="Constant">x is: &quot;</span><span class="Identifier">$x</span><span class="Constant">&quot;</span>
<span class="Constant">The value of </span><span class="Special">\$</span><span class="Constant">str is: &quot;</span><span class="Identifier">$str</span><span class="Constant">&quot;</span>
<span class="Constant">The value of true is: &quot;</span><span class="Identifier">$true</span><span class="Constant">&quot;</span>

<span class="Constant">Hoola</span>

<span class="Constant">END</span>
</pre></div>
<p>
Its output is:
</p>
<div class="code_block"><pre>
The value of $x is: "Hello"
The value of $str is: "There you go."
The value of true is: "False"

Hoola


</pre></div>
<p>
Note that if the delimeters on the terminator after the <tt>&lt;&lt;</tt>
are double-quotes (<tt>"..."</tt>), then the here-document will interpolate,
and if they are single-quotes (<tt>'...'</tt>), it will not.
</p>
<p>
An unquoted ending string causes the here-doc to interpolate, in case
you encounter it in the wild. Note however, that in your code, you should
always quote it, so people won't have to guess what you meant.
</p>
<hr />

</div>
<div class="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--string-forms--here_doc--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--processes--system--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--processes--DIR">Executing Other Processes</a>
</div>
<h1 id="page--processes--DIR">
4. Executing Other Processes
</h1>
<p>
Perl enables one to execute other system commands while returning
control to the script. There are several different ways to do this,
and they would be covered here.
</p>
<ul class="contentsmain">
<li><a href="#page--processes--system--PAGE" class="contents">4.1. The system() Command</a>
</li><li><a href="#page--processes--backticks--PAGE" class="contents">4.2. Trapping Command Output with `...`</a>
</li><li><a href="#page--processes--opens--PAGE" class="contents">4.3. open() for Command Execution</a>
</li><li><a href="#page--processes--string-shellquote--PAGE" class="contents">4.4. String::ShellQuote</a>
</li></ul>
<hr />

</div>
<div class="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--processes--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--processes--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--processes--backticks--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--processes--DIR">Executing Other Processes</a><a href="#page--processes--system--PAGE">The system() Command</a>
</div>
<h1 id="page--processes--system--PAGE">
4.1. The system() Command
</h1>
<p>
The <tt>system()</tt> function executes a shell command, while maintaining
the same standard input, standard output and environment of the invoking
script. If called with one argument, it passes this argument as is to the
shell, which in turn will process it for special characters. If passed an
array, it will call the command in the first member with the rest of the
array as command line arguments.
</p>
<p>
If you receive an arbitrary array and you fear it may contain only
one argument, you can use the <tt>system { $cmd_line[0] } @cmd_line</tt>
notation (similar to <tt>print()</tt>'s ).
</p>
<p>
On success, system() returns 0 (not a true value) and one should make
sure to throw an exception upon failure while referencing the built-in
error variable <tt>$?</tt>.
</p>
<p>
Here are some examples, that will only work on UNIX systems.
</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">system</span>(<span class="Constant">&quot;ls -l /&quot;</span>) == <span class="Constant">0</span>)
    <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;system 'ls -l /' failed - </span><span class="Identifier">$?</span><span class="Constant">&quot;</span>;

<span class="Statement">my</span> <span class="Identifier">@args</span> = (<span class="Constant">&quot;ls&quot;</span>, <span class="Constant">&quot;-l&quot;</span>, <span class="Constant">&quot;/&quot;</span>);
(<span class="Statement">system</span>(<span class="Identifier">@args</span>) == <span class="Constant">0</span>)
    <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Could not ls -l / - </span><span class="Identifier">$?</span><span class="Constant">&quot;</span>;

(<span class="Statement">system</span>(<span class="Constant">&quot;ls -l | grep ^d | wc -l&quot;</span>) == <span class="Constant">0</span>)
    <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Could not pipeline - </span><span class="Identifier">$?</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="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--processes--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--processes--system--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--processes--opens--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--processes--DIR">Executing Other Processes</a><a href="#page--processes--backticks--PAGE">Trapping Command Output with `...`</a>
</div>
<h1 id="page--processes--backticks--PAGE">
4.2. Trapping Command Output with `...`
</h1>
<p>
The backticks (or more generally <tt>qx{ ... }</tt>), can be used to trap
the output of a shell command. It executes the command and returns
all of its output. Interpolation is used.
</p>
<p>
If assigned to a scalar, it returns the output as a complete string. If
the output is assigned to an array, the array will contain the lines of
the output.
</p>
<p>
Here is an example for a program that counts the number of directories in
a directory that is given as an argument:
</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">$dir</span> = <span class="Statement">shift</span>;
<span class="Comment"># Prepare $dir for placement inside a '...' argument</span>
<span class="Comment"># A safer way would be to use String::ShellQuote</span>
<span class="Identifier">$dir</span> =~ <span class="Statement">s!</span><span class="Constant">'</span><span class="Statement">!</span><span class="Constant">'</span><span class="Special">\\</span><span class="Constant">''</span><span class="Statement">!g</span>;

<span class="Statement">my</span> <span class="Identifier">$count</span> = <span class="Statement">`</span><span class="Constant">ls -l '</span><span class="Identifier">$dir</span><span class="Constant">' | grep ^d | wc -l</span><span class="Statement">`</span>;

<span class="Statement">if</span> (<span class="Identifier">$?</span>)
{
    <span class="Statement">die</span> <span class="Constant">&quot;Error returned by ls -l command is </span><span class="Identifier">$@</span><span class="Constant">.&quot;</span>;
}

<span class="Statement">if</span> (<span class="Identifier">$count</span> !~ <span class="Statement">/</span><span class="Special">(\d+)</span><span class="Statement">/</span>)
{
    <span class="Comment"># Retrieve the number via the special regex variable $1</span>
    <span class="Identifier">$count</span> = <span class="Identifier">$1</span>;
    <span class="Statement">print</span> <span class="Constant">&quot;There are </span><span class="Identifier">$count</span><span class="Constant"> directories</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">else</span>
{
    <span class="Statement">die</span> <span class="Constant">&quot;Wrong output.&quot;</span>
}
</pre></div>
<hr />

</div>
<div class="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--processes--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--processes--backticks--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--processes--string-shellquote--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--processes--DIR">Executing Other Processes</a><a href="#page--processes--opens--PAGE">open() for Command Execution</a>
</div>
<h1 id="page--processes--opens--PAGE">
4.3. open() for Command Execution
</h1>
<p>
The <tt>open</tt> command can be used for command execution. By prefixing
the filename with a pipe (<tt>|</tt>), the rest of it is interpreted
as a command invocation, which accepts standard input by printing to the
filehandle, and is executed after the filehandle is closed. If the last
character is a pipe, then the command is executed and its standard output
is fed into the filehandle where it can be read using Perl's file input
mechanisms.
</p>
<p>
Here are some examples:
</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">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;/sbin/ifconfig |&quot;</span>;

<span class="Statement">my</span> (<span class="Identifier">@addrs</span>);

<span class="Statement">while</span> (<span class="Statement">my</span> <span class="Identifier">$line</span> = &lt;<span class="Identifier">$in</span>&gt;)
{
    <span class="Statement">if</span> (<span class="Identifier">$line</span> =~ <span class="Statement">/</span><span class="Constant">inet addr:</span><span class="Special">((\d+\.)+\d)</span><span class="Statement">/</span>)
    {
        <span class="Statement">push</span> <span class="Identifier">@addrs</span>, <span class="Identifier">$1</span>;
    }
}
<span class="Statement">close</span>(<span class="Identifier">$in</span>);

<span class="Statement">print</span> <span class="Constant">&quot;You have the following addresses: </span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>,<span class="Identifier">@addrs</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />
<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="Comment"># Send an E-mail to myself</span>
<span class="Comment"># Note: this is just an example - there are modules to do this on CPAN.</span>

<span class="Statement">open</span> <span class="Identifier">MAIL</span>, <span class="Constant">&quot;|/usr/sbin/sendmail shlomif</span><span class="Special">\@</span><span class="Constant">shlomifish.org&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">MAIL</span> <span class="Constant">&quot;To: Shlomi Fish &lt;shlomif</span><span class="Special">\@</span><span class="Constant">shlomifish.org&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">MAIL</span> <span class="Constant">&quot;From: Shlomi Fish &lt;shlomif</span><span class="Special">\@</span><span class="Constant">shlomifish.org&gt;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">MAIL</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> <span class="Identifier">MAIL</span> <span class="Constant">&quot;Hello there, moi!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">close</span>(<span class="Identifier">MAIL</span>);
</pre></div>
<h2 id="open-pipe-list">Pipe to @args</h2>
<p>
Recent versions of Perl also have a syntax that allows opening a process
for input or output using its command line arguments. These are:
</p>
<div class="code_block"><pre>
open my $print_to_process, "|-", $cmd, @args;
print {$print_to_process} ...;
</pre></div>
<p>
and:
</p>
<div class="code_block"><pre>
open my $read_from_process, "-|", $cmd, @args;
while (my $line = &lt;$read_from_process&gt;)
{
.
.
.
}
</pre></div>
<p>
Doing something like
<tt>open my $print_to_process, "|-", "sendmail", $to_address;</tt>
is safer than doing:
<tt>open my $print_to_process, "|-", "sendmail $to_address";</tt>
Because a malicious person may put some offending shell characters in
<tt>$to_address</tt> and end up with something like:
</p>
<div class="code_block"><pre>
sendmail ; rm -fr $HOME
</pre></div>
<hr />

</div>
<div class="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--processes--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--processes--opens--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--and_or--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--processes--DIR">Executing Other Processes</a><a href="#page--processes--string-shellquote--PAGE">String::ShellQuote</a>
</div>
<h1 id="page--processes--string-shellquote--PAGE">
4.4. String::ShellQuote
</h1>
<p>
When invoking raw shell commands (instead of passing a list of command
line arguments) one can easily cause a situation where an interpolated
string given as argument will place arbitrary code in the shell. If
for example we have the following qx call:
</p>
<div class="code_block"><pre>
my $ls_output = qx/ls '$dir'/;
</pre></div>
<p>
Then <tt>$dir</tt> may be set to "<tt>' ; rm -fr ~ ; '</tt>",
which will make the shell delete our entire home directory.
</p>
<p>
To overcome such problems, one should make use of
<a href="http://search.cpan.org/dist/String-ShellQuote/">the
String-ShellQuote module</a> which provides functions for safely
preventing shell-code injection.
</p>
<hr />

</div>
<div class="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--processes--string-shellquote--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--and_or--sort--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--and_or--DIR">More about || and &amp;&amp;</a>
</div>
<h1 id="page--and_or--DIR">
5. More about || and &amp;&amp;
</h1>
<p>
<tt>||</tt> and <tt>&amp;&amp;</tt> return the last argument that was
evaluated. Thus, <tt>||</tt> is useful for assigning default values,
like this:
</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="Comment"># shift by default shifts from @ARGV in the main program</span>
<span class="Statement">my</span> <span class="Identifier">$start</span> = <span class="Statement">shift</span> || <span class="Constant">1</span>;
<span class="Statement">my</span> <span class="Identifier">$end</span> = <span class="Statement">shift</span> || (<span class="Identifier">$start</span>+<span class="Constant">9</span>);

<span class="Statement">for</span> <span class="Statement">my</span> <span class="Identifier">$i</span> (<span class="Identifier">$start</span> .. <span class="Identifier">$end</span>)
{
    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Identifier">$i</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<ul class="contentsmain">
<li><a href="#page--and_or--sort--PAGE" class="contents">5.1. For sort()</a>
</li><li><a href="#page--and_or--english_and_or--PAGE" class="contents">5.2. The "and" and "or" Operators</a>
</li></ul>
<hr />

</div>
<div class="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--and_or--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--and_or--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--and_or--english_and_or--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--and_or--DIR">More about || and &amp;&amp;</a><a href="#page--and_or--sort--PAGE">For sort()</a>
</div>
<h1 id="page--and_or--sort--PAGE">
5.1. For sort()
</h1>
<p>
The <tt>||</tt> operator can be used in <tt>sort</tt>, in conjunction
with operators such as <tt>cmp</tt> or <tt>&lt;=&gt;</tt> to sort
according to several criteria. For example, if you wish to sort
according to the last name and if this is equal according to the first
name as well, you can write the following:
</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">@array</span> =
(
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Amanda&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Smith&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Jane&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Arden&quot;</span>,},
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Tony&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Hoffer&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Shlomi&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Fish&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Chip&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Fish&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;John&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Smith&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Peter&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Torry&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Michael&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Hoffer&quot;</span>, },
    { <span class="Constant">'first'</span> =&gt; <span class="Constant">&quot;Ben&quot;</span>, <span class="Constant">'last'</span> =&gt; <span class="Constant">&quot;Smith&quot;</span>, },
);

<span class="Statement">my</span> <span class="Identifier">@sorted_array</span> =
    (<span class="Statement">sort</span>
        {
            (<span class="Identifier">$a-&gt;{</span><span class="Constant">'last'</span><span class="Identifier">}</span> <span class="Statement">cmp</span> <span class="Identifier">$b-&gt;{</span><span class="Constant">'last'</span><span class="Identifier">}</span>) ||
            (<span class="Identifier">$a-&gt;{</span><span class="Constant">'first'</span><span class="Identifier">}</span> <span class="Statement">cmp</span> <span class="Identifier">$b-&gt;{</span><span class="Constant">'first'</span><span class="Identifier">}</span>)
        }
        <span class="Identifier">@array</span>
    );

<span class="Statement">foreach</span> <span class="Statement">my</span> <span class="Identifier">$record</span> (<span class="Identifier">@sorted_array</span>)
{
    <span class="Statement">print</span> <span class="Identifier">$record-&gt;{</span><span class="Constant">'last'</span><span class="Identifier">}</span> . <span class="Constant">&quot;, &quot;</span> . <span class="Identifier">$record-&gt;{</span><span class="Constant">'first'</span><span class="Identifier">}</span> . <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
</pre></div>
<p>
Its output is:
</p>
<div class="code_block"><pre>
Arden, Jane
Fish, Chip
Fish, Shlomi
Hoffer, Michael
Hoffer, Tony
Smith, Amanda
Smith, Ben
Smith, John
Torry, Peter
</pre></div>
<hr />

</div>
<div class="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--and_or--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--and_or--sort--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--exceptions--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--and_or--DIR">More about || and &amp;&amp;</a><a href="#page--and_or--english_and_or--PAGE">The "and" and "or" Operators</a>
</div>
<h1 id="page--and_or--english_and_or--PAGE">
5.2. The "and" and "or" Operators
</h1>
<p>
Perl supplies two operators <tt>and</tt> and <tt>or</tt> which are equivalent
to <tt>&amp;&amp;</tt> and <tt>||</tt> except that they have a very low
precedence. (lower than any other operator in fact). There's also <tt>not</tt>
which is the ultra-low precedence equivalent of <tt>!</tt>.
</p>
<p>
You can use them after a statement to write error handlers.
</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="Comment"># Terminate if we cannot open a file.</span>
<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;/hello.txt&quot;</span> <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Cannot open file!&quot;</span>;

<span class="Statement">print</span> <span class="Identifier">O</span> <span class="Constant">&quot;Hello World!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

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

</div>
<div class="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--and_or--english_and_or--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--exceptions--die_and_eval--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--exceptions--DIR">Exceptions</a>
</div>
<h1 id="page--exceptions--DIR">
6. Exceptions
</h1>
<p>
Exceptions are a mechanism to raise an error in a program which will propagate
to outside blocks, until it is caught by an explicit catching block, or
it terminates the program. It is a convenient way to manage errors.
</p>
<p>
In Perl, there is a statement that throws an exception, and one that catches
it. The exception may escape out of function calls as well.
</p>
<ul class="contentsmain">
<li><a href="#page--exceptions--die_and_eval--PAGE" class="contents">6.1. die and eval</a>
</li><li><a href="#page--exceptions--carp--PAGE" class="contents">6.2. The Carp module</a>
</li><li><a href="#page--exceptions--error.pm--PAGE" class="contents">6.3. The Error.pm module</a>
</li></ul>
<hr />

</div>
<div class="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--exceptions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--exceptions--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--exceptions--carp--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--exceptions--DIR">Exceptions</a><a href="#page--exceptions--die_and_eval--PAGE">die and eval</a>
</div>
<h1 id="page--exceptions--die_and_eval--PAGE">
6.1. die and eval
</h1>
<p>
The statement <tt>die</tt> throws an exception which can be any Perl
scalar. The statement <tt>eval { ... }</tt> catches an excpetion that was
given inside it, and after it sets the special variable <tt>$@</tt>
to be the value of the exception or undef if none was caught.
</p>
<p>
Here's an example:
</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">sub </span><span class="Identifier">read_text</span>
{
    <span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Constant">&quot;../hello/there.txt&quot;</span> ;
    <span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;&lt;</span><span class="Identifier">$filename</span><span class="Constant">&quot;</span>
        <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Could not open </span><span class="Identifier">$filename</span><span class="Constant">&quot;</span>;
    <span class="Statement">my</span> <span class="Identifier">$text</span> = <span class="Statement">join</span>(<span class="Constant">&quot;&quot;</span>,<span class="Identifier">&lt;I&gt;</span>);
    <span class="Statement">close</span>(<span class="Identifier">I</span>);

    <span class="Statement">return</span> <span class="Identifier">$text</span>;
}

<span class="Statement">sub </span><span class="Identifier">write_text</span>
{
    <span class="Statement">my</span> <span class="Identifier">$text</span> = <span class="Statement">shift</span>;
    <span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Constant">&quot;../there/hello.txt&quot;</span>;
    <span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;&gt;</span><span class="Identifier">$filename</span><span class="Constant">&quot;</span>
        <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Could not open </span><span class="Identifier">$filename</span><span class="Constant"> for writing&quot;</span>;
    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Identifier">$text</span>;
    <span class="Statement">close</span>(<span class="Identifier">O</span>);
}

<span class="Statement">sub </span><span class="Identifier">read_and_write</span>
{
    <span class="Statement">my</span> <span class="Identifier">$text</span> = read_text();

    write_text(<span class="Identifier">$text</span>);
}

<span class="Statement">sub </span><span class="Identifier">perform_transaction</span>
{
    <span class="Statement">eval</span> {
    read_and_write();
    };
    <span class="Statement">if</span> (<span class="Identifier">$@</span>)
    {
        <span class="Statement">print</span> <span class="Constant">&quot;Could not perform the transaction. Reason is:</span><span class="Special">\n</span><span class="Identifier">$@</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
    }
}

perform_transaction();
</pre></div>
<hr />

</div>
<div class="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--exceptions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--exceptions--die_and_eval--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--exceptions--error.pm--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--exceptions--DIR">Exceptions</a><a href="#page--exceptions--carp--PAGE">The Carp module</a>
</div>
<h1 id="page--exceptions--carp--PAGE">
6.2. The Carp module
</h1>
<p>
The <tt>Carp</tt> module warns or throws errors with a more useful information
that the normal Perl behaviour. It supplies several such methods. For more
information run <tt>perldoc Carp</tt>.
</p>
<hr />

</div>
<div class="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--exceptions--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--exceptions--carp--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--DIR" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--exceptions--DIR">Exceptions</a><a href="#page--exceptions--error.pm--PAGE">The Error.pm module</a>
</div>
<h1 id="page--exceptions--error.pm--PAGE">
6.3. The Error.pm module
</h1>
<p>
The <a href="http://search.cpan.org/dist/Error/">Error.pm</a>
module, which is available from CPAN, supplies object oriented
exception handling. Namely, one can catch exceptions of a certain class
explicitly, and differentiate between several types of exceptions.
</p>
<p>
Error.pm provides a lot of syntactic sugar that tends to break easily.
As such, its use is not too recommended.
</p>
<p>
On the other side, there's the
<a href="http://search.cpan.org/dist/Exception-Class/">Exception-Class
module</a> which provides object-oriented exceptions with no special
syntactic sugar, and which works very well. Its use is highly recommended.
</p>
<p>
Throwing objects which are associated with classes is a good way to be able
to handle one's exceptions programatically .
</p>
<hr />

</div>
<div class="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--exceptions--error.pm--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--dir--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--system-funcs--DIR">More System Functions</a>
</div>
<h1 id="page--system-funcs--DIR">
7. More System Functions
</h1>
<p>
Perl supplies the user with many functions useful for performing system
tasks. This section will cover some of them for your own reference.
</p>
<ul class="contentsmain">
<li><a href="#page--system-funcs--dir--PAGE" class="contents">7.1. Directory Input Routines</a>
</li><li><a href="#page--system-funcs--random_io--PAGE" class="contents">7.2. Random File I/O</a>
</li><li><a href="#page--system-funcs--file-tests--PAGE" class="contents">7.3. File Tests (-e, -d...)</a>
</li><li><a href="#page--system-funcs--chdir_getcwd_mkdir--PAGE" class="contents">7.4. chdir(), getcwd() and mkdir().</a>
</li><li><a href="#page--system-funcs--stat--PAGE" class="contents">7.5. The stat() Function</a>
</li></ul>
<hr />

</div>
<div class="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--system-funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--system-funcs--DIR" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--random_io--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--system-funcs--DIR">More System Functions</a><a href="#page--system-funcs--dir--PAGE">Directory Input Routines</a>
</div>
<h1 id="page--system-funcs--dir--PAGE">
7.1. Directory Input Routines
</h1>
<p>
The <tt>opendir DIRHANDLE, EXPR</tt> function can be used to open the
directory <tt>EXPR</tt> for reading its file and sub-directory entries.
Afterwards <tt>readdir(DIRHANDLE)</tt> can be used to read one entry
from there, or all the entries if used in list context.
</p>
<p>
Use <tt>closedir()</tt> to close an opened directory.
</p>
<p>
Here's an example that counts the number of mp3s in a directory:
</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">sub </span><span class="Identifier">get_dir_files</span>
{
    <span class="Statement">my</span> <span class="Identifier">$dir_path</span> = <span class="Statement">shift</span>;

    <span class="Statement">opendir</span> <span class="Identifier">D</span>, <span class="Identifier">$dir_path</span>
        <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Cannot open the directory </span><span class="Identifier">$dir_path</span><span class="Constant">&quot;</span>;

    <span class="Statement">my</span> <span class="Identifier">@entries</span>;
    <span class="Identifier">@entries</span> = <span class="Statement">readdir</span>(<span class="Identifier">D</span>);
    <span class="Statement">closedir</span>(<span class="Identifier">D</span>);

    <span class="Statement">return</span> \<span class="Identifier">@entries</span>;
}

<span class="Statement">my</span> <span class="Identifier">$dir_path</span> = <span class="Statement">shift</span> || <span class="Constant">&quot;.&quot;</span>;

<span class="Statement">my</span> <span class="Identifier">$entries</span> = get_dir_files(<span class="Identifier">$dir_path</span>);
<span class="Statement">my</span> <span class="Identifier">@mp3s</span> = (<span class="Statement">grep {</span> <span class="Statement">/</span><span class="Special">\.</span><span class="Constant">mp3$</span><span class="Statement">/</span> <span class="Statement">}</span> <span class="Identifier">@$entries</span>);

<span class="Statement">print</span> <span class="Constant">&quot;You have &quot;</span> . <span class="Statement">scalar</span>(<span class="Identifier">@mp3s</span>) . <span class="Constant">&quot; mp3s in </span><span class="Identifier">$dir_path</span><span class="Constant">.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
</pre></div>
<hr />

</div>
<div class="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--system-funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--system-funcs--dir--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--file-tests--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--system-funcs--DIR">More System Functions</a><a href="#page--system-funcs--random_io--PAGE">Random File I/O</a>
</div>
<h1 id="page--system-funcs--random_io--PAGE">
7.2. Random File I/O
</h1>
<p>
Perl provides mechanisms for moving to certain positions in files, and
reading blocks of a certain size.
</p>
<p>
<tt>seek FILEHANDLE, POSITION, WHENCE</tt> sets the filehandle position
within the file in bytes. If you specify <tt>use Fcntl;</tt> at
the beginning of your program, then WHENCE can be <tt>SEEK_SET</tt> for
start of file, <tt>SEEK_CUR</tt> for the current position and
<tt>SEEK_END</tt> for the end of file.
</p>
<p>
<tt>tell FILEHANDLE</tt> returns the position of the current file cursor
in bytes from the beginning of the file.
</p>
<p>
<tt>read FILEHANDLE, SCALAR, LENGTH</tt> reads <tt>LENGTH</tt> characters
from <tt>FILEHANDLE</tt> into the <tt>SCALAR</tt> variable.
</p>
<p>
Here's an example that replaces bytes 64-127 in a file with their rot13
equivalent:
</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">use </span>Fcntl;

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

<span class="Statement">open</span> <span class="Identifier">F</span>, <span class="Constant">&quot;+&lt;</span><span class="Identifier">$filename</span><span class="Constant">&quot;</span>
    <span class="Statement">or</span> <span class="Statement">die</span> <span class="Constant">&quot;Could not open file&quot;</span>;

<span class="Comment"># Read bytes 64-127 into $text</span>
<span class="Statement">seek</span>(<span class="Identifier">F</span>, <span class="Constant">64</span>, SEEK_SET);

<span class="Statement">my</span> <span class="Identifier">$text</span>;
<span class="Statement">read</span>(<span class="Identifier">F</span>,<span class="Identifier">$text</span>,<span class="Constant">64</span>);
<span class="Comment"># Do the actual rot13'ing with the tr command</span>
<span class="Identifier">$text</span> =~ <span class="Statement">tr/</span><span class="Constant">A-Za-z</span><span class="Statement">/</span><span class="Constant">N-ZA-Mn-za-m</span><span class="Statement">/</span>;
<span class="Comment"># Write them at position 64</span>
<span class="Statement">seek</span>(<span class="Identifier">F</span>, <span class="Constant">64</span>, SEEK_SET);
<span class="Statement">print</span> <span class="Identifier">F</span> <span class="Identifier">$text</span>
<span class="Statement">close</span>(<span class="Identifier">F</span>);
</pre></div>
<hr />

</div>
<div class="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--system-funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--system-funcs--random_io--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--chdir_getcwd_mkdir--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--system-funcs--DIR">More System Functions</a><a href="#page--system-funcs--file-tests--PAGE">File Tests (-e, -d...)</a>
</div>
<h1 id="page--system-funcs--file-tests--PAGE">
7.3. File Tests (-e, -d...)
</h1>
<p>
Perl provides several file tests that can be used to test a filehandle
or filename for various conditions. <tt>-e filename</tt> determines if
<tt>filename</tt> exists. <tt>-r</tt> determines if the file is readable,
<tt>-w</tt> if it's writeable, <tt>-x</tt> if it is executable and so on.
</p>
<p>
<tt>-d</tt> determines if the file is a directory, and <tt>-f</tt> if it's
a plain file. For a list of other file tests and their use consult
<tt>perldoc -f -X</tt>.
</p>
<hr />

</div>
<div class="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--system-funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--system-funcs--file-tests--PAGE" class="nav" accesskey="p">Prev</a></td>
<td>
<a href="#page--system-funcs--stat--PAGE" class="nav" accesskey="n">Next</a></td>
</tr></table>
<div class="bread">
<a href="#page--DIR">"Perl for Perl Newbies" - Part 4</a><a href="#page--system-funcs--DIR">More System Functions</a><a href="#page--system-funcs--chdir_getcwd_mkdir--PAGE">chdir(), getcwd() and mkdir().</a>
</div>
<h1 id="page--system-funcs--chdir_getcwd_mkdir--PAGE">
7.4. chdir(), getcwd() and mkdir().
</h1>
<p>
The built-in function <tt>chdir EXPR</tt> can be used to change the working
directory of the program to a new value. If <tt>EXPR</tt> is omitted
it changes to the home directory.
</p>
<p>
By <tt>use</tt>'ing the Cwd module, one can invoke the <tt>getcwd()</tt>
function that will retrieve the current working directory. This is similar
to the <tt>pwd</tt> command on UNIX shells.
</p>
<p>
Finally, <tt>mkdir FILENAME, MASK</tt> can be used to create a new directory
with the permissions mask of <tt>MASK</tt>
</p>
<hr />

</div>
<div class="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--system-funcs--DIR" class="nav" accesskey="u">Up</a></td>
<td>
<a href="#page--system-funcs--chdir_getcwd_mkdir--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 4</a><a href="#page--system-funcs--DIR">More System Functions</a><a href="#page--system-funcs--stat--PAGE">The stat() Function</a>
</div>
<h1 id="page--system-funcs--stat--PAGE">
7.5. The stat() Function
</h1>
<p>
The <tt>stat</tt> function can be used to retrieve a 13-element array
that gives status information for a given file or filehandle.
</p>
<p>
The syntax is:
</p>
<div class="code_block"><pre>
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)
        = stat($filename);
</pre></div>
<p>
Here, <tt>$mode</tt> is the file mode, <tt>$size</tt> is the size of the
file, <tt>$atime</tt> is the last access time (in seconds since the epoch),
<tt>$mtime</tt> is the last modification time.
</p>
<p>
For more information about <tt>stat</tt> consult <tt>perldoc -f stat</tt>
or its entry in <tt>perlfunc</tt>.
</p>
<hr />

</div>

</body></html>