This file is indexed.

/usr/share/doc/perl-doc-html/perlguts.html is in perl-doc-html 5.22.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlguts - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlguts</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 22.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-internals.html">Internals and C language interface</a> &gt;
      
    
    perlguts
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlguts</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Variables">Variables</a><ul><li><a href="#Datatypes">Datatypes</a><li><a href="#What-is-an-%22IV%22%3f">What is an "IV"?</a><li><a href="#Working-with-SVs">Working with SVs</a><li><a href="#Offsets">Offsets</a><li><a href="#What's-Really-Stored-in-an-SV%3f">What's Really Stored in an SV?</a><li><a href="#Working-with-AVs">Working with AVs</a><li><a href="#Working-with-HVs">Working with HVs</a><li><a href="#Hash-API-Extensions">Hash API Extensions</a><li><a href="#AVs%2c-HVs-and-undefined-values">AVs, HVs and undefined values</a><li><a href="#References">References</a><li><a href="#Blessed-References-and-Class-Objects">Blessed References and Class Objects</a><li><a href="#Creating-New-Variables">Creating New Variables</a><li><a href="#Reference-Counts-and-Mortality">Reference Counts and Mortality</a><li><a href="#Stashes-and-Globs">Stashes and Globs</a><li><a href="#Double-Typed-SVs">Double-Typed SVs</a><li><a href="#Read-Only-Values">Read-Only Values</a><li><a href="#Copy-on-Write">Copy on Write</a><li><a href="#Magic-Variables">Magic Variables</a><li><a href="#Assigning-Magic">Assigning Magic</a><li><a href="#Magic-Virtual-Tables">Magic Virtual Tables</a><li><a href="#Finding-Magic">Finding Magic</a><li><a href="#Understanding-the-Magic-of-Tied-Hashes-and-Arrays">Understanding the Magic of Tied Hashes and Arrays</a><li><a href="#Localizing-changes">Localizing changes</a></ul><li><a href="#Subroutines">Subroutines</a><ul><li><a href="#XSUBs-and-the-Argument-Stack">XSUBs and the Argument Stack</a><li><a href="#Autoloading-with-XSUBs">Autoloading with XSUBs</a><li><a href="#Calling-Perl-Routines-from-within-C-Programs">Calling Perl Routines from within C Programs</a><li><a href="#Putting-a-C-value-on-Perl-stack">Putting a C value on Perl stack</a><li><a href="#Scratchpads">Scratchpads</a><li><a href="#Scratchpads-and-recursion">Scratchpads and recursion</a></ul><li><a href="#Memory-Allocation">Memory Allocation</a><ul><li><a href="#Allocation">Allocation</a><li><a href="#Reallocation">Reallocation</a><li><a href="#Moving">Moving</a></ul><li><a href="#PerlIO">PerlIO</a><li><a href="#Compiled-code">Compiled code</a><ul><li><a href="#Code-tree">Code tree</a><li><a href="#Examining-the-tree">Examining the tree</a><li><a href="#Compile-pass-1%3a-check-routines">Compile pass 1: check routines</a><li><a href="#Compile-pass-1a%3a-constant-folding">Compile pass 1a: constant folding</a><li><a href="#Compile-pass-2%3a-context-propagation">Compile pass 2: context propagation</a><li><a href="#Compile-pass-3%3a-peephole-optimization">Compile pass 3: peephole optimization</a><li><a href="#Pluggable-runops">Pluggable runops</a><li><a href="#Compile-time-scope-hooks">Compile-time scope hooks</a></ul><li><a href="#Examining-internal-data-structures-with-the-dump-functions">Examining internal data structures with the dump functions</a><li><a href="#How-multiple-interpreters-and-concurrency-are-supported">How multiple interpreters and concurrency are supported</a><ul><li><a href="#Background-and-PERL_IMPLICIT_CONTEXT">Background and PERL_IMPLICIT_CONTEXT</a><li><a href="#So-what-happened-to-dTHR%3f">So what happened to dTHR?</a><li><a href="#How-do-I-use-all-this-in-extensions%3f">How do I use all this in extensions?</a><li><a href="#Should-I-do-anything-special-if-I-call-perl-from-multiple-threads%3f">Should I do anything special if I call perl from multiple threads?</a><li><a href="#Future-Plans-and-PERL_IMPLICIT_SYS">Future Plans and PERL_IMPLICIT_SYS</a></ul><li><a href="#Internal-Functions">Internal Functions</a><ul><li><a href="#Formatted-Printing-of-IVs%2c-UVs%2c-and-NVs">Formatted Printing of IVs, UVs, and NVs</a><li><a href="#Pointer-To-Integer-and-Integer-To-Pointer">Pointer-To-Integer and Integer-To-Pointer</a><li><a href="#Exception-Handling">Exception Handling</a><li><a href="#Source-Documentation">Source Documentation</a><li><a href="#Backwards-compatibility">Backwards compatibility</a></ul><li><a href="#Unicode-Support">Unicode Support</a><ul><li><a href="#What-*is*-Unicode%2c-anyway%3f">What *is* Unicode, anyway?</a><li><a href="#How-can-I-recognise-a-UTF-8-string%3f">How can I recognise a UTF-8 string?</a><li><a href="#How-does-UTF-8-represent-Unicode-characters%3f">How does UTF-8 represent Unicode characters?</a><li><a href="#How-does-Perl-store-UTF-8-strings%3f">How does Perl store UTF-8 strings?</a><li><a href="#How-do-I-convert-a-string-to-UTF-8%3f">How do I convert a string to UTF-8?</a><li><a href="#How-do-I-compare-strings%3f">How do I compare strings?</a><li><a href="#Is-there-anything-else-I-need-to-know%3f">Is there anything else I need to know?</a></ul><li><a href="#Custom-Operators">Custom Operators</a><li><a href="#AUTHORS">AUTHORS</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlguts - Introduction to the Perl API</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document attempts to describe how to use the Perl API, as well as
to provide some info on the basic workings of the Perl core.  It is far
from complete and probably contains many errors.  Please refer any
questions or comments to the author below.</p>
<a name="Variables"></a><h1>Variables</h1>
<a name="Datatypes"></a><h2>Datatypes</h2>
<p>Perl has three typedefs that handle Perl's three main data types:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>  <span class="w">Scalar</span> <span class="w">Value</span></li><li>    <span class="w">AV</span>  <span class="w">Array</span> <span class="w">Value</span></li><li>    <span class="w">HV</span>  <span class="w">Hash</span> <span class="w">Value</span></li></ol></pre><p>Each typedef has specific routines that manipulate the various data types.</p>
<a name="What-is-an-%22IV%22%3f"></a><h2>What is an "IV"?</h2>
<p>Perl uses a special typedef IV which is a simple signed integer type that is
guaranteed to be large enough to hold a pointer (as well as an integer).
Additionally, there is the UV, which is simply an unsigned IV.</p>
<p>Perl also uses two special typedefs, I32 and I16, which will always be at
least 32-bits and 16-bits long, respectively.  (Again, there are U32 and U16,
as well.)  They will usually be exactly 32 and 16 bits long, but on Crays
they will both be 64 bits.</p>
<a name="Working-with-SVs"></a><h2>Working with SVs</h2>
<p>An SV can be created and loaded with one command.  There are five types of
values that can be loaded: an integer value (IV), an unsigned integer
value (UV), a double (NV), a string (PV), and another scalar (SV).
("PV" stands for "Pointer Value".  You might think that it is misnamed
because it is described as pointing only to strings.  However, it is
possible to have it point to other things.  For example, it could point
to an array of UVs.  But,
using it for non-strings requires care, as the underlying assumption of
much of the internals is that PVs are just for strings.  Often, for
example, a trailing <code class="inline"><span class="w">NUL</span></code>
 is tacked on automatically.  The non-string use
is documented only in this paragraph.)</p>
<p>The seven routines are:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>*  <span class="i">newSViv</span><span class="s">(</span><span class="w">IV</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVuv</span><span class="s">(</span><span class="w">UV</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVnv</span><span class="s">(</span><span class="w">double</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVpv</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVpvn</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVpvf</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*  <span class="i">newSVsv</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">STRLEN</span></code>
 is an integer type (Size_t, usually defined as size_t in
<i>config.h</i>) guaranteed to be large enough to represent the size of
any string that perl can handle.</p>
<p>In the unlikely case of a SV requiring more complex initialization, you
can create an empty SV with newSV(len).  If <code class="inline"><span class="w">len</span></code>
 is 0 an empty SV of
type NULL is returned, else an SV of type PV is returned with len + 1 (for
the <code class="inline"><span class="w">NUL</span></code>
) bytes of storage allocated, accessible via SvPVX.  In both cases
the SV has the undef value.</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span> *<span class="w">sv</span> = <span class="i">newSV</span><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span>   <span class="q">/* no storage allocated  */</span></li><li>    <span class="w">SV</span> *<span class="w">sv</span> = <span class="i">newSV</span><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span>  <span class="q">/* 10 (+1) bytes of uninitialised storage</span></li><li>                          <span class="q">                          * allocated */</span></li></ol></pre><p>To change the value of an <i>already-existing</i> SV, there are eight routines:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span>  <span class="i">sv_setiv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">IV</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_setuv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">UV</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_setnv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">double</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_setpv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_setpvn</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="s">)</span></li><li>    <span class="w">void</span>  <span class="i">sv_setpvf</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_vsetpvfn</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="cm">,</span> <span class="w">va_list</span> *<span class="cm">,</span></li><li>                                                    <span class="w">SV</span> **<span class="cm">,</span> <span class="w">I32</span><span class="cm">,</span> <span class="w">bool</span> *<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_setsv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">SV</span>*<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Notice that you can choose to specify the length of the string to be
assigned by using <code class="inline"><span class="w">sv_setpvn</span></code>
, <code class="inline"><span class="w">newSVpvn</span></code>
, or <code class="inline"><span class="w">newSVpv</span></code>
, or you may
allow Perl to calculate the length by using <code class="inline"><span class="w">sv_setpv</span></code>
 or by specifying
0 as the second argument to <code class="inline"><span class="w">newSVpv</span></code>
.  Be warned, though, that Perl will
determine the string's length by using <code class="inline"><span class="w">strlen</span></code>
, which depends on the
string terminating with a <code class="inline"><span class="w">NUL</span></code>
 character, and not otherwise containing
NULs.</p>
<p>The arguments of <code class="inline"><span class="w">sv_setpvf</span></code>
 are processed like <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code>, and the
formatted output becomes the value.</p>
<p><code class="inline"><span class="w">sv_vsetpvfn</span></code>
 is an analogue of <code class="inline"><span class="w">vsprintf</span></code>
, but it allows you to specify
either a pointer to a variable argument list or the address and length of
an array of SVs.  The last argument points to a boolean; on return, if that
boolean is true, then locale-specific information has been used to format
the string, and the string's contents are therefore untrustworthy (see
<a href="perlsec.html">perlsec</a>).  This pointer may be NULL if that information is not
important.  Note that this function requires you to specify the length of
the format.</p>
<p>The <code class="inline"><span class="w">sv_set</span>*<span class="s">(</span><span class="s">)</span></code>
 functions are not generic enough to operate on values
that have "magic".  See <a href="#Magic-Virtual-Tables">Magic Virtual Tables</a> later in this document.</p>
<p>All SVs that contain strings should be terminated with a <code class="inline"><span class="w">NUL</span></code>
 character.
If it is not <code class="inline"><span class="w">NUL</span></code>
-terminated there is a risk of
core dumps and corruptions from code which passes the string to C
functions or system calls which expect a <code class="inline"><span class="w">NUL</span></code>
-terminated string.
Perl's own functions typically add a trailing <code class="inline"><span class="w">NUL</span></code>
 for this reason.
Nevertheless, you should be very careful when you pass a string stored
in an SV to a C function or system call.</p>
<p>To access the actual value that an SV points to, you can use the macros:</p>
<pre class="verbatim"><ol><li>    SvIV(SV*)</li><li>    SvUV(SV*)</li><li>    SvNV(SV*)</li><li>    SvPV(SV*, STRLEN len)</li><li>    SvPV_nolen(SV*)</li></ol></pre><p>which will automatically coerce the actual scalar type into an IV, UV, double,
or string.</p>
<p>In the <code class="inline"><span class="w">SvPV</span></code>
 macro, the length of the string returned is placed into the
variable <code class="inline"><span class="w">len</span></code>
 (this is a macro, so you do <i>not</i> use <code class="inline"><span class="i">&amp;len</span></code>
).  If you do
not care what the length of the data is, use the <code class="inline"><span class="w">SvPV_nolen</span></code>
 macro.
Historically the <code class="inline"><span class="w">SvPV</span></code>
 macro with the global variable <code class="inline"><span class="w">PL_na</span></code>
 has been
used in this case.  But that can be quite inefficient because <code class="inline"><span class="w">PL_na</span></code>
 must
be accessed in thread-local storage in threaded Perl.  In any case, remember
that Perl allows arbitrary strings of data that may both contain NULs and
might not be terminated by a <code class="inline"><span class="w">NUL</span></code>
.</p>
<p>Also remember that C doesn't allow you to safely say <code class="inline">foo(SvPV(s, len),
len);</code>.  It might work with your
compiler, but it won't work for everyone.
Break this sort of statement up into separate assignments:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span> *<span class="q">s;</span></li><li>    <span class="q">    STRLEN len;</span></li><li>    <span class="q">    char *ptr;</span></li><li>    <span class="w">ptr</span> = <span class="i">SvPV</span><span class="s">(</span><span class="q">s, len);</span></li><li>    <span class="q">    foo(ptr, len);</span></li></ol></pre><p>If you want to know if the scalar value is TRUE, you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvTRUE</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>Although Perl will automatically grow strings for you, if you need to force
Perl to allocate more memory for your SV, you can use the macro</p>
<pre class="verbatim"><ol><li>    <span class="i">SvGROW</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">STRLEN</span> <span class="w">newlen</span><span class="s">)</span></li></ol></pre><p>which will determine if more memory needs to be allocated.  If so, it will
call the function <code class="inline"><span class="w">sv_grow</span></code>
.  Note that <code class="inline"><span class="w">SvGROW</span></code>
 can only increase, not
decrease, the allocated memory of an SV and that it does not automatically
add space for the trailing <code class="inline"><span class="w">NUL</span></code>
 byte (perl's own string functions typically do
<code class="inline"><span class="i">SvGROW</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span> + <span class="n">1</span><span class="s">)</span></code>
).</p>
<p>If you want to write to an existing SV's buffer and set its value to a
string, use SvPV_force() or one of its variants to force the SV to be
a PV.  This will remove any of various types of non-stringness from
the SV while preserving the content of the SV in the PV.  This can be
used, for example, to append data from an API function to a buffer
without extra copying:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="w">void</span><span class="s">)</span><span class="i">SvPVbyte_force</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="q">s = SvGROW(sv, len + needlen + 1);</span></li><li>    <span class="q">    /* something that modifies up to needlen bytes at s+len, but</span></li><li>       <span class="q">       modifies newlen bytes</span></li><li>         <span class="q">         eg. newlen = read(fd, s + len, needlen);</span></li><li>       <span class="q">       ignoring errors for these examples</span></li><li>     <span class="q">     */</span></li><li>    <span class="q">    s[len + newlen] =</span> <span class="q">&#39;\0&#39;</span><span class="sc">;</span></li><li>    <span class="i">SvCUR_set</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span> + <span class="w">newlen</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">SvUTF8_off</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">SvSETMAGIC</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you already have the data in memory or if you want to keep your
code simple, you can use one of the sv_cat*() variants, such as
sv_catpvn().  If you want to insert anywhere in the string you can use
sv_insert() or sv_insert_flags().</p>
<p>If you don't need the existing content of the SV, you can avoid some
copying with:</p>
<pre class="verbatim"><ol><li>    <span class="i">sv_setpvn</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="q">&quot;&quot;</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="q">s = SvGROW(sv, needlen + 1);</span></li><li>    <span class="q">    /* something that modifies up to needlen bytes at s, but modifies</span></li><li>       <span class="q">       newlen bytes</span></li><li>         <span class="q">         eg. newlen = read(fd, s. needlen);</span></li><li>     <span class="q">     */</span></li><li>    <span class="q">    s[newlen] =</span> <span class="q">&#39;\0&#39;</span><span class="sc">;</span></li><li>    <span class="i">SvCUR_set</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">newlen</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">SvPOK_only</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span> <span class="q">/* also clears SVf_UTF8 */</span></li><li>    <span class="i">SvSETMAGIC</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Again, if you already have the data in memory or want to avoid the
complexity of the above, you can use sv_setpvn().</p>
<p>If you have a buffer allocated with Newx() and want to set that as the
SV's value, you can use sv_usepvn_flags().  That has some requirements
if you want to avoid perl re-allocating the buffer to fit the trailing
NUL:</p>
<pre class="verbatim"><ol><li>   <span class="i">Newx</span><span class="s">(</span><span class="w">buf</span><span class="cm">,</span> <span class="w">somesize</span>+<span class="n">1</span><span class="cm">,</span> <span class="w">char</span><span class="s">)</span><span class="sc">;</span></li><li>   <span class="q">/* ... fill in buf ... */</span></li><li>   <span class="w">buf</span><span class="s">[</span><span class="w">somesize</span><span class="s">]</span> = <span class="q">&#39;\0&#39;</span><span class="sc">;</span></li><li>   <span class="i">sv_usepvn_flags</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">buf</span><span class="cm">,</span> <span class="w">somesize</span><span class="cm">,</span> <span class="w">SV_SMAGIC</span> | <span class="w">SV_HAS_TRAILING_NUL</span><span class="s">)</span><span class="sc">;</span></li><li>   <span class="q">/* buf now belongs to perl, don&#39;t release it */</span></li></ol></pre><p>If you have an SV and want to know what kind of data Perl thinks is stored
in it, you can use the following macros to check the type of SV you have.</p>
<pre class="verbatim"><ol><li>    SvIOK(SV*)</li><li>    SvNOK(SV*)</li><li>    SvPOK(SV*)</li></ol></pre><p>You can get and set the current length of the string stored in an SV with
the following macros:</p>
<pre class="verbatim"><ol><li>    SvCUR(SV*)</li><li>    SvCUR_set(SV*, I32 val)</li></ol></pre><p>You can also get a pointer to the end of the string stored in the SV
with the macro:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvEND</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>But note that these last three macros are valid only if <code class="inline"><span class="i">SvPOK</span><span class="s">(</span><span class="s">)</span></code>
 is true.</p>
<p>If you want to append something to the end of string stored in an <code class="inline"><span class="w">SV</span>*</code>
,
you can use the following functions:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span>  <span class="i">sv_catpv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_catpvn</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_catpvf</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_vcatpvfn</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">STRLEN</span><span class="cm">,</span> <span class="w">va_list</span> *<span class="cm">,</span> <span class="w">SV</span> **<span class="cm">,</span></li><li>                                                             <span class="w">I32</span><span class="cm">,</span> <span class="w">bool</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">sv_catsv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">SV</span>*<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The first function calculates the length of the string to be appended by
using <code class="inline"><span class="w">strlen</span></code>
.  In the second, you specify the length of the string
yourself.  The third function processes its arguments like <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code> and
appends the formatted output.  The fourth function works like <code class="inline"><span class="w">vsprintf</span></code>
.
You can specify the address and length of an array of SVs instead of the
va_list argument.  The fifth function
extends the string stored in the first
SV with the string stored in the second SV.  It also forces the second SV
to be interpreted as a string.</p>
<p>The <code class="inline"><span class="w">sv_cat</span>*<span class="s">(</span><span class="s">)</span></code>
 functions are not generic enough to operate on values that
have "magic".  See <a href="#Magic-Virtual-Tables">Magic Virtual Tables</a> later in this document.</p>
<p>If you know the name of a scalar variable, you can get a pointer to its SV
by using the following:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>*  <span class="i">get_sv</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This returns NULL if the variable does not exist.</p>
<p>If you want to know if this variable (or any other SV) is actually <code class="inline"><a class="l_k" href="functions/defined.html">defined</a></code>,
you can call:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvOK</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>The scalar <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> value is stored in an SV instance called <code class="inline"><span class="w">PL_sv_undef</span></code>
.</p>
<p>Its address can be used whenever an <code class="inline"><span class="w">SV</span>*</code>
 is needed.  Make sure that
you don't try to compare a random sv with <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
.  For example
when interfacing Perl code, it'll work correctly for:</p>
<pre class="verbatim"><ol><li>  <span class="i">foo</span><span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>But won't work when called as:</p>
<pre class="verbatim"><ol><li>  <span class="i">$x</span> = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>  <span class="i">foo</span><span class="s">(</span><span class="i">$x</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>So to repeat always use SvOK() to check whether an sv is defined.</p>
<p>Also you have to be careful when using <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 as a value in
AVs or HVs (see <a href="#AVs%2c-HVs-and-undefined-values">AVs, HVs and undefined values</a>).</p>
<p>There are also the two values <code class="inline"><span class="w">PL_sv_yes</span></code>
 and <code class="inline"><span class="w">PL_sv_no</span></code>
, which contain
boolean TRUE and FALSE values, respectively.  Like <code class="inline"><span class="w">PL_sv_undef</span></code>
, their
addresses can be used whenever an <code class="inline"><span class="w">SV</span>*</code>
 is needed.</p>
<p>Do not be fooled into thinking that <code class="inline">(SV *) 0</code> is the same as <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
.
Take this code:</p>
<pre class="verbatim"><ol><li>    SV* sv = (SV*) 0;</li><li>    if (I-am-to-return-a-real-value) {</li><li>            sv = sv_2mortal(newSViv(42));</li><li>    }</li><li>    sv_setsv(ST(0), sv);</li></ol></pre><p>This code tries to return a new SV (which contains the value 42) if it should
return a real value, or undef otherwise.  Instead it has returned a NULL
pointer which, somewhere down the line, will cause a segmentation violation,
bus error, or just weird results.  Change the zero to <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 in the
first line and all will be well.</p>
<p>To free an SV that you've created, call <code class="inline"><span class="i">SvREFCNT_dec</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></code>
.  Normally this
call is not necessary (see <a href="#Reference-Counts-and-Mortality">Reference Counts and Mortality</a>).</p>
<a name="Offsets"></a><h2>Offsets</h2>
<p>Perl provides the function <code class="inline"><span class="w">sv_chop</span></code>
 to efficiently remove characters
from the beginning of a string; you give it an SV and a pointer to
somewhere inside the PV, and it discards everything before the
pointer.  The efficiency comes by means of a little hack: instead of
actually removing the characters, <code class="inline"><span class="w">sv_chop</span></code>
 sets the flag <code class="inline"><span class="w">OOK</span></code>

(offset OK) to signal to other functions that the offset hack is in
effect, and it moves the PV pointer (called <code class="inline"><span class="w">SvPVX</span></code>
) forward
by the number of bytes chopped off, and adjusts <code class="inline"><span class="w">SvCUR</span></code>
 and <code class="inline"><span class="w">SvLEN</span></code>

accordingly.  (A portion of the space between the old and new PV
pointers is used to store the count of chopped bytes.)</p>
<p>Hence, at this point, the start of the buffer that we allocated lives
at <code class="inline"><span class="i">SvPVX</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span> - <span class="i">SvIV</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></code>
 in memory and the PV pointer is pointing
into the middle of this allocated storage.</p>
<p>This is best demonstrated by example.  Normally copy-on-write will prevent
the substitution from operator from using this hack, but if you can craft a
string for which copy-on-write is not possible, you can see it in play.  In
the current implementation, the final byte of a string buffer is used as a
copy-on-write reference count.  If the buffer is not big enough, then
copy-on-write is skipped.  First have a look at an empty string:</p>
<pre class="verbatim"><ol><li>  <span class="i">% .</span>/<span class="w">perl</span> -<span class="w">Ilib</span> -<span class="w">MDevel::Peek</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;$a=&quot;&quot;; $a .= &quot;&quot;; Dump $a&#39;</span></li><li>  <span class="w">SV</span> = <span class="i">PV</span><span class="s">(</span><span class="n">0x7ffb7c008a70</span><span class="s">)</span> <span class="w">at</span> <span class="n">0x7ffb7c030390</span></li><li>    <span class="w">REFCNT</span> = <span class="n">1</span></li><li>    <span class="w">FLAGS</span> = <span class="s">(</span><span class="w">POK</span><span class="cm">,</span><span class="w">pPOK</span><span class="s">)</span></li><li>    <span class="w">PV</span> = <span class="n">0x7ffb7bc05b50</span> <span class="q">&quot;&quot;</span>\<span class="n">0</span></li><li>    <span class="w">CUR</span> = <span class="n">0</span></li><li>    <span class="w">LEN</span> = <span class="n">10</span></li></ol></pre><p>Notice here the LEN is 10.  (It may differ on your platform.)  Extend the
length of the string to one less than 10, and do a substitution:</p>
<pre class="verbatim"><ol><li>  <span class="i">% .</span>/<span class="w">perl</span> -<span class="w">Ilib</span> -<span class="w">MDevel::Peek</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;$a=&quot;&quot;; $a.=&quot;123456789&quot;; $a=~s/.//; Dump($a)&#39;</span></li><li>  <span class="w">SV</span> = <span class="i">PV</span><span class="s">(</span><span class="n">0x7ffa04008a70</span><span class="s">)</span> <span class="w">at</span> <span class="n">0x7ffa04030390</span></li><li>    <span class="w">REFCNT</span> = <span class="n">1</span></li><li>    <span class="w">FLAGS</span> = <span class="s">(</span><span class="w">POK</span><span class="cm">,</span><span class="w">OOK</span><span class="cm">,</span><span class="w">pPOK</span><span class="s">)</span></li><li>    <span class="w">OFFSET</span> = <span class="n">1</span></li><li>    <span class="w">PV</span> = <span class="n">0x7ffa03c05b61</span> <span class="s">(</span> <span class="q">&quot;\1&quot;</span> . <span class="s">)</span> <span class="q">&quot;23456789&quot;</span>\<span class="n">0</span></li><li>    <span class="w">CUR</span> = <span class="n">8</span></li><li>    <span class="w">LEN</span> = <span class="n">9</span></li></ol></pre><p>Here the number of bytes chopped off (1) is shown next as the OFFSET.  The
portion of the string between the "real" and the "fake" beginnings is
shown in parentheses, and the values of <code class="inline"><span class="w">SvCUR</span></code>
 and <code class="inline"><span class="w">SvLEN</span></code>
 reflect
the fake beginning, not the real one.  (The first character of the string
buffer happens to have changed to "\1" here, not "1", because the current
implementation stores the offset count in the string buffer.  This is
subject to change.)</p>
<p>Something similar to the offset hack is performed on AVs to enable
efficient shifting and splicing off the beginning of the array; while
<code class="inline"><span class="w">AvARRAY</span></code>
 points to the first element in the array that is visible from
Perl, <code class="inline"><span class="w">AvALLOC</span></code>
 points to the real start of the C array.  These are
usually the same, but a <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code> operation can be carried out by
increasing <code class="inline"><span class="w">AvARRAY</span></code>
 by one and decreasing <code class="inline"><span class="w">AvFILL</span></code>
 and <code class="inline"><span class="w">AvMAX</span></code>
.
Again, the location of the real start of the C array only comes into
play when freeing the array.  See <code class="inline"><span class="w">av_shift</span></code>
 in <i>av.c</i>.</p>
<a name="What's-Really-Stored-in-an-SV%3f"></a><h2>What's Really Stored in an SV?</h2>
<p>Recall that the usual method of determining the type of scalar you have is
to use <code class="inline"><span class="w">Sv</span>*<span class="w">OK</span></code>
 macros.  Because a scalar can be both a number and a string,
usually these macros will always return TRUE and calling the <code class="inline"><span class="w">Sv</span>*<span class="w">V</span></code>

macros will do the appropriate conversion of string to integer/double or
integer/double to string.</p>
<p>If you <i>really</i> need to know if you have an integer, double, or string
pointer in an SV, you can use the following three macros instead:</p>
<pre class="verbatim"><ol><li>    SvIOKp(SV*)</li><li>    SvNOKp(SV*)</li><li>    SvPOKp(SV*)</li></ol></pre><p>These will tell you if you truly have an integer, double, or string pointer
stored in your SV.  The "p" stands for private.</p>
<p>There are various ways in which the private and public flags may differ.
For example, in perl 5.16 and earlier a tied SV may have a valid
underlying value in the IV slot (so SvIOKp is true), but the data
should be accessed via the FETCH routine rather than directly,
so SvIOK is false.  (In perl 5.18 onwards, tied scalars use
the flags the same way as untied scalars.)  Another is when
numeric conversion has occurred and precision has been lost: only the
private flag is set on 'lossy' values.  So when an NV is converted to an
IV with loss, SvIOKp, SvNOKp and SvNOK will be set, while SvIOK wont be.</p>
<p>In general, though, it's best to use the <code class="inline"><span class="w">Sv</span>*<span class="w">V</span></code>
 macros.</p>
<a name="Working-with-AVs"></a><h2>Working with AVs</h2>
<p>There are two ways to create and load an AV.  The first method creates an
empty AV:</p>
<pre class="verbatim"><ol><li>    <span class="w">AV</span>*  <span class="i">newAV</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The second method both creates the AV and initially populates it with SVs:</p>
<pre class="verbatim"><ol><li>    <span class="w">AV</span>*  <span class="i">av_make</span><span class="s">(</span><span class="w">SSize_t</span> <span class="w">num</span><span class="cm">,</span> <span class="w">SV</span> **<span class="w">ptr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The second argument points to an array containing <code class="inline"><span class="w">num</span></code>
 <code class="inline"><span class="w">SV</span>*</code>
's.  Once the
AV has been created, the SVs can be destroyed, if so desired.</p>
<p>Once the AV has been created, the following operations are possible on it:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span>  <span class="i">av_push</span><span class="s">(</span><span class="w">AV</span>*<span class="cm">,</span> <span class="w">SV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*   <span class="i">av_pop</span><span class="s">(</span><span class="w">AV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*   <span class="i">av_shift</span><span class="s">(</span><span class="w">AV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">av_unshift</span><span class="s">(</span><span class="w">AV</span>*<span class="cm">,</span> <span class="w">SSize_t</span> <span class="w">num</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>These should be familiar operations, with the exception of <code class="inline"><span class="w">av_unshift</span></code>
.
This routine adds <code class="inline"><span class="w">num</span></code>
 elements at the front of the array with the <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>
value.  You must then use <code class="inline"><span class="w">av_store</span></code>
 (described below) to assign values
to these new elements.</p>
<p>Here are some other functions:</p>
<pre class="verbatim"><ol><li>    <span class="w">SSize_t</span> <span class="i">av_top_index</span><span class="s">(</span><span class="w">AV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>**    <span class="i">av_fetch</span><span class="s">(</span><span class="w">AV</span>*<span class="cm">,</span> <span class="w">SSize_t</span> <span class="w">key</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">lval</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>**    <span class="i">av_store</span><span class="s">(</span><span class="w">AV</span>*<span class="cm">,</span> <span class="w">SSize_t</span> <span class="w">key</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">val</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">av_top_index</span></code>
 function returns the highest index value in an array (just
like $#array in Perl).  If the array is empty, -1 is returned.  The
<code class="inline"><span class="w">av_fetch</span></code>
 function returns the value at index <code class="inline"><span class="w">key</span></code>
, but if <code class="inline"><span class="w">lval</span></code>

is non-zero, then <code class="inline"><span class="w">av_fetch</span></code>
 will store an undef value at that index.
The <code class="inline"><span class="w">av_store</span></code>
 function stores the value <code class="inline"><span class="w">val</span></code>
 at index <code class="inline"><span class="w">key</span></code>
, and does
not increment the reference count of <code class="inline"><span class="w">val</span></code>
.  Thus the caller is responsible
for taking care of that, and if <code class="inline"><span class="w">av_store</span></code>
 returns NULL, the caller will
have to decrement the reference count to avoid a memory leak.  Note that
<code class="inline"><span class="w">av_fetch</span></code>
 and <code class="inline"><span class="w">av_store</span></code>
 both return <code class="inline"><span class="w">SV</span>**</code>
's, not <code class="inline"><span class="w">SV</span>*</code>
's as their
return value.</p>
<p>A few more:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span>  <span class="i">av_clear</span><span class="s">(</span><span class="w">AV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">av_undef</span><span class="s">(</span><span class="w">AV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>  <span class="i">av_extend</span><span class="s">(</span><span class="w">AV</span>*<span class="cm">,</span> <span class="w">SSize_t</span> <span class="w">key</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">av_clear</span></code>
 function deletes all the elements in the AV* array, but
does not actually delete the array itself.  The <code class="inline"><span class="w">av_undef</span></code>
 function will
delete all the elements in the array plus the array itself.  The
<code class="inline"><span class="w">av_extend</span></code>
 function extends the array so that it contains at least <code class="inline"><span class="w">key</span>+<span class="n">1</span></code>

elements.  If <code class="inline"><span class="w">key</span>+<span class="n">1</span></code>
 is less than the currently allocated length of the array,
then nothing is done.</p>
<p>If you know the name of an array variable, you can get a pointer to its AV
by using the following:</p>
<pre class="verbatim"><ol><li>    <span class="w">AV</span>*  <span class="i">get_av</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This returns NULL if the variable does not exist.</p>
<p>See <a href="#Understanding-the-Magic-of-Tied-Hashes-and-Arrays">Understanding the Magic of Tied Hashes and Arrays</a> for more
information on how to use the array access functions on tied arrays.</p>
<a name="Working-with-HVs"></a><h2>Working with HVs</h2>
<p>To create an HV, you use the following routine:</p>
<pre class="verbatim"><ol><li>    <span class="w">HV</span>*  <span class="i">newHV</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Once the HV has been created, the following operations are possible on it:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>**  <span class="i">hv_store</span><span class="s">(</span><span class="w">HV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">klen</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">val</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">hash</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>**  <span class="i">hv_fetch</span><span class="s">(</span><span class="w">HV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">klen</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">lval</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">klen</span></code>
 parameter is the length of the key being passed in (Note that
you cannot pass 0 in as a value of <code class="inline"><span class="w">klen</span></code>
 to tell Perl to measure the
length of the key).  The <code class="inline"><span class="w">val</span></code>
 argument contains the SV pointer to the
scalar being stored, and <code class="inline"><span class="w">hash</span></code>
 is the precomputed hash value (zero if
you want <code class="inline"><span class="w">hv_store</span></code>
 to calculate it for you).  The <code class="inline"><span class="w">lval</span></code>
 parameter
indicates whether this fetch is actually a part of a store operation, in
which case a new undefined value will be added to the HV with the supplied
key and <code class="inline"><span class="w">hv_fetch</span></code>
 will return as if the value had already existed.</p>
<p>Remember that <code class="inline"><span class="w">hv_store</span></code>
 and <code class="inline"><span class="w">hv_fetch</span></code>
 return <code class="inline"><span class="w">SV</span>**</code>
's and not just
<code class="inline"><span class="w">SV</span>*</code>
.  To access the scalar value, you must first dereference the return
value.  However, you should check to make sure that the return value is
not NULL before dereferencing it.</p>
<p>The first of these two functions checks if a hash table entry exists, and the 
second deletes it.</p>
<pre class="verbatim"><ol><li>    <span class="w">bool</span>  <span class="i">hv_exists</span><span class="s">(</span><span class="w">HV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">klen</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*   <span class="i">hv_delete</span><span class="s">(</span><span class="w">HV</span>*<span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">klen</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">flags</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If <code class="inline"><span class="w">flags</span></code>
 does not include the <code class="inline"><span class="w">G_DISCARD</span></code>
 flag then <code class="inline"><span class="w">hv_delete</span></code>
 will
create and return a mortal copy of the deleted value.</p>
<p>And more miscellaneous functions:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span>   <span class="i">hv_clear</span><span class="s">(</span><span class="w">HV</span>*<span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span>   <span class="i">hv_undef</span><span class="s">(</span><span class="w">HV</span>*<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Like their AV counterparts, <code class="inline"><span class="w">hv_clear</span></code>
 deletes all the entries in the hash
table but does not actually delete the hash table.  The <code class="inline"><span class="w">hv_undef</span></code>
 deletes
both the entries and the hash table itself.</p>
<p>Perl keeps the actual data in a linked list of structures with a typedef of HE.
These contain the actual key and value pointers (plus extra administrative
overhead).  The key is a string pointer; the value is an <code class="inline"><span class="w">SV</span>*</code>
.  However,
once you have an <code class="inline"><span class="w">HE</span>*</code>
, to get the actual key and value, use the routines
specified below.</p>
<pre class="verbatim"><ol><li>    I32    hv_iterinit(HV*);</li><li>            /* Prepares starting point to traverse hash table */</li><li>    HE*    hv_iternext(HV*);</li><li>            /* Get the next entry, and return a pointer to a</li><li>               structure that has both the key and value */</li><li>    char*  hv_iterkey(HE* entry, I32* retlen);</li><li>            /* Get the key from an HE structure and also return</li><li>               the length of the key string */</li><li>    SV*    hv_iterval(HV*, HE* entry);</li><li>            /* Return an SV pointer to the value of the HE</li><li>               structure */</li><li>    SV*    hv_iternextsv(HV*, char** key, I32* retlen);</li><li>            /* This convenience routine combines hv_iternext,</li><li>	       hv_iterkey, and hv_iterval.  The key and retlen</li><li>	       arguments are return values for the key and its</li><li>	       length.  The value is returned in the SV* argument */</li></ol></pre><p>If you know the name of a hash variable, you can get a pointer to its HV
by using the following:</p>
<pre class="verbatim"><ol><li>    <span class="w">HV</span>*  <span class="i">get_hv</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This returns NULL if the variable does not exist.</p>
<p>The hash algorithm is defined in the <code class="inline"><span class="w">PERL_HASH</span></code>
 macro:</p>
<pre class="verbatim"><ol><li>    <span class="i">PERL_HASH</span><span class="s">(</span><span class="w">hash</span><span class="cm">,</span> <span class="w">key</span><span class="cm">,</span> <span class="w">klen</span><span class="s">)</span></li></ol></pre><p>The exact implementation of this macro varies by architecture and version
of perl, and the return value may change per invocation, so the value
is only valid for the duration of a single perl process.</p>
<p>See <a href="#Understanding-the-Magic-of-Tied-Hashes-and-Arrays">Understanding the Magic of Tied Hashes and Arrays</a> for more
information on how to use the hash access functions on tied hashes.</p>
<a name="Hash-API-Extensions"></a><h2>Hash API Extensions</h2>
<p>Beginning with version 5.004, the following functions are also supported:</p>
<pre class="verbatim"><ol><li>    <span class="w">HE</span>*     <span class="w">hv_fetch_ent</span>  <span class="s">(</span><span class="w">HV</span>* <span class="w">tb</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">lval</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">hash</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">HE</span>*     <span class="w">hv_store_ent</span>  <span class="s">(</span><span class="w">HV</span>* <span class="w">tb</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">val</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">hash</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="w">bool</span>    <span class="w">hv_exists_ent</span> <span class="s">(</span><span class="w">HV</span>* <span class="w">tb</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">hash</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>*     <span class="w">hv_delete_ent</span> <span class="s">(</span><span class="w">HV</span>* <span class="w">tb</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">flags</span><span class="cm">,</span> <span class="w">U32</span> <span class="w">hash</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="w">SV</span>*     <span class="w">hv_iterkeysv</span>  <span class="s">(</span><span class="w">HE</span>* <span class="w">entry</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that these functions take <code class="inline"><span class="w">SV</span>*</code>
 keys, which simplifies writing
of extension code that deals with hash structures.  These functions
also allow passing of <code class="inline"><span class="w">SV</span>*</code>
 keys to <code class="inline"><a class="l_k" href="functions/tie.html">tie</a></code> functions without forcing
you to stringify the keys (unlike the previous set of functions).</p>
<p>They also return and accept whole hash entries (<code class="inline"><span class="w">HE</span>*</code>
), making their
use more efficient (since the hash number for a particular string
doesn't have to be recomputed every time).  See <a href="perlapi.html">perlapi</a> for detailed
descriptions.</p>
<p>The following macros must always be used to access the contents of hash
entries.  Note that the arguments to these macros must be simple
variables, since they may get evaluated more than once.  See
<a href="perlapi.html">perlapi</a> for detailed descriptions of these macros.</p>
<pre class="verbatim"><ol><li>    HePV(HE* he, STRLEN len)</li><li>    HeVAL(HE* he)</li><li>    HeHASH(HE* he)</li><li>    HeSVKEY(HE* he)</li><li>    HeSVKEY_force(HE* he)</li><li>    HeSVKEY_set(HE* he, SV* sv)</li></ol></pre><p>These two lower level macros are defined, but must only be used when
dealing with keys that are not <code class="inline"><span class="w">SV</span>*</code>
s:</p>
<pre class="verbatim"><ol><li>    HeKEY(HE* he)</li><li>    HeKLEN(HE* he)</li></ol></pre><p>Note that both <code class="inline"><span class="w">hv_store</span></code>
 and <code class="inline"><span class="w">hv_store_ent</span></code>
 do not increment the
reference count of the stored <code class="inline"><span class="w">val</span></code>
, which is the caller's responsibility.
If these functions return a NULL value, the caller will usually have to
decrement the reference count of <code class="inline"><span class="w">val</span></code>
 to avoid a memory leak.</p>
<a name="AVs%2c-HVs-and-undefined-values"></a><h2>AVs, HVs and undefined values</h2>
<p>Sometimes you have to store undefined values in AVs or HVs.  Although
this may be a rare case, it can be tricky.  That's because you're
used to using <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 if you need an undefined SV.</p>
<p>For example, intuition tells you that this XS code:</p>
<pre class="verbatim"><ol><li>    <span class="w">AV</span> *<span class="w">av</span> = <span class="i">newAV</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">av_store</span><span class="s">(</span> <span class="w">av</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="i">&amp;PL_sv_undef</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>is equivalent to this Perl code:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@av</span><span class="sc">;</span></li><li>    <span class="i">$av</span>[<span class="n">0</span>] = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li></ol></pre><p>Unfortunately, this isn't true.  In perl 5.18 and earlier, AVs use <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 as a marker
for indicating that an array element has not yet been initialized.
Thus, <code class="inline"><a class="l_k" href="functions/exists.html">exists</a> <span class="i">$av</span>[<span class="n">0</span>]</code>
 would be true for the above Perl code, but
false for the array generated by the XS code.  In perl 5.20, storing
&amp;PL_sv_undef will create a read-only element, because the scalar
&amp;PL_sv_undef itself is stored, not a copy.</p>
<p>Similar problems can occur when storing <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 in HVs:</p>
<pre class="verbatim"><ol><li>    <span class="i">hv_store</span><span class="s">(</span> <span class="w">hv</span><span class="cm">,</span> <span class="q">&quot;key&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="i">&amp;PL_sv_undef</span><span class="cm">,</span> <span class="n">0</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This will indeed make the value <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, but if you try to modify
the value of <code class="inline"><span class="w">key</span></code>
, you'll get the following error:</p>
<pre class="verbatim"><ol><li>    <span class="w">Modification</span> <span class="w">of</span> <span class="w">non</span>-<span class="w">creatable</span> <span class="w">hash</span> <span class="w">value</span> <span class="w">attempted</span></li></ol></pre><p>In perl 5.8.0, <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
 was also used to mark placeholders
in restricted hashes.  This caused such hash entries not to appear
when iterating over the hash or when checking for the keys
with the <code class="inline"><span class="w">hv_exists</span></code>
 function.</p>
<p>You can run into similar problems when you store <code class="inline"><span class="i">&amp;PL_sv_yes</span></code>
 or
<code class="inline"><span class="i">&amp;PL_sv_no</span></code>
 into AVs or HVs.  Trying to modify such elements
will give you the following error:</p>
<pre class="verbatim"><ol><li>    <span class="w">Modification</span> <span class="w">of</span> <span class="w">a</span> <a class="l_k" href="functions/read.html">read</a>-<span class="w">only</span> <span class="w">value</span> <span class="w">attempted</span></li></ol></pre><p>To make a long story short, you can use the special variables
<code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
, <code class="inline"><span class="i">&amp;PL_sv_yes</span></code>
 and <code class="inline"><span class="i">&amp;PL_sv_no</span></code>
 with AVs and
HVs, but you have to make sure you know what you're doing.</p>
<p>Generally, if you want to store an undefined value in an AV
or HV, you should not use <code class="inline"><span class="i">&amp;PL_sv_undef</span></code>
, but rather create a
new undefined value using the <code class="inline"><span class="w">newSV</span></code>
 function, for example:</p>
<pre class="verbatim"><ol><li>    <span class="i">av_store</span><span class="s">(</span> <span class="w">av</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="i">newSV</span><span class="s">(</span><span class="n">0</span><span class="s">)</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">hv_store</span><span class="s">(</span> <span class="w">hv</span><span class="cm">,</span> <span class="q">&quot;foo&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="i">newSV</span><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="cm">,</span> <span class="n">0</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="References"></a><h2>References</h2>
<p>References are a special type of scalar that point to other data types
(including other references).</p>
<p>To create a reference, use either of the following functions:</p>
<pre class="verbatim"><ol><li>    SV* newRV_inc((SV*) thing);</li><li>    SV* newRV_noinc((SV*) thing);</li></ol></pre><p>The <code class="inline"><span class="w">thing</span></code>
 argument can be any of an <code class="inline"><span class="w">SV</span>*</code>
, <code class="inline"><span class="w">AV</span>*</code>
, or <code class="inline"><span class="w">HV</span>*</code>
.  The
functions are identical except that <code class="inline"><span class="w">newRV_inc</span></code>
 increments the reference
count of the <code class="inline"><span class="w">thing</span></code>
, while <code class="inline"><span class="w">newRV_noinc</span></code>
 does not.  For historical
reasons, <code class="inline"><span class="w">newRV</span></code>
 is a synonym for <code class="inline"><span class="w">newRV_inc</span></code>
.</p>
<p>Once you have a reference, you can use the following macro to dereference
the reference:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvRV</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>then call the appropriate routines, casting the returned <code class="inline"><span class="w">SV</span>*</code>
 to either an
<code class="inline"><span class="w">AV</span>*</code>
 or <code class="inline"><span class="w">HV</span>*</code>
, if required.</p>
<p>To determine if an SV is a reference, you can use the following macro:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvROK</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>To discover what type of value the reference refers to, use the following
macro and then check the return value.</p>
<pre class="verbatim"><ol><li>    <span class="i">SvTYPE</span><span class="s">(</span><span class="i">SvRV</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span><span class="s">)</span></li></ol></pre><p>The most useful types that will be returned are:</p>
<pre class="verbatim"><ol><li>    &lt; <span class="w">SVt_PVAV</span>  <span class="w">Scalar</span></li><li>    <span class="w">SVt_PVAV</span>    <span class="w">Array</span></li><li>    <span class="w">SVt_PVHV</span>    <span class="w">Hash</span></li><li>    <span class="w">SVt_PVCV</span>    <span class="w">Code</span></li><li>    <span class="w">SVt_PVGV</span>    <span class="w">Glob</span> <span class="s">(</span><span class="w">possibly</span> <span class="w">a</span> <span class="w">file</span> <span class="w">handle</span><span class="s">)</span></li></ol></pre><p>See <a href="perlapi.html#svtype">svtype in perlapi</a> for more details.</p>
<a name="Blessed-References-and-Class-Objects"></a><h2>Blessed References and Class Objects</h2>
<p>References are also used to support object-oriented programming.  In perl's
OO lexicon, an object is simply a reference that has been blessed into a
package (or class).  Once blessed, the programmer may now use the reference
to access the various methods in the class.</p>
<p>A reference can be blessed into a package with the following function:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>* <span class="i">sv_bless</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">HV</span>* <span class="w">stash</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">sv</span></code>
 argument must be a reference value.  The <code class="inline"><span class="w">stash</span></code>
 argument
specifies which class the reference will belong to.  See
<a href="#Stashes-and-Globs">Stashes and Globs</a> for information on converting class names into stashes.</p>
<p>/* Still under construction */</p>
<p>The following function upgrades rv to reference if not already one.
Creates a new SV for rv to point to.  If <code class="inline"><span class="w">classname</span></code>
 is non-null, the SV
is blessed into the specified class.  SV is returned.</p>
<pre class="verbatim"><ol><li>	<span class="w">SV</span>* <span class="i">newSVrv</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following three functions copy integer, unsigned integer or double
into an SV whose reference is <code class="inline"><span class="w">rv</span></code>
.  SV is blessed if <code class="inline"><span class="w">classname</span></code>
 is
non-null.</p>
<pre class="verbatim"><ol><li>	<span class="w">SV</span>* <span class="i">sv_setref_iv</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="cm">,</span> <span class="w">IV</span> <span class="w">iv</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="w">SV</span>* <span class="i">sv_setref_uv</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="cm">,</span> <span class="w">UV</span> <span class="w">uv</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="w">SV</span>* <span class="i">sv_setref_nv</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="cm">,</span> <span class="w">NV</span> <span class="w">iv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following function copies the pointer value (<i>the address, not the
string!</i>) into an SV whose reference is rv.  SV is blessed if <code class="inline"><span class="w">classname</span></code>

is non-null.</p>
<pre class="verbatim"><ol><li>	<span class="w">SV</span>* <span class="i">sv_setref_pv</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="cm">,</span> <span class="w">void</span>* <span class="w">pv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following function copies a string into an SV whose reference is <code class="inline"><span class="w">rv</span></code>
.
Set length to 0 to let Perl calculate the string length.  SV is blessed if
<code class="inline"><span class="w">classname</span></code>
 is non-null.</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>* <span class="i">sv_setref_pvn</span><span class="s">(</span><span class="w">SV</span>* <span class="w">rv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">classname</span><span class="cm">,</span> <span class="w">char</span>* <span class="w">pv</span><span class="cm">,</span></li><li>                                                         <span class="w">STRLEN</span> <a class="l_k" href="functions/length.html">length</a><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following function tests whether the SV is blessed into the specified
class.  It does not check inheritance relationships.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/int.html">int</a>  <span class="i">sv_isa</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">name</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following function tests whether the SV is a reference to a blessed object.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/int.html">int</a>  <span class="i">sv_isobject</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The following function tests whether the SV is derived from the specified
class.  SV can be either a reference to a blessed object or a string
containing a class name.  This is the function implementing the
<code class="inline"><span class="w">UNIVERSAL::isa</span></code>
 functionality.</p>
<pre class="verbatim"><ol><li>	<span class="w">bool</span> <span class="i">sv_derived_from</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">name</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To check if you've got an object derived from a specific class you have
to write:</p>
<pre class="verbatim"><ol><li>	if <span class="s">(</span><span class="i">sv_isobject</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span> &amp;&amp; <span class="i">sv_derived_from</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">class</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><a name="Creating-New-Variables"></a><h2>Creating New Variables</h2>
<p>To create a new Perl variable with an undef value which can be accessed from
your Perl script, use the following routines, depending on the variable type.</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>*  <span class="i">get_sv</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="w">GV_ADD</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">AV</span>*  <span class="i">get_av</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="w">GV_ADD</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">HV</span>*  <span class="i">get_hv</span><span class="s">(</span><span class="q">&quot;package::varname&quot;</span><span class="cm">,</span> <span class="w">GV_ADD</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Notice the use of GV_ADD as the second parameter.  The new variable can now
be set, using the routines appropriate to the data type.</p>
<p>There are additional macros whose values may be bitwise OR'ed with the
<code class="inline"><span class="w">GV_ADD</span></code>
 argument to enable certain extra features.  Those bits are:</p>
<ul>
<li><a name="GV_ADDMULTI"></a><b>GV_ADDMULTI</b>
<p>Marks the variable as multiply defined, thus preventing the:</p>
<pre class="verbatim"><ol><li>  Name &lt;varname&gt; used only once: possible typo</li></ol></pre><p>warning.</p>
</li>
<li><a name="GV_ADDWARN"></a><b>GV_ADDWARN</b>
<p>Issues the warning:</p>
<pre class="verbatim"><ol><li>  Had to create &lt;varname&gt; unexpectedly</li></ol></pre><p>if the variable did not exist before the function was called.</p>
</li>
</ul>
<p>If you do not specify a package name, the variable is created in the current
package.</p>
<a name="Reference-Counts-and-Mortality"></a><h2>Reference Counts and Mortality</h2>
<p>Perl uses a reference count-driven garbage collection mechanism.  SVs,
AVs, or HVs (xV for short in the following) start their life with a
reference count of 1.  If the reference count of an xV ever drops to 0,
then it will be destroyed and its memory made available for reuse.</p>
<p>This normally doesn't happen at the Perl level unless a variable is
undef'ed or the last variable holding a reference to it is changed or
overwritten.  At the internal level, however, reference counts can be
manipulated with the following macros:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/int.html">int</a> <span class="i">SvREFCNT</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">SV</span>* <span class="i">SvREFCNT_inc</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">void</span> <span class="i">SvREFCNT_dec</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>However, there is one other function which manipulates the reference
count of its argument.  The <code class="inline"><span class="w">newRV_inc</span></code>
 function, you will recall,
creates a reference to the specified argument.  As a side effect,
it increments the argument's reference count.  If this is not what
you want, use <code class="inline"><span class="w">newRV_noinc</span></code>
 instead.</p>
<p>For example, imagine you want to return a reference from an XSUB function.
Inside the XSUB routine, you create an SV which initially has a reference
count of one.  Then you call <code class="inline"><span class="w">newRV_inc</span></code>
, passing it the just-created SV.
This returns the reference as a new SV, but the reference count of the
SV you passed to <code class="inline"><span class="w">newRV_inc</span></code>
 has been incremented to two.  Now you
return the reference from the XSUB routine and forget about the SV.
But Perl hasn't!  Whenever the returned reference is destroyed, the
reference count of the original SV is decreased to one and nothing happens.
The SV will hang around without any way to access it until Perl itself
terminates.  This is a memory leak.</p>
<p>The correct procedure, then, is to use <code class="inline"><span class="w">newRV_noinc</span></code>
 instead of
<code class="inline"><span class="w">newRV_inc</span></code>
.  Then, if and when the last reference is destroyed,
the reference count of the SV will go to zero and it will be destroyed,
stopping any memory leak.</p>
<p>There are some convenience functions available that can help with the
destruction of xVs.  These functions introduce the concept of "mortality".
An xV that is mortal has had its reference count marked to be decremented,
but not actually decremented, until "a short time later".  Generally the
term "short time later" means a single Perl statement, such as a call to
an XSUB function.  The actual determinant for when mortal xVs have their
reference count decremented depends on two macros, SAVETMPS and FREETMPS.
See <a href="perlcall.html">perlcall</a> and <a href="perlxs.html">perlxs</a> for more details on these macros.</p>
<p>"Mortalization" then is at its simplest a deferred <code class="inline"><span class="w">SvREFCNT_dec</span></code>
.
However, if you mortalize a variable twice, the reference count will
later be decremented twice.</p>
<p>"Mortal" SVs are mainly used for SVs that are placed on perl's stack.
For example an SV which is created just to pass a number to a called sub
is made mortal to have it cleaned up automatically when it's popped off
the stack.  Similarly, results returned by XSUBs (which are pushed on the
stack) are often made mortal.</p>
<p>To create a mortal variable, use the functions:</p>
<pre class="verbatim"><ol><li>    SV*  sv_newmortal()</li><li>    SV*  sv_2mortal(SV*)</li><li>    SV*  sv_mortalcopy(SV*)</li></ol></pre><p>The first call creates a mortal SV (with no value), the second converts an existing
SV to a mortal SV (and thus defers a call to <code class="inline"><span class="w">SvREFCNT_dec</span></code>
), and the
third creates a mortal copy of an existing SV.
Because <code class="inline"><span class="w">sv_newmortal</span></code>
 gives the new SV no value, it must normally be given one
via <code class="inline"><span class="w">sv_setpv</span></code>
, <code class="inline"><span class="w">sv_setiv</span></code>
, etc. :</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span> *<span class="w">tmp</span> = <span class="i">sv_newmortal</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">sv_setiv</span><span class="s">(</span><span class="w">tmp</span><span class="cm">,</span> <span class="w">an_integer</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>As that is multiple C statements it is quite common so see this idiom instead:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span> *<span class="w">tmp</span> = <span class="i">sv_2mortal</span><span class="s">(</span><span class="i">newSViv</span><span class="s">(</span><span class="w">an_integer</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You should be careful about creating mortal variables.  Strange things
can happen if you make the same value mortal within multiple contexts,
or if you make a variable mortal multiple
times.  Thinking of "Mortalization"
as deferred <code class="inline"><span class="w">SvREFCNT_dec</span></code>
 should help to minimize such problems.
For example if you are passing an SV which you <i>know</i> has a high enough REFCNT
to survive its use on the stack you need not do any mortalization.
If you are not sure then doing an <code class="inline"><span class="w">SvREFCNT_inc</span></code>
 and <code class="inline"><span class="w">sv_2mortal</span></code>
, or
making a <code class="inline"><span class="w">sv_mortalcopy</span></code>
 is safer.</p>
<p>The mortal routines are not just for SVs; AVs and HVs can be
made mortal by passing their address (type-casted to <code class="inline"><span class="w">SV</span>*</code>
) to the
<code class="inline"><span class="w">sv_2mortal</span></code>
 or <code class="inline"><span class="w">sv_mortalcopy</span></code>
 routines.</p>
<a name="Stashes-and-Globs"></a><h2>Stashes and Globs</h2>
<p>A <b>stash</b> is a hash that contains all variables that are defined
within a package.  Each key of the stash is a symbol
name (shared by all the different types of objects that have the same
name), and each value in the hash table is a GV (Glob Value).  This GV
in turn contains references to the various objects of that name,
including (but not limited to) the following:</p>
<pre class="verbatim"><ol><li>    <span class="w">Scalar</span> <span class="w">Value</span></li><li>    <span class="w">Array</span> <span class="w">Value</span></li><li>    <span class="w">Hash</span> <span class="w">Value</span></li><li>    <span class="w">I</span>/<span class="w">O</span> <span class="w">Handle</span></li><li>    <span class="w">Format</span></li><li>    <span class="w">Subroutine</span></li></ol></pre><p>There is a single stash called <code class="inline"><span class="w">PL_defstash</span></code>
 that holds the items that exist
in the <code class="inline"><span class="w">main</span></code>
 package.  To get at the items in other packages, append the
string "::" to the package name.  The items in the <code class="inline"><span class="w">Foo</span></code>
 package are in
the stash <code class="inline"><span class="w">Foo::</span></code>
 in PL_defstash.  The items in the <code class="inline"><span class="w">Bar::Baz</span></code>
 package are
in the stash <code class="inline"><span class="w">Baz::</span></code>
 in <code class="inline"><span class="w">Bar::</span></code>
's stash.</p>
<p>To get the stash pointer for a particular package, use the function:</p>
<pre class="verbatim"><ol><li>    HV*  gv_stashpv(const char* name, I32 flags)</li><li>    HV*  gv_stashsv(SV*, I32 flags)</li></ol></pre><p>The first function takes a literal string, the second uses the string stored
in the SV.  Remember that a stash is just a hash table, so you get back an
<code class="inline"><span class="w">HV</span>*</code>
.  The <code class="inline"><span class="w">flags</span></code>
 flag will create a new package if it is set to GV_ADD.</p>
<p>The name that <code class="inline"><span class="w">gv_stash</span>*<span class="w">v</span></code>
 wants is the name of the package whose symbol table
you want.  The default package is called <code class="inline"><span class="w">main</span></code>
.  If you have multiply nested
packages, pass their names to <code class="inline"><span class="w">gv_stash</span>*<span class="w">v</span></code>
, separated by <code class="inline"><span class="w">::</span></code>
 as in the Perl
language itself.</p>
<p>Alternately, if you have an SV that is a blessed reference, you can find
out the stash pointer by using:</p>
<pre class="verbatim"><ol><li>    <span class="w">HV</span>*  <span class="i">SvSTASH</span><span class="s">(</span><span class="i">SvRV</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>then use the following to get the package name itself:</p>
<pre class="verbatim"><ol><li>    <span class="w">char</span>*  <span class="i">HvNAME</span><span class="s">(</span><span class="w">HV</span>* <span class="w">stash</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you need to bless or re-bless an object you can use the following
function:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span>*  <span class="i">sv_bless</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">HV</span>* <span class="w">stash</span><span class="s">)</span></li></ol></pre><p>where the first argument, an <code class="inline"><span class="w">SV</span>*</code>
, must be a reference, and the second
argument is a stash.  The returned <code class="inline"><span class="w">SV</span>*</code>
 can now be used in the same way
as any other SV.</p>
<p>For more information on references and blessings, consult <a href="perlref.html">perlref</a>.</p>
<a name="Double-Typed-SVs"></a><h2>Double-Typed SVs</h2>
<p>Scalar variables normally contain only one type of value, an integer,
double, pointer, or reference.  Perl will automatically convert the
actual scalar data from the stored type into the requested type.</p>
<p>Some scalar variables contain more than one type of scalar data.  For
example, the variable <code class="inline"><span class="i">$!</span></code>
 contains either the numeric value of <code class="inline"><span class="w">errno</span></code>

or its string equivalent from either <code class="inline"><span class="w">strerror</span></code>
 or <code class="inline"><span class="w">sys_errlist</span><span class="s">[</span><span class="s">]</span></code>
.</p>
<p>To force multiple data values into an SV, you must do two things: use the
<code class="inline"><span class="w">sv_set</span>*<span class="w">v</span></code>
 routines to add the additional scalar type, then set a flag
so that Perl will believe it contains more than one type of data.  The
four macros to set the flags are:</p>
<pre class="verbatim"><ol><li>	<span class="w">SvIOK_on</span></li><li>	<span class="w">SvNOK_on</span></li><li>	<span class="w">SvPOK_on</span></li><li>	<span class="w">SvROK_on</span></li></ol></pre><p>The particular macro you must use depends on which <code class="inline"><span class="w">sv_set</span>*<span class="w">v</span></code>
 routine
you called first.  This is because every <code class="inline"><span class="w">sv_set</span>*<span class="w">v</span></code>
 routine turns on
only the bit for the particular type of data being set, and turns off
all the rest.</p>
<p>For example, to create a new Perl variable called "dberror" that contains
both the numeric and descriptive string error values, you could use the
following code:</p>
<pre class="verbatim"><ol><li>    extern int  dberror;</li><li>    extern char *dberror_list;</li><li></li><li>    SV* sv = get_sv("dberror", GV_ADD);</li><li>    sv_setiv(sv, (IV) dberror);</li><li>    sv_setpv(sv, dberror_list[dberror]);</li><li>    SvIOK_on(sv);</li></ol></pre><p>If the order of <code class="inline"><span class="w">sv_setiv</span></code>
 and <code class="inline"><span class="w">sv_setpv</span></code>
 had been reversed, then the
macro <code class="inline"><span class="w">SvPOK_on</span></code>
 would need to be called instead of <code class="inline"><span class="w">SvIOK_on</span></code>
.</p>
<a name="Read-Only-Values"></a><h2>Read-Only Values</h2>
<p>In Perl 5.16 and earlier, copy-on-write (see the next section) shared a
flag bit with read-only scalars.  So the only way to test whether
<code class="inline"><span class="w">sv_setsv</span></code>
, etc., will raise a "Modification of a read-only value" error
in those versions is:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvREADONLY</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span> &amp;&amp; !<span class="i">SvIsCOW</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></li></ol></pre><p>Under Perl 5.18 and later, SvREADONLY only applies to read-only variables,
and, under 5.20, copy-on-write scalars can also be read-only, so the above
check is incorrect.  You just want:</p>
<pre class="verbatim"><ol><li>    <span class="i">SvREADONLY</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></li></ol></pre><p>If you need to do this check often, define your own macro like this:</p>
<pre class="verbatim"><ol><li>    <span class="c">#if PERL_VERSION &gt;= 18</span></li><li>    <span class="c"># define SvTRULYREADONLY(sv) SvREADONLY(sv)</span></li><li>    <span class="c">#else</span></li><li>    <span class="c"># define SvTRULYREADONLY(sv) (SvREADONLY(sv) &amp;&amp; !SvIsCOW(sv))</span></li><li>    <span class="c">#endif</span></li></ol></pre><a name="Copy-on-Write"></a><h2>Copy on Write</h2>
<p>Perl implements a copy-on-write (COW) mechanism for scalars, in which
string copies are not immediately made when requested, but are deferred
until made necessary by one or the other scalar changing.  This is mostly
transparent, but one must take care not to modify string buffers that are
shared by multiple SVs.</p>
<p>You can test whether an SV is using copy-on-write with <code class="inline"><span class="i">SvIsCOW</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></code>
.</p>
<p>You can force an SV to make its own copy of its string buffer by calling <code class="inline"><span class="i">sv_force_normal</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></code>
 or SvPV_force_nolen(sv).</p>
<p>If you want to make the SV drop its string buffer, use
<code class="inline"><span class="i">sv_force_normal_flags</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">SV_COW_DROP_PV</span><span class="s">)</span></code>
 or simply
<code class="inline"><span class="i">sv_setsv</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">NULL</span><span class="s">)</span></code>
.</p>
<p>All of these functions will croak on read-only scalars (see the previous
section for more on those).</p>
<p>To test that your code is behaving correctly and not modifying COW buffers,
on systems that support <i>mmap(2)</i> (i.e., Unix) you can configure perl with
<code class="inline">-<span class="w">Accflags</span>=-<span class="w">DPERL_DEBUG_READONLY_COW</span></code>
 and it will turn buffer violations
into crashes.  You will find it to be marvellously slow, so you may want to
skip perl's own tests.</p>
<a name="Magic-Variables"></a><h2>Magic Variables</h2>
<p>[This section still under construction.  Ignore everything here.  Post no
bills.  Everything not permitted is forbidden.]</p>
<p>Any SV may be magical, that is, it has special features that a normal
SV does not have.  These features are stored in the SV structure in a
linked list of <code class="inline"><span class="w">struct</span> <span class="w">magic</span></code>
's, typedef'ed to <code class="inline"><span class="w">MAGIC</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="w">struct</span> <span class="i">magic</span> <span class="s">{</span></li><li>        <span class="w">MAGIC</span>*      <span class="w">mg_moremagic</span><span class="sc">;</span></li><li>        <span class="w">MGVTBL</span>*     <span class="w">mg_virtual</span><span class="sc">;</span></li><li>        <span class="w">U16</span>         <span class="w">mg_private</span><span class="sc">;</span></li><li>        <span class="w">char</span>        <span class="w">mg_type</span><span class="sc">;</span></li><li>        <span class="w">U8</span>          <span class="w">mg_flags</span><span class="sc">;</span></li><li>        <span class="w">I32</span>         <span class="w">mg_len</span><span class="sc">;</span></li><li>        <span class="w">SV</span>*         <span class="w">mg_obj</span><span class="sc">;</span></li><li>        <span class="w">char</span>*       <span class="w">mg_ptr</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>Note this is current as of patchlevel 0, and could change at any time.</p>
<a name="Assigning-Magic"></a><h2>Assigning Magic</h2>
<p>Perl adds magic to an SV using the sv_magic function:</p>
<pre class="verbatim"><ol><li>  <span class="w">void</span> <span class="i">sv_magic</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">obj</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">how</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">name</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">namlen</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">sv</span></code>
 argument is a pointer to the SV that is to acquire a new magical
feature.</p>
<p>If <code class="inline"><span class="w">sv</span></code>
 is not already magical, Perl uses the <code class="inline"><span class="w">SvUPGRADE</span></code>
 macro to
convert <code class="inline"><span class="w">sv</span></code>
 to type <code class="inline"><span class="w">SVt_PVMG</span></code>
.
Perl then continues by adding new magic
to the beginning of the linked list of magical features.  Any prior entry
of the same type of magic is deleted.  Note that this can be overridden,
and multiple instances of the same type of magic can be associated with an
SV.</p>
<p>The <code class="inline"><span class="w">name</span></code>
 and <code class="inline"><span class="w">namlen</span></code>
 arguments are used to associate a string with
the magic, typically the name of a variable.  <code class="inline"><span class="w">namlen</span></code>
 is stored in the
<code class="inline"><span class="w">mg_len</span></code>
 field and if <code class="inline"><span class="w">name</span></code>
 is non-null then either a <code class="inline"><span class="w">savepvn</span></code>
 copy of
<code class="inline"><span class="w">name</span></code>
 or <code class="inline"><span class="w">name</span></code>
 itself is stored in the <code class="inline"><span class="w">mg_ptr</span></code>
 field, depending on
whether <code class="inline"><span class="w">namlen</span></code>
 is greater than zero or equal to zero respectively.  As a
special case, if <code class="inline"><span class="s">(</span><span class="w">name</span> &amp;&amp; <span class="w">namlen</span> == <span class="w">HEf_SVKEY</span><span class="s">)</span></code>
 then <code class="inline"><span class="w">name</span></code>
 is assumed
to contain an <code class="inline"><span class="w">SV</span>*</code>
 and is stored as-is with its REFCNT incremented.</p>
<p>The sv_magic function uses <code class="inline"><span class="w">how</span></code>
 to determine which, if any, predefined
"Magic Virtual Table" should be assigned to the <code class="inline"><span class="w">mg_virtual</span></code>
 field.
See the <a href="#Magic-Virtual-Tables">Magic Virtual Tables</a> section below.  The <code class="inline"><span class="w">how</span></code>
 argument is also
stored in the <code class="inline"><span class="w">mg_type</span></code>
 field.  The value of
<code class="inline"><span class="w">how</span></code>
 should be chosen from the set of macros
<code class="inline"><span class="w">PERL_MAGIC_foo</span></code>
 found in <i>perl.h</i>.  Note that before
these macros were added, Perl internals used to directly use character
literals, so you may occasionally come across old code or documentation
referring to 'U' magic rather than <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 for example.</p>
<p>The <code class="inline"><span class="w">obj</span></code>
 argument is stored in the <code class="inline"><span class="w">mg_obj</span></code>
 field of the <code class="inline"><span class="w">MAGIC</span></code>

structure.  If it is not the same as the <code class="inline"><span class="w">sv</span></code>
 argument, the reference
count of the <code class="inline"><span class="w">obj</span></code>
 object is incremented.  If it is the same, or if
the <code class="inline"><span class="w">how</span></code>
 argument is <code class="inline"><span class="w">PERL_MAGIC_arylen</span></code>
, or if it is a NULL pointer,
then <code class="inline"><span class="w">obj</span></code>
 is merely stored, without the reference count being incremented.</p>
<p>See also <code class="inline"><span class="w">sv_magicext</span></code>
 in <a href="perlapi.html">perlapi</a> for a more flexible way to add magic
to an SV.</p>
<p>There is also a function to add magic to an <code class="inline"><span class="w">HV</span></code>
:</p>
<pre class="verbatim"><ol><li>    <span class="w">void</span> <span class="i">hv_magic</span><span class="s">(</span><span class="w">HV</span> *<span class="w">hv</span><span class="cm">,</span> <span class="w">GV</span> *<span class="w">gv</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">how</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This simply calls <code class="inline"><span class="w">sv_magic</span></code>
 and coerces the <code class="inline"><span class="w">gv</span></code>
 argument into an <code class="inline"><span class="w">SV</span></code>
.</p>
<p>To remove the magic from an SV, call the function sv_unmagic:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/int.html">int</a> <span class="i">sv_unmagic</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">type</span></code>
 argument should be equal to the <code class="inline"><span class="w">how</span></code>
 value when the <code class="inline"><span class="w">SV</span></code>

was initially made magical.</p>
<p>However, note that <code class="inline"><span class="w">sv_unmagic</span></code>
 removes all magic of a certain <code class="inline"><span class="w">type</span></code>
 from the
<code class="inline"><span class="w">SV</span></code>
.  If you want to remove only certain
magic of a <code class="inline"><span class="w">type</span></code>
 based on the magic
virtual table, use <code class="inline"><span class="w">sv_unmagicext</span></code>
 instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/int.html">int</a> <span class="i">sv_unmagicext</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">type</span><span class="cm">,</span> <span class="w">MGVTBL</span> *<span class="w">vtbl</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Magic-Virtual-Tables"></a><h2>Magic Virtual Tables</h2>
<p>The <code class="inline"><span class="w">mg_virtual</span></code>
 field in the <code class="inline"><span class="w">MAGIC</span></code>
 structure is a pointer to an
<code class="inline"><span class="w">MGVTBL</span></code>
, which is a structure of function pointers and stands for
"Magic Virtual Table" to handle the various operations that might be
applied to that variable.</p>
<p>The <code class="inline"><span class="w">MGVTBL</span></code>
 has five (or sometimes eight) pointers to the following
routine types:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_get</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_set</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">U32</span>  <span class="s">(</span><span class="i">*svt_len</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_clear</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_free</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_copy</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <span class="w">MAGIC</span>* <span class="w">mg</span><span class="cm">,</span> <span class="w">SV</span> *<span class="w">nsv</span><span class="cm">,</span></li><li>                                          <span class="w">const</span> <span class="w">char</span> *<span class="w">name</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">namlen</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_dup</span><span class="s">)</span><span class="s">(</span><span class="w">MAGIC</span> *<span class="w">mg</span><span class="cm">,</span> <span class="w">CLONE_PARAMS</span> *<span class="w">param</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/int.html">int</a>  <span class="s">(</span><span class="i">*svt_local</span><span class="s">)</span><span class="s">(</span><span class="w">SV</span> *<span class="w">nsv</span><span class="cm">,</span> <span class="w">MAGIC</span> *<span class="w">mg</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This MGVTBL structure is set at compile-time in <i>perl.h</i> and there are
currently 32 types.  These different structures contain pointers to various
routines that perform additional actions depending on which function is
being called.</p>
<pre class="verbatim"><ol><li>   <span class="w">Function</span> <span class="w">pointer</span>    <span class="w">Action</span> <span class="w">taken</span></li><li>   ----------------    ------------</li><li>   <span class="w">svt_get</span>             <span class="w">Do</span> <span class="w">something</span> <span class="w">before</span> <span class="w">the</span> <span class="w">value</span> <span class="w">of</span> <span class="w">the</span> <span class="w">SV</span> <span class="w">is</span></li><li>                       <span class="w">retrieved</span>.</li><li>   <span class="w">svt_set</span>             <span class="w">Do</span> <span class="w">something</span> <span class="w">after</span> <span class="w">the</span> <span class="w">SV</span> <span class="w">is</span> <span class="w">assigned</span> <span class="w">a</span> <span class="w">value</span>.</li><li>   <span class="w">svt_len</span>             <span class="w">Report</span> <span class="w">on</span> <span class="w">the</span> <span class="w">SV&#39;s</span> <a class="l_k" href="functions/length.html">length</a>.</li><li>   <span class="w">svt_clear</span>           <span class="w">Clear</span> <span class="w">something</span> <span class="w">the</span> <span class="w">SV</span> <span class="w">represents</span>.</li><li>   <span class="w">svt_free</span>            <span class="w">Free</span> <span class="w">any</span> <span class="w">extra</span> <span class="w">storage</span> <span class="w">associated</span> <span class="w">with</span> <span class="w">the</span> <span class="w">SV</span>.</li><li></li><li>   <span class="w">svt_copy</span>            <span class="w">copy</span> <a class="l_k" href="functions/tied.html">tied</a> <span class="w">variable</span> <span class="w">magic</span> <span class="w">to</span> <span class="w">a</span> <a class="l_k" href="functions/tied.html">tied</a> <span class="w">element</span></li><li>   <span class="w">svt_dup</span>             <span class="w">duplicate</span> <span class="w">a</span> <span class="w">magic</span> <span class="w">structure</span> <span class="w">during</span> <span class="w">thread</span> <span class="w">cloning</span></li><li>   <span class="w">svt_local</span>           <span class="w">copy</span> <span class="w">magic</span> <span class="w">to</span> <a class="l_k" href="functions/local.html">local</a> <span class="w">value</span> <span class="w">during</span> <span class="q">&#39;local&#39;</span></li></ol></pre><p>For instance, the MGVTBL structure called <code class="inline"><span class="w">vtbl_sv</span></code>
 (which corresponds
to an <code class="inline"><span class="w">mg_type</span></code>
 of <code class="inline"><span class="w">PERL_MAGIC_sv</span></code>
) contains:</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span> <span class="w">magic_get</span><span class="cm">,</span> <span class="w">magic_set</span><span class="cm">,</span> <span class="w">magic_len</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span> <span class="s">}</span></li></ol></pre><p>Thus, when an SV is determined to be magical and of type <code class="inline"><span class="w">PERL_MAGIC_sv</span></code>
,
if a get operation is being performed, the routine <code class="inline"><span class="w">magic_get</span></code>
 is
called.  All the various routines for the various magical types begin
with <code class="inline"><span class="w">magic_</span></code>
.  NOTE: the magic routines are not considered part of
the Perl API, and may not be exported by the Perl library.</p>
<p>The last three slots are a recent addition, and for source code
compatibility they are only checked for if one of the three flags
MGf_COPY, MGf_DUP or MGf_LOCAL is set in mg_flags.
This means that most code can continue declaring
a vtable as a 5-element value.  These three are
currently used exclusively by the threading code, and are highly subject
to change.</p>
<p>The current kinds of Magic Virtual Tables are:</p>
<pre class="verbatim"><ol><li> <span class="i">mg_type</span></li><li> <span class="s">(</span><span class="w">old</span>-<span class="w">style</span> <span class="w">char</span> <a class="l_k" href="functions/and.html">and</a> <span class="w">macro</span><span class="s">)</span>   <span class="w">MGVTBL</span>         <span class="w">Type</span> <span class="w">of</span> <span class="w">magic</span></li><li> --------------------------   ------         -------------</li><li> \<span class="n">0</span> <span class="w">PERL_MAGIC_sv</span>             <span class="w">vtbl_sv</span>        <span class="w">Special</span> <a class="l_k" href="functions/scalar.html">scalar</a> <span class="w">variable</span></li><li> <span class="c">#  PERL_MAGIC_arylen         vtbl_arylen    Array length ($#ary)</span></li><li> %  <span class="w">PERL_MAGIC_rhash</span>          <span class="s">(</span><span class="w">none</span><span class="s">)</span>         <span class="w">Extra</span> <span class="w">data</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">restricted</span></li><li>                                             <span class="w">hashes</span></li><li> *  <span class="w">PERL_MAGIC_debugvar</span>       <span class="w">vtbl_debugvar</span>  <span class="i">$DB::single</span><span class="cm">,</span> <span class="w">signal</span><span class="cm">,</span> <span class="w">trace</span></li><li>                                             <span class="w">vars</span></li><li> .  <span class="w">PERL_MAGIC_pos</span>            <span class="w">vtbl_pos</span>       <a class="l_k" href="functions/pos.html">pos</a><span class="s">(</span><span class="s">)</span> <span class="w">lvalue</span></li><li> <span class="co">:</span>  <span class="w">PERL_MAGIC_symtab</span>         <span class="s">(</span><span class="w">none</span><span class="s">)</span>         <span class="w">Extra</span> <span class="w">data</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">symbol</span></li><li>                                             <span class="w">tables</span></li><li> &lt;  <span class="w">PERL_MAGIC_backref</span>        <span class="w">vtbl_backref</span>   <span class="w">For</span> <span class="w">weak</span> <a class="l_k" href="functions/ref.html">ref</a> <span class="w">data</span></li><li> <span class="i">@  PERL_MAGIC_arylen_p</span>       <span class="s">(</span><span class="w">none</span><span class="s">)</span>         <span class="w">To</span> <span class="w">move</span> <span class="w">arylen</span> <span class="w">out</span> <span class="w">of</span> <span class="w">XPVAV</span></li><li> <span class="w">B</span>  <span class="w">PERL_MAGIC_bm</span>             <span class="w">vtbl_regexp</span>    <span class="w">Boyer</span>-<span class="w">Moore</span> </li><li>                                             <span class="s">(</span><span class="w">fast</span> <span class="w">string</span> <span class="w">search</span><span class="s">)</span></li><li> <span class="w">c</span>  <span class="w">PERL_MAGIC_overload_table</span> <span class="w">vtbl_ovrld</span>     <span class="w">Holds</span> <span class="w">overload</span> <span class="w">table</span> </li><li>                                             <span class="s">(</span><span class="w">AMT</span><span class="s">)</span> <span class="w">on</span> <span class="w">stash</span></li><li> <span class="w">D</span>  <span class="w">PERL_MAGIC_regdata</span>        <span class="w">vtbl_regdata</span>   <span class="w">Regex</span> <span class="w">match</span> <span class="w">position</span> <span class="w">data</span> </li><li>                                             <span class="s">(</span><span class="i">@+</span> <a class="l_k" href="functions/and.html">and</a> <span class="i">@-</span> <span class="w">vars</span><span class="s">)</span></li><li> <span class="w">d</span>  <span class="w">PERL_MAGIC_regdatum</span>       <span class="w">vtbl_regdatum</span>  <span class="w">Regex</span> <span class="w">match</span> <span class="w">position</span> <span class="w">data</span></li><li>                                             <span class="w">element</span></li><li> <span class="w">E</span>  <span class="w">PERL_MAGIC_env</span>            <span class="w">vtbl_env</span>       <span class="i">%ENV</span> <span class="w">hash</span></li><li> <span class="w">e</span>  <span class="w">PERL_MAGIC_envelem</span>        <span class="w">vtbl_envelem</span>   <span class="i">%ENV</span> <span class="w">hash</span> <span class="w">element</span></li><li> <span class="w">f</span>  <span class="w">PERL_MAGIC_fm</span>             <span class="w">vtbl_regexp</span>    <span class="w">Formline</span> </li><li>                                             <span class="s">(</span><span class="q">&#39;compiled&#39;</span> format)</li><li><span class="hh"> g  PERL_MAGIC_regex_global   vtbl_mglob     m//g target</span></li><li><span class="hh"> H  PERL_MAGIC_hints          vtbl_hints     %^H hash</span></li><li><span class="hh"> h  PERL_MAGIC_hintselem      vtbl_hintselem %^H hash element</span></li><li><span class="hh"> I  PERL_MAGIC_isa            vtbl_isa       @ISA array</span></li><li><span class="hh"> i  PERL_MAGIC_isaelem        vtbl_isaelem   @ISA array element</span></li><li><span class="hh"> k  PERL_MAGIC_nkeys          vtbl_nkeys     scalar(keys()) lvalue</span></li><li><span class="hh"> L  PERL_MAGIC_dbfile         (none)         Debugger %_&lt;filename</span></li><li><span class="hh"> l  PERL_MAGIC_dbline         vtbl_dbline    Debugger %_&lt;filename</span></li><li><span class="hh">                                             element</span></li><li><span class="hh"> N  PERL_MAGIC_shared         (none)         Shared between threads</span></li><li><span class="hh"> n  PERL_MAGIC_shared_scalar  (none)         Shared between threads</span></li><li><span class="hh"> o  PERL_MAGIC_collxfrm       vtbl_collxfrm  Locale transformation</span></li><li><span class="hh"> P  PERL_MAGIC_tied           vtbl_pack      Tied array or hash</span></li><li><span class="hh"> p  PERL_MAGIC_tiedelem       vtbl_packelem  Tied array or hash element</span></li><li><span class="hh"> q  PERL_MAGIC_tiedscalar     vtbl_packelem  Tied scalar or handle</span></li><li><span class="hh"> r  PERL_MAGIC_qr             vtbl_regexp    Precompiled qr// regex</span></li><li><span class="hh"> S  PERL_MAGIC_sig            (none)         %SIG hash</span></li><li><span class="hh"> s  PERL_MAGIC_sigelem        vtbl_sigelem   %SIG hash element</span></li><li><span class="hh"> t  PERL_MAGIC_taint          vtbl_taint     Taintedness</span></li><li><span class="hh"> U  PERL_MAGIC_uvar           vtbl_uvar      Available for use by</span></li><li><span class="hh">                                             extensions</span></li><li><span class="hh"> u  PERL_MAGIC_uvar_elem      (none)         Reserved for use by</span></li><li><span class="hh">                                             extensions</span></li><li><span class="hh"> V  PERL_MAGIC_vstring        (none)         SV was vstring literal</span></li><li><span class="hh"> v  PERL_MAGIC_vec            vtbl_vec       vec() lvalue</span></li><li><span class="hh"> w  PERL_MAGIC_utf8           vtbl_utf8      Cached UTF-8 information</span></li><li><span class="hh"> x  PERL_MAGIC_substr         vtbl_substr    substr() lvalue</span></li><li><span class="hh"> y  PERL_MAGIC_defelem        vtbl_defelem   Shadow &quot;foreach&quot; iterator</span></li><li><span class="hh">                                             variable / smart parameter</span></li><li><span class="hh">                                             vivification</span></li><li><span class="hh"> \  PERL_MAGIC_lvref          vtbl_lvref     Lvalue reference</span></li><li><span class="hh">                                             constructor</span></li><li><span class="hh"> ]  PERL_MAGIC_checkcall      vtbl_checkcall Inlining/mutation of call</span></li><li><span class="hh">                                             to this CV</span></li><li><span class="hh"> ~  PERL_MAGIC_ext            (none)         Available for use by</span></li><li><span class="hh">                                             extensions</span></li></ol></pre><p>When an uppercase and lowercase letter both exist in the table, then the
uppercase letter is typically used to represent some kind of composite type
(a list or a hash), and the lowercase letter is used to represent an element
of that composite type.  Some internals code makes use of this case
relationship.  However, 'v' and 'V' (vec and v-string) are in no way related.</p>
<p>The <code class="inline"><span class="w">PERL_MAGIC_ext</span></code>
 and <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 magic types are defined
specifically for use by extensions and will not be used by perl itself.
Extensions can use <code class="inline"><span class="w">PERL_MAGIC_ext</span></code>
 magic to 'attach' private information
to variables (typically objects).  This is especially useful because
there is no way for normal perl code to corrupt this private information
(unlike using extra elements of a hash object).</p>
<p>Similarly, <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 magic can be used much like tie() to call a
C function any time a scalar's value is used or changed.  The <code class="inline"><span class="w">MAGIC</span></code>
's
<code class="inline"><span class="w">mg_ptr</span></code>
 field points to a <code class="inline"><span class="w">ufuncs</span></code>
 structure:</p>
<pre class="verbatim"><ol><li>    struct ufuncs {</li><li>        I32 (*uf_val)(pTHX_ IV, SV*);</li><li>        I32 (*uf_set)(pTHX_ IV, SV*);</li><li>        IV uf_index;</li><li>    };</li></ol></pre><p>When the SV is read from or written to, the <code class="inline"><span class="w">uf_val</span></code>
 or <code class="inline"><span class="w">uf_set</span></code>

function will be called with <code class="inline"><span class="w">uf_index</span></code>
 as the first arg and a pointer to
the SV as the second.  A simple example of how to add <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>

magic is shown below.  Note that the ufuncs structure is copied by
sv_magic, so you can safely allocate it on the stack.</p>
<pre class="verbatim"><ol><li>    void</li><li>    Umagic(sv)</li><li>        SV *sv;</li><li>    PREINIT:</li><li>        struct ufuncs uf;</li><li>    CODE:</li><li>        uf.uf_val   = &amp;my_get_fn;</li><li>        uf.uf_set   = &amp;my_set_fn;</li><li>        uf.uf_index = 0;</li><li>        sv_magic(sv, 0, PERL_MAGIC_uvar, (char*)&amp;uf, sizeof(uf));</li></ol></pre><p>Attaching <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 to arrays is permissible but has no effect.</p>
<p>For hashes there is a specialized hook that gives control over hash
keys (but not values).  This hook calls <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 'get' magic
if the "set" function in the <code class="inline"><span class="w">ufuncs</span></code>
 structure is NULL.  The hook
is activated whenever the hash is accessed with a key specified as
an <code class="inline"><span class="w">SV</span></code>
 through the functions <code class="inline"><span class="w">hv_store_ent</span></code>
, <code class="inline"><span class="w">hv_fetch_ent</span></code>
,
<code class="inline"><span class="w">hv_delete_ent</span></code>
, and <code class="inline"><span class="w">hv_exists_ent</span></code>
.  Accessing the key as a string
through the functions without the <code class="inline">...<span class="w">_ent</span></code>
 suffix circumvents the
hook.  See <a href="Hash/Util/FieldHash.html#GUTS">GUTS in Hash::Util::FieldHash</a> for a detailed description.</p>
<p>Note that because multiple extensions may be using <code class="inline"><span class="w">PERL_MAGIC_ext</span></code>

or <code class="inline"><span class="w">PERL_MAGIC_uvar</span></code>
 magic, it is important for extensions to take
extra care to avoid conflict.  Typically only using the magic on
objects blessed into the same class as the extension is sufficient.
For <code class="inline"><span class="w">PERL_MAGIC_ext</span></code>
 magic, it is usually a good idea to define an
<code class="inline"><span class="w">MGVTBL</span></code>
, even if all its fields will be <code class="inline"><span class="n">0</span></code>
, so that individual
<code class="inline"><span class="w">MAGIC</span></code>
 pointers can be identified as a particular kind of magic
using their magic virtual table.  <code class="inline"><span class="w">mg_findext</span></code>
 provides an easy way
to do that:</p>
<pre class="verbatim"><ol><li>    <span class="w">STATIC</span> <span class="w">MGVTBL</span> <span class="w">my_vtbl</span> = <span class="s">{</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span> <span class="s">}</span><span class="sc">;</span></li><li></li><li>    <span class="w">MAGIC</span> *<span class="w">mg</span><span class="sc">;</span></li><li>    if <span class="s">(</span><span class="s">(</span><span class="w">mg</span> = <span class="i">mg_findext</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">PERL_MAGIC_ext</span><span class="cm">,</span> <span class="i">&amp;my_vtbl</span><span class="s">)</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">/* this is really ours, not another module&#39;s PERL_MAGIC_ext */</span></li><li>        <span class="w">my_priv_data_t</span> *<span class="w">priv</span> = <span class="s">(</span><span class="w">my_priv_data_t</span> *<span class="s">)</span><span class="w">mg</span><span class="w">-&gt;mg_ptr</span><span class="sc">;</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>Also note that the <code class="inline"><span class="w">sv_set</span>*<span class="s">(</span><span class="s">)</span></code>
 and <code class="inline"><span class="w">sv_cat</span>*<span class="s">(</span><span class="s">)</span></code>
 functions described
earlier do <b>not</b> invoke 'set' magic on their targets.  This must
be done by the user either by calling the <code class="inline"><span class="i">SvSETMAGIC</span><span class="s">(</span><span class="s">)</span></code>
 macro after
calling these functions, or by using one of the <code class="inline"><span class="w">sv_set</span>*<span class="i">_mg</span><span class="s">(</span><span class="s">)</span></code>
 or
<code class="inline"><span class="w">sv_cat</span>*<span class="i">_mg</span><span class="s">(</span><span class="s">)</span></code>
 functions.  Similarly, generic C code must call the
<code class="inline"><span class="i">SvGETMAGIC</span><span class="s">(</span><span class="s">)</span></code>
 macro to invoke any 'get' magic if they use an SV
obtained from external sources in functions that don't handle magic.
See <a href="perlapi.html">perlapi</a> for a description of these functions.
For example, calls to the <code class="inline"><span class="w">sv_cat</span>*<span class="s">(</span><span class="s">)</span></code>
 functions typically need to be
followed by <code class="inline"><span class="i">SvSETMAGIC</span><span class="s">(</span><span class="s">)</span></code>
, but they don't need a prior <code class="inline"><span class="i">SvGETMAGIC</span><span class="s">(</span><span class="s">)</span></code>

since their implementation handles 'get' magic.</p>
<a name="Finding-Magic"></a><h2>Finding Magic</h2>
<pre class="verbatim"><ol><li>    <span class="w">MAGIC</span> *<span class="i">mg_find</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">type</span><span class="s">)</span><span class="sc">;</span> <span class="q">/* Finds the magic pointer of that</span></li><li>                                       <span class="q">                                       * type */</span></li></ol></pre><p>This routine returns a pointer to a <code class="inline"><span class="w">MAGIC</span></code>
 structure stored in the SV.
If the SV does not have that magical
feature, <code class="inline"><span class="w">NULL</span></code>
 is returned.  If the
SV has multiple instances of that magical feature, the first one will be
returned.  <code class="inline"><span class="w">mg_findext</span></code>
 can be used
to find a <code class="inline"><span class="w">MAGIC</span></code>
 structure of an SV
based on both its magic type and its magic virtual table:</p>
<pre class="verbatim"><ol><li>    <span class="w">MAGIC</span> *<span class="i">mg_findext</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">type</span><span class="cm">,</span> <span class="w">MGVTBL</span> *<span class="w">vtbl</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Also, if the SV passed to <code class="inline"><span class="w">mg_find</span></code>
 or <code class="inline"><span class="w">mg_findext</span></code>
 is not of type
SVt_PVMG, Perl may core dump.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/int.html">int</a> <span class="i">mg_copy</span><span class="s">(</span><span class="w">SV</span>* <span class="w">sv</span><span class="cm">,</span> <span class="w">SV</span>* <span class="w">nsv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span>* <span class="w">key</span><span class="cm">,</span> <span class="w">STRLEN</span> <span class="w">klen</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This routine checks to see what types of magic <code class="inline"><span class="w">sv</span></code>
 has.  If the mg_type
field is an uppercase letter, then the mg_obj is copied to <code class="inline"><span class="w">nsv</span></code>
, but
the mg_type field is changed to be the lowercase letter.</p>
<a name="Understanding-the-Magic-of-Tied-Hashes-and-Arrays"></a><h2>Understanding the Magic of Tied Hashes and Arrays</h2>
<p>Tied hashes and arrays are magical beasts of the <code class="inline"><span class="w">PERL_MAGIC_tied</span></code>

magic type.</p>
<p>WARNING: As of the 5.004 release, proper usage of the array and hash
access functions requires understanding a few caveats.  Some
of these caveats are actually considered bugs in the API, to be fixed
in later releases, and are bracketed with [MAYCHANGE] below.  If
you find yourself actually applying such information in this section, be
aware that the behavior may change in the future, umm, without warning.</p>
<p>The perl tie function associates a variable with an object that implements
the various GET, SET, etc methods.  To perform the equivalent of the perl
tie function from an XSUB, you must mimic this behaviour.  The code below
carries out the necessary steps -- firstly it creates a new hash, and then
creates a second hash which it blesses into the class which will implement
the tie methods.  Lastly it ties the two hashes together, and returns a
reference to the new tied hash.  Note that the code below does NOT call the
TIEHASH method in the MyTie class -
see <a href="#Calling-Perl-Routines-from-within-C-Programs">Calling Perl Routines from within C Programs</a> for details on how
to do this.</p>
<pre class="verbatim"><ol><li>    SV*</li><li>    mytie()</li><li>    PREINIT:</li><li>        HV *hash;</li><li>        HV *stash;</li><li>        SV *tie;</li><li>    CODE:</li><li>        hash = newHV();</li><li>        tie = newRV_noinc((SV*)newHV());</li><li>        stash = gv_stashpv("MyTie", GV_ADD);</li><li>        sv_bless(tie, stash);</li><li>        hv_magic(hash, (GV*)tie, PERL_MAGIC_tied);</li><li>        RETVAL = newRV_noinc(hash);</li><li>    OUTPUT:</li><li>        RETVAL</li></ol></pre><p>The <code class="inline"><span class="w">av_store</span></code>
 function, when given a tied array argument, merely
copies the magic of the array onto the value to be "stored", using
<code class="inline"><span class="w">mg_copy</span></code>
.  It may also return NULL, indicating that the value did not
actually need to be stored in the array.  [MAYCHANGE] After a call to
<code class="inline"><span class="w">av_store</span></code>
 on a tied array, the caller will usually need to call
<code class="inline"><span class="i">mg_set</span><span class="s">(</span><span class="w">val</span><span class="s">)</span></code>
 to actually invoke the perl level "STORE" method on the
TIEARRAY object.  If <code class="inline"><span class="w">av_store</span></code>
 did return NULL, a call to
<code class="inline"><span class="i">SvREFCNT_dec</span><span class="s">(</span><span class="w">val</span><span class="s">)</span></code>
 will also be usually necessary to avoid a memory
leak. [/MAYCHANGE]</p>
<p>The previous paragraph is applicable verbatim to tied hash access using the
<code class="inline"><span class="w">hv_store</span></code>
 and <code class="inline"><span class="w">hv_store_ent</span></code>
 functions as well.</p>
<p><code class="inline"><span class="w">av_fetch</span></code>
 and the corresponding hash functions <code class="inline"><span class="w">hv_fetch</span></code>
 and
<code class="inline"><span class="w">hv_fetch_ent</span></code>
 actually return an undefined mortal value whose magic
has been initialized using <code class="inline"><span class="w">mg_copy</span></code>
.  Note the value so returned does not
need to be deallocated, as it is already mortal.  [MAYCHANGE] But you will
need to call <code class="inline"><span class="i">mg_get</span><span class="s">(</span><span class="s">)</span></code>
 on the returned value in order to actually invoke
the perl level "FETCH" method on the underlying TIE object.  Similarly,
you may also call <code class="inline"><span class="i">mg_set</span><span class="s">(</span><span class="s">)</span></code>
 on the return value after possibly assigning
a suitable value to it using <code class="inline"><span class="w">sv_setsv</span></code>
,  which will invoke the "STORE"
method on the TIE object. [/MAYCHANGE]</p>
<p>[MAYCHANGE]
In other words, the array or hash fetch/store functions don't really
fetch and store actual values in the case of tied arrays and hashes.  They
merely call <code class="inline"><span class="w">mg_copy</span></code>
 to attach magic to the values that were meant to be
"stored" or "fetched".  Later calls to <code class="inline"><span class="w">mg_get</span></code>
 and <code class="inline"><span class="w">mg_set</span></code>
 actually
do the job of invoking the TIE methods on the underlying objects.  Thus
the magic mechanism currently implements a kind of lazy access to arrays
and hashes.</p>
<p>Currently (as of perl version 5.004), use of the hash and array access
functions requires the user to be aware of whether they are operating on
"normal" hashes and arrays, or on their tied variants.  The API may be
changed to provide more transparent access to both tied and normal data
types in future versions.
[/MAYCHANGE]</p>
<p>You would do well to understand that the TIEARRAY and TIEHASH interfaces
are mere sugar to invoke some perl method calls while using the uniform hash
and array syntax.  The use of this sugar imposes some overhead (typically
about two to four extra opcodes per FETCH/STORE operation, in addition to
the creation of all the mortal variables required to invoke the methods).
This overhead will be comparatively small if the TIE methods are themselves
substantial, but if they are only a few statements long, the overhead
will not be insignificant.</p>
<a name="Localizing-changes"></a><h2>Localizing changes</h2>
<p>Perl has a very handy construction</p>
<pre class="verbatim"><ol><li>  <span class="s">{</span></li><li>    <a class="l_k" href="functions/local.html">local</a> <span class="i">$var</span> = <span class="n">2</span><span class="sc">;</span></li><li>    ...</li><li>  <span class="s">}</span></li></ol></pre><p>This construction is <i>approximately</i> equivalent to</p>
<pre class="verbatim"><ol><li>  <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$oldvar</span> = <span class="i">$var</span><span class="sc">;</span></li><li>    <span class="i">$var</span> = <span class="n">2</span><span class="sc">;</span></li><li>    ...</li><li>    <span class="i">$var</span> = <span class="i">$oldvar</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>The biggest difference is that the first construction would
reinstate the initial value of $var, irrespective of how control exits
the block: <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>, <code class="inline"><a class="l_k" href="functions/return.html">return</a></code>, <code class="inline"><a class="l_k" href="functions/die.html">die</a></code>/<code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>, etc.  It is a little bit
more efficient as well.</p>
<p>There is a way to achieve a similar task from C via Perl API: create a
<i>pseudo-block</i>, and arrange for some changes to be automatically
undone at the end of it, either explicit, or via a non-local exit (via
die()).  A <i>block</i>-like construct is created by a pair of
<code class="inline"><span class="w">ENTER</span></code>
/<code class="inline"><span class="w">LEAVE</span></code>
 macros (see <a href="perlcall.html#Returning-a-Scalar">Returning a Scalar in perlcall</a>).
Such a construct may be created specially for some important localized
task, or an existing one (like boundaries of enclosing Perl
subroutine/block, or an existing pair for freeing TMPs) may be
used.  (In the second case the overhead of additional localization must
be almost negligible.)  Note that any XSUB is automatically enclosed in
an <code class="inline"><span class="w">ENTER</span></code>
/<code class="inline"><span class="w">LEAVE</span></code>
 pair.</p>
<p>Inside such a <i>pseudo-block</i> the following service is available:</p>
<ul>
<li><a name="SAVEINT(int-i)"></a><b><code class="inline"><span class="i">SAVEINT</span><span class="s">(</span><a class="l_k" href="functions/int.html">int</a> <span class="w">i</span><span class="s">)</span></code>
</b>
</li>
<li><a name="SAVEIV(IV-i)"></a><b><code class="inline"><span class="i">SAVEIV</span><span class="s">(</span><span class="w">IV</span> <span class="w">i</span><span class="s">)</span></code>
</b>
</li>
<li><a name="SAVEI32(I32-i)"></a><b><code class="inline"><span class="i">SAVEI32</span><span class="s">(</span><span class="w">I32</span> <span class="w">i</span><span class="s">)</span></code>
</b>
</li>
<li><a name="SAVELONG(long-i)"></a><b><code class="inline"><span class="i">SAVELONG</span><span class="s">(</span><span class="w">long</span> <span class="w">i</span><span class="s">)</span></code>
</b>
<p>These macros arrange things to restore the value of integer variable
<code class="inline"><span class="w">i</span></code>
 at the end of enclosing <i>pseudo-block</i>.</p>
</li>
<li><a name="SAVESPTR(s)"></a><b><code class="inline">SAVESPTR(s)</code></b>
</li>
<li><a name="SAVEPPTR(p)"></a><b><code class="inline"><span class="i">SAVEPPTR</span><span class="s">(</span><span class="w">p</span><span class="s">)</span></code>
</b>
<p>These macros arrange things to restore the value of pointers <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> and
<code class="inline"><span class="w">p</span></code>
.  <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> must be a pointer of a type which survives conversion to
<code class="inline"><span class="w">SV</span>*</code>
 and back, <code class="inline"><span class="w">p</span></code>
 should be able to survive conversion to <code class="inline"><span class="w">char</span>*</code>

and back.</p>
</li>
<li><a name="SAVEFREESV(SV-*sv)"></a><b><code class="inline"><span class="i">SAVEFREESV</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="s">)</span></code>
</b>
<p>The refcount of <code class="inline"><span class="w">sv</span></code>
 would be decremented at the end of
<i>pseudo-block</i>.  This is similar to <code class="inline"><span class="w">sv_2mortal</span></code>
 in that it is also a
mechanism for doing a delayed <code class="inline"><span class="w">SvREFCNT_dec</span></code>
.  However, while <code class="inline"><span class="w">sv_2mortal</span></code>

extends the lifetime of <code class="inline"><span class="w">sv</span></code>
 until the beginning of the next statement,
<code class="inline"><span class="w">SAVEFREESV</span></code>
 extends it until the end of the enclosing scope.  These
lifetimes can be wildly different.</p>
<p>Also compare <code class="inline"><span class="w">SAVEMORTALIZESV</span></code>
.</p>
</li>
<li><a name="SAVEMORTALIZESV(SV-*sv)"></a><b><code class="inline"><span class="i">SAVEMORTALIZESV</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="s">)</span></code>
</b>
<p>Just like <code class="inline"><span class="w">SAVEFREESV</span></code>
, but mortalizes <code class="inline"><span class="w">sv</span></code>
 at the end of the current
scope instead of decrementing its reference count.  This usually has the
effect of keeping <code class="inline"><span class="w">sv</span></code>
 alive until the statement that called the currently
live scope has finished executing.</p>
</li>
<li><a name="SAVEFREEOP(OP-*op)"></a><b><code class="inline"><span class="i">SAVEFREEOP</span><span class="s">(</span><span class="w">OP</span> *<span class="w">op</span><span class="s">)</span></code>
</b>
<p>The <code class="inline"><span class="w">OP</span> *</code>
 is op_free()ed at the end of <i>pseudo-block</i>.</p>
</li>
<li><a name="SAVEFREEPV(p)"></a><b><code class="inline"><span class="i">SAVEFREEPV</span><span class="s">(</span><span class="w">p</span><span class="s">)</span></code>
</b>
<p>The chunk of memory which is pointed to by <code class="inline"><span class="w">p</span></code>
 is Safefree()ed at the
end of <i>pseudo-block</i>.</p>
</li>
<li><a name="SAVECLEARSV(SV-*sv)"></a><b><code class="inline"><span class="i">SAVECLEARSV</span><span class="s">(</span><span class="w">SV</span> *<span class="w">sv</span><span class="s">)</span></code>
</b>
<p>Clears a slot in the current scratchpad which corresponds to <code class="inline"><span class="w">sv</span></code>
 at
the end of <i>pseudo-block</i>.</p>
</li>
<li><a name="SAVEDELETE(HV-*hv%2c-char-*key%2c-I32-length)"></a><b><code class="inline"><span class="i">SAVEDELETE</span><span class="s">(</span><span class="w">HV</span> *<span class="w">hv</span><span class="cm">,</span> <span class="w">char</span> *<span class="w">key</span><span class="cm">,</span> <span class="w">I32</span> <a class="l_k" href="functions/length.html">length</a><span class="s">)</span></code>
</b>
<p>The key <code class="inline"><span class="w">key</span></code>
 of <code class="inline"><span class="w">hv</span></code>
 is deleted at the end of <i>pseudo-block</i>.  The
string pointed to by <code class="inline"><span class="w">key</span></code>
 is Safefree()ed.  If one has a <i>key</i> in
short-lived storage, the corresponding string may be reallocated like
this:</p>
<pre class="verbatim"><ol><li>  <span class="i">SAVEDELETE</span><span class="s">(</span><span class="w">PL_defstash</span><span class="cm">,</span> <span class="i">savepv</span><span class="s">(</span><span class="w">tmpbuf</span><span class="s">)</span><span class="cm">,</span> <span class="i">strlen</span><span class="s">(</span><span class="w">tmpbuf</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="SAVEDESTRUCTOR(DESTRUCTORFUNC_NOCONTEXT_t-f%2c-void-*p)"></a><b><code class="inline"><span class="i">SAVEDESTRUCTOR</span><span class="s">(</span><span class="w">DESTRUCTORFUNC_NOCONTEXT_t</span> <span class="w">f</span><span class="cm">,</span> <span class="w">void</span> *<span class="w">p</span><span class="s">)</span></code>
</b>
<p>At the end of <i>pseudo-block</i> the function <code class="inline"><span class="w">f</span></code>
 is called with the
only argument <code class="inline"><span class="w">p</span></code>
.</p>
</li>
<li><a name="SAVEDESTRUCTOR_X(DESTRUCTORFUNC_t-f%2c-void-*p)"></a><b><code class="inline"><span class="i">SAVEDESTRUCTOR_X</span><span class="s">(</span><span class="w">DESTRUCTORFUNC_t</span> <span class="w">f</span><span class="cm">,</span> <span class="w">void</span> *<span class="w">p</span><span class="s">)</span></code>
</b>
<p>At the end of <i>pseudo-block</i> the function <code class="inline"><span class="w">f</span></code>
 is called with the
implicit context argument (if any), and <code class="inline"><span class="w">p</span></code>
.</p>
</li>
<li><a name="SAVESTACK_POS()"></a><b><code class="inline"><span class="i">SAVESTACK_POS</span><span class="s">(</span><span class="s">)</span></code>
</b>
<p>The current offset on the Perl internal stack (cf. <code class="inline"><span class="w">SP</span></code>
) is restored
at the end of <i>pseudo-block</i>.</p>
</li>
</ul>
<p>The following API list contains functions, thus one needs to
provide pointers to the modifiable data explicitly (either C pointers,
or Perlish <code class="inline"><span class="w">GV</span> *</code>
s).  Where the above macros take <code class="inline"><a class="l_k" href="functions/int.html">int</a></code>, a similar
function takes <code class="inline"><a class="l_k" href="functions/int.html">int</a> <span class="i">*</span></code>
.</p>
<ul>
<li><a name="SV*-save_scalar(GV-*gv)"></a><b><code class="inline"><span class="w">SV</span>* <span class="i">save_scalar</span><span class="s">(</span><span class="w">GV</span> *<span class="w">gv</span><span class="s">)</span></code>
</b>
<p>Equivalent to Perl code <code class="inline"><a class="l_k" href="functions/local.html">local</a> <span class="i">$gv</span></code>
.</p>
</li>
<li><a name="AV*-save_ary(GV-*gv)"></a><b><code class="inline"><span class="w">AV</span>* <span class="i">save_ary</span><span class="s">(</span><span class="w">GV</span> *<span class="w">gv</span><span class="s">)</span></code>
</b>
</li>
<li><a name="HV*-save_hash(GV-*gv)"></a><b><code class="inline"><span class="w">HV</span>* <span class="i">save_hash</span><span class="s">(</span><span class="w">GV</span> *<span class="w">gv</span><span class="s">)</span></code>
</b>
<p>Similar to <code class="inline"><span class="w">save_scalar</span></code>
, but localize <code class="inline"><span class="i">@gv</span></code>
 and <code class="inline"><span class="i">%gv</span></code>
.</p>
</li>
<li><a name="void-save_item(SV-*item)"></a><b><code class="inline"><span class="w">void</span> <span class="i">save_item</span><span class="s">(</span><span class="w">SV</span> *<span class="w">item</span><span class="s">)</span></code>
</b>
<p>Duplicates the current value of <code class="inline"><span class="w">SV</span></code>
, on the exit from the current
<code class="inline"><span class="w">ENTER</span></code>
/<code class="inline"><span class="w">LEAVE</span></code>
 <i>pseudo-block</i> will restore the value of <code class="inline"><span class="w">SV</span></code>

using the stored value.  It doesn't handle magic.  Use <code class="inline"><span class="w">save_scalar</span></code>
 if
magic is affected.</p>
</li>
<li><a name="void-save_list(SV-**sarg%2c-I32-maxsarg)"></a><b><code class="inline"><span class="w">void</span> <span class="i">save_list</span><span class="s">(</span><span class="w">SV</span> **<span class="w">sarg</span><span class="cm">,</span> <span class="w">I32</span> <span class="w">maxsarg</span><span class="s">)</span></code>
</b>
<p>A variant of <code class="inline"><span class="w">save_item</span></code>
 which takes multiple arguments via an array
<code class="inline"><span class="w">sarg</span></code>
 of <code class="inline"><span class="w">SV</span>*</code>
 of length <code class="inline"><span class="w">maxsarg</span></code>
.</p>
</li>
<li><a name="SV*-save_svref(SV-**sptr)"></a><b><code class="inline"><span class="w">SV</span>* <span class="i">save_svref</span><span class="s">(</span><span class="w">SV</span> **<span class="w">sptr</span><span class="s">)</span></code>
</b>
<p>Similar to <code class="inline"><span class="w">save_scalar</span></code>
, but will reinstate an <code class="inline"><span class="w">SV</span> *</code>
.</p>
</li>
<li><a name="void-save_aptr(AV-**aptr)"></a><b><code class="inline"><span class="w">void</span> <span class="i">save_aptr</span><span class="s">(</span><span class="w">AV</span> **<span class="w">aptr</span><span class="s">)</span></code>
</b>
</li>
<li><a name="void-save_hptr(HV-**hptr)"></a><b><code class="inline"><span class="w">void</span> <span class="i">save_hptr</span><span class="s">(</span><span class="w">HV</span> **<span class="w">hptr</span><span class="s">)</span></code>
</b>
<p>Similar to <code class="inline"><span class="w">save_svref</span></code>
, but localize <code class="inline"><span class="w">AV</span> *</code>
 and <code class="inline"><span class="w">HV</span> *</code>
.</p>
</li>
</ul>
<p>The <code class="inline"><span class="w">Alias</span></code>
 module implements localization of the basic types within the
<i>caller's scope</i>.  People who are interested in how to localize things in
the containing scope should take a look there too.</p>
<a name="Subroutines"></a><h1>Subroutines</h1>
<a name="XSUBs-and-the-Argument-Stack"></a><h2>XSUBs and the Argument Stack</h2>
<p>The XSUB mechanism is a simple way for Perl programs to access C subroutines.
An XSUB routine will have a stack that contains the arguments from the Perl
program, and a way to map from the Perl data structures to a C equivalent.</p>
<p>The stack arguments are accessible through the <code class="inline"><span class="i">ST</span><span class="s">(</span><span class="w">n</span><span class="s">)</span></code>
 macro, which returns
the <code class="inline"><span class="w">n</span></code>
'th stack argument.  Argument 0 is the first argument passed in the
Perl subroutine call.  These arguments are <code class="inline"><span class="w">SV</span>*</code>
, and can be used anywhere
an <code class="inline"><span class="w">SV</span>*</code>
 is used.</p>
<p>Most of the time, output from the C routine can be handled through use of
the RETVAL and OUTPUT directives.  However, there are some cases where the
argument stack is not already long enough to handle all the return values.
An example is the POSIX tzname() call, which takes no arguments, but returns
two, the local time zone's standard and summer time abbreviations.</p>
<p>To handle this situation, the PPCODE directive is used and the stack is
extended using the macro:</p>
<pre class="verbatim"><ol><li>    <span class="i">EXTEND</span><span class="s">(</span><span class="w">SP</span><span class="cm">,</span> <span class="w">num</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>where <code class="inline"><span class="w">SP</span></code>
 is the macro that represents the local copy of the stack pointer,
and <code class="inline"><span class="w">num</span></code>
 is the number of elements the stack should be extended by.</p>
<p>Now that there is room on the stack, values can be pushed on it using <code class="inline"><span class="w">PUSHs</span></code>

macro.  The pushed values will often need to be "mortal" (See
<a href="#Reference-Counts-and-Mortality">Reference Counts and Mortality</a>):</p>
<pre class="verbatim"><ol><li>    <span class="i">PUSHs</span><span class="s">(</span><span class="i">sv_2mortal</span><span class="s">(</span><span class="i">newSViv</span><span class="s">(</span><span class="w">an_integer</span><span class="s">)</span><span class="s">)</span><span class="s">)</span></li><li>    <span class="i">PUSHs</span><span class="s">(</span><span class="i">sv_2mortal</span><span class="s">(</span><span class="i">newSVuv</span><span class="s">(</span><span class="w">an_unsigned_integer</span><span class="s">)</span><span class="s">)</span><span class="s">)</span></li><li>    <span class="i">PUSHs</span><span class="s">(</span><span class="i">sv_2mortal</span><span class="s">(</span><span class="i">newSVnv</span><span class="s">(</span><span class="w">a_double</span><span class="s">)</span><span class="s">)</span><span class="s">)</span></li><li>    <span class="i">PUSHs</span><span class="s">(</span><span class="i">sv_2mortal</span><span class="s">(</span><span class="i">newSVpv</span><span class="s">(</span><span class="q">&quot;Some String&quot;</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="s">)</span><span class="s">)</span></li><li>    /<span class="i">* Although</span> <span class="w">the</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">example</span> <span class="w">is</span> <span class="w">better</span> <span class="w">written</span> <span class="w">as</span> <span class="w">the</span> <span class="w">more</span></li><li>     * <span class="w">efficient</span><span class="co">:</span> <span class="i">*/</span></li><li>    <span class="i">PUSHs</span><span class="s">(</span><span class="i">newSVpvs_flags</span><span class="s">(</span><span class="q">&quot;Some String&quot;</span><span class="cm">,</span> <span class="w">SVs_TEMP</span><span class="s">)</span><span class="s">)</span></li></ol></pre><p>And now the Perl program calling <code class="inline"><span class="w">tzname</span></code>
, the two values will be assigned
as in:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$standard_abbrev</span><span class="cm">,</span> <span class="i">$summer_abbrev</span><span class="s">)</span> = <span class="w">POSIX::tzname</span><span class="sc">;</span></li></ol></pre><p>An alternate (and possibly simpler) method to pushing values on the stack is
to use the macro:</p>
<pre class="verbatim"><ol><li>    <span class="i">XPUSHs</span><span class="s">(</span><span class="w">SV</span>*<span class="s">)</span></li></ol></pre><p>This macro automatically adjusts the stack for you, if needed.  Thus, you
do not need to call <code class="inline"><span class="w">EXTEND</span></code>
 to extend the stack.</p>
<p>Despite their suggestions in earlier versions of this document the macros
<code class="inline">(X)PUSH[iunp]</code> are <i>not</i> suited to XSUBs which return multiple results.
For that, either stick to the <code class="inline">(X)PUSHs</code> macros shown above, or use the new
<code class="inline"><a class="l_k" href="functions/m.html">m(X)PUSH[iunp]</a></code> macros instead; see <a href="#Putting-a-C-value-on-Perl-stack">Putting a C value on Perl stack</a>.</p>
<p>For more information, consult <a href="perlxs.html">perlxs</a> and <a href="perlxstut.html">perlxstut</a>.</p>
<a name="Autoloading-with-XSUBs"></a><h2>Autoloading with XSUBs</h2>
<p>If an AUTOLOAD routine is an XSUB, as with Perl subroutines, Perl puts the
fully-qualified name of the autoloaded subroutine in the $AUTOLOAD variable
of the XSUB's package.</p>
<p>But it also puts the same information in certain fields of the XSUB itself:</p>
<pre class="verbatim"><ol><li>    <span class="w">HV</span> *<span class="w">stash</span>           = <span class="i">CvSTASH</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">const</span> <span class="w">char</span> *<span class="w">subname</span> = <span class="i">SvPVX</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">STRLEN</span> <span class="w">name_length</span>  = <span class="i">SvCUR</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span><span class="sc">;</span> <span class="q">/* in bytes */</span></li><li>    <span class="w">U32</span> <span class="w">is_utf8</span>         = <span class="i">SvUTF8</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="i">SvPVX</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span></code>
 contains just the sub name itself, not including the package.
For an AUTOLOAD routine in UNIVERSAL or one of its superclasses,
<code class="inline"><span class="i">CvSTASH</span><span class="s">(</span><span class="w">cv</span><span class="s">)</span></code>
 returns NULL during a method call on a nonexistent package.</p>
<p><b>Note</b>: Setting $AUTOLOAD stopped working in 5.6.1, which did not support
XS AUTOLOAD subs at all.  Perl 5.8.0 introduced the use of fields in the
XSUB itself.  Perl 5.16.0 restored the setting of $AUTOLOAD.  If you need
to support 5.8-5.14, use the XSUB's fields.</p>
<a name="Calling-Perl-Routines-from-within-C-Programs"></a><h2>Calling Perl Routines from within C Programs</h2>
<p>There are four routines that can be used to call a Perl subroutine from
within a C program.  These four are:</p>
<pre class="verbatim"><ol><li>    <span class="w">I32</span>  <span class="i">call_sv</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">I32</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">I32</span>  <span class="i">call_pv</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">I32</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">I32</span>  <span class="i">call_method</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">I32</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">I32</span>  <span class="i">call_argv</span><span class="s">(</span><span class="w">const</span> <span class="w">char</span>*<span class="cm">,</span> <span class="w">I32</span><span class="cm">,</span> <span class="w">char</span>**<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The routine most often used is <code class="inline"><span class="w">call_sv</span></code>
.  The <code class="inline"><span class="w">SV</span>*</code>
 argument
contains either the name of the Perl subroutine to be called, or a
reference to the subroutine.  The second argument consists of flags
that control the context in which the subroutine is called, whether
or not the subroutine is being passed arguments, how errors should be
trapped, and how to treat return values.</p>
<p>All four routines return the number of arguments that the subroutine returned
on the Perl stack.</p>
<p>These routines used to be called <code class="inline"><span class="w">perl_call_sv</span></code>
, etc., before Perl v5.6.0,
but those names are now deprecated; macros of the same name are provided for
compatibility.</p>
<p>When using any of these routines (except <code class="inline"><span class="w">call_argv</span></code>
), the programmer
must manipulate the Perl stack.  These include the following macros and
functions:</p>
<pre class="verbatim"><ol><li>    dSP</li><li>    SP</li><li>    PUSHMARK()</li><li>    PUTBACK</li><li>    SPAGAIN</li><li>    ENTER</li><li>    SAVETMPS</li><li>    FREETMPS</li><li>    LEAVE</li><li>    XPUSH*()</li><li>    POP*()</li></ol></pre><p>For a detailed description of calling conventions from C to Perl,
consult <a href="perlcall.html">perlcall</a>.</p>
<a name="Putting-a-C-value-on-Perl-stack"></a><h2>Putting a C value on Perl stack</h2>
<p>A lot of opcodes (this is an elementary operation in the internal perl
stack machine) put an SV* on the stack.  However, as an optimization
the corresponding SV is (usually) not recreated each time.  The opcodes
reuse specially assigned SVs (<i>target</i>s) which are (as a corollary)
not constantly freed/created.</p>
<p>Each of the targets is created only once (but see
<a href="#Scratchpads-and-recursion">Scratchpads and recursion</a> below), and when an opcode needs to put
an integer, a double, or a string on stack, it just sets the
corresponding parts of its <i>target</i> and puts the <i>target</i> on stack.</p>
<p>The macro to put this target on stack is <code class="inline"><span class="w">PUSHTARG</span></code>
, and it is
directly used in some opcodes, as well as indirectly in zillions of
others, which use it via <code class="inline">(X)PUSH[iunp]</code>.</p>
<p>Because the target is reused, you must be careful when pushing multiple
values on the stack.  The following code will not do what you think:</p>
<pre class="verbatim"><ol><li>    <span class="i">XPUSHi</span><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">XPUSHi</span><span class="s">(</span><span class="n">20</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This translates as "set <code class="inline"><span class="w">TARG</span></code>
 to 10, push a pointer to <code class="inline"><span class="w">TARG</span></code>
 onto
the stack; set <code class="inline"><span class="w">TARG</span></code>
 to 20, push a pointer to <code class="inline"><span class="w">TARG</span></code>
 onto the stack".
At the end of the operation, the stack does not contain the values 10
and 20, but actually contains two pointers to <code class="inline"><span class="w">TARG</span></code>
, which we have set
to 20.</p>
<p>If you need to push multiple different values then you should either use
the <code class="inline">(X)PUSHs</code> macros, or else use the new <code class="inline"><a class="l_k" href="functions/m.html">m(X)PUSH[iunp]</a></code> macros,
none of which make use of <code class="inline"><span class="w">TARG</span></code>
.  The <code class="inline">(X)PUSHs</code> macros simply push an
SV* on the stack, which, as noted under <a href="#XSUBs-and-the-Argument-Stack">XSUBs and the Argument Stack</a>,
will often need to be "mortal".  The new <code class="inline"><a class="l_k" href="functions/m.html">m(X)PUSH[iunp]</a></code> macros make
this a little easier to achieve by creating a new mortal for you (via
<code class="inline">(X)PUSHmortal</code>), pushing that onto the stack (extending it if necessary
in the case of the <code class="inline"><span class="w">mXPUSH</span><span class="s">[</span><span class="w">iunp</span><span class="s">]</span></code>
 macros), and then setting its value.
Thus, instead of writing this to "fix" the example above:</p>
<pre class="verbatim"><ol><li>    XPUSHs(sv_2mortal(newSViv(10)))</li><li>    XPUSHs(sv_2mortal(newSViv(20)))</li></ol></pre><p>you can simply write:</p>
<pre class="verbatim"><ol><li>    mXPUSHi(10)</li><li>    mXPUSHi(20)</li></ol></pre><p>On a related note, if you do use <code class="inline">(X)PUSH[iunp]</code>, then you're going to
need a <code class="inline"><span class="w">dTARG</span></code>
 in your variable declarations so that the <code class="inline"><span class="i">*PUSH</span>*</code>

macros can make use of the local variable <code class="inline"><span class="w">TARG</span></code>
.  See also <code class="inline"><span class="w">dTARGET</span></code>

and <code class="inline"><span class="w">dXSTARG</span></code>
.</p>
<a name="Scratchpads"></a><h2>Scratchpads</h2>
<p>The question remains on when the SVs which are <i>target</i>s for opcodes
are created.  The answer is that they are created when the current
unit--a subroutine or a file (for opcodes for statements outside of
subroutines)--is compiled.  During this time a special anonymous Perl
array is created, which is called a scratchpad for the current unit.</p>
<p>A scratchpad keeps SVs which are lexicals for the current unit and are
targets for opcodes.  A previous version of this document
stated that one can deduce that an SV lives on a scratchpad
by looking on its flags: lexicals have <code class="inline"><span class="w">SVs_PADMY</span></code>
 set, and
<i>target</i>s have <code class="inline"><span class="w">SVs_PADTMP</span></code>
 set.  But this has never been fully true.
<code class="inline"><span class="w">SVs_PADMY</span></code>
 could be set on a variable that no longer resides in any pad.
While <i>target</i>s do have <code class="inline"><span class="w">SVs_PADTMP</span></code>
 set, it can also be set on variables
that have never resided in a pad, but nonetheless act like <i>target</i>s.  As
of perl 5.21.5, the <code class="inline"><span class="w">SVs_PADMY</span></code>
 flag is no longer used and is defined as
0.  <code class="inline"><span class="i">SvPADMY</span><span class="s">(</span><span class="s">)</span></code>
 now returns true for anything without <code class="inline"><span class="w">SVs_PADTMP</span></code>
.</p>
<p>The correspondence between OPs and <i>target</i>s is not 1-to-1.  Different
OPs in the compile tree of the unit can use the same target, if this
would not conflict with the expected life of the temporary.</p>
<a name="Scratchpads-and-recursion"></a><h2>Scratchpads and recursion</h2>
<p>In fact it is not 100% true that a compiled unit contains a pointer to
the scratchpad AV.  In fact it contains a pointer to an AV of
(initially) one element, and this element is the scratchpad AV.  Why do
we need an extra level of indirection?</p>
<p>The answer is <b>recursion</b>, and maybe <b>threads</b>.  Both
these can create several execution pointers going into the same
subroutine.  For the subroutine-child not write over the temporaries
for the subroutine-parent (lifespan of which covers the call to the
child), the parent and the child should have different
scratchpads.  (<i>And</i> the lexicals should be separate anyway!)</p>
<p>So each subroutine is born with an array of scratchpads (of length 1).
On each entry to the subroutine it is checked that the current
depth of the recursion is not more than the length of this array, and
if it is, new scratchpad is created and pushed into the array.</p>
<p>The <i>target</i>s on this scratchpad are <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>s, but they are already
marked with correct flags.</p>
<a name="Memory-Allocation"></a><h1>Memory Allocation</h1>
<a name="Allocation"></a><h2>Allocation</h2>
<p>All memory meant to be used with the Perl API functions should be manipulated
using the macros described in this section.  The macros provide the necessary
transparency between differences in the actual malloc implementation that is
used within perl.</p>
<p>It is suggested that you enable the version of malloc that is distributed
with Perl.  It keeps pools of various sizes of unallocated memory in
order to satisfy allocation requests more quickly.  However, on some
platforms, it may cause spurious malloc or free errors.</p>
<p>The following three macros are used to initially allocate memory :</p>
<pre class="verbatim"><ol><li>    <span class="i">Newx</span><span class="s">(</span><span class="w">pointer</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Newxc</span><span class="s">(</span><span class="w">pointer</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="cm">,</span> <span class="w">cast</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Newxz</span><span class="s">(</span><span class="w">pointer</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The first argument <code class="inline"><span class="w">pointer</span></code>
 should be the name of a variable that will
point to the newly allocated memory.</p>
<p>The second and third arguments <code class="inline"><span class="w">number</span></code>
 and <code class="inline"><span class="w">type</span></code>
 specify how many of
the specified type of data structure should be allocated.  The argument
<code class="inline"><span class="w">type</span></code>
 is passed to <code class="inline"><span class="w">sizeof</span></code>
.  The final argument to <code class="inline"><span class="w">Newxc</span></code>
, <code class="inline"><span class="w">cast</span></code>
,
should be used if the <code class="inline"><span class="w">pointer</span></code>
 argument is different from the <code class="inline"><span class="w">type</span></code>

argument.</p>
<p>Unlike the <code class="inline"><span class="w">Newx</span></code>
 and <code class="inline"><span class="w">Newxc</span></code>
 macros, the <code class="inline"><span class="w">Newxz</span></code>
 macro calls <code class="inline"><span class="w">memzero</span></code>

to zero out all the newly allocated memory.</p>
<a name="Reallocation"></a><h2>Reallocation</h2>
<pre class="verbatim"><ol><li>    <span class="i">Renew</span><span class="s">(</span><span class="w">pointer</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Renewc</span><span class="s">(</span><span class="w">pointer</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="cm">,</span> <span class="w">cast</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Safefree</span><span class="s">(</span><span class="w">pointer</span><span class="s">)</span></li></ol></pre><p>These three macros are used to change a memory buffer size or to free a
piece of memory no longer needed.  The arguments to <code class="inline"><span class="w">Renew</span></code>
 and <code class="inline"><span class="w">Renewc</span></code>

match those of <code class="inline"><span class="w">New</span></code>
 and <code class="inline"><span class="w">Newc</span></code>
 with the exception of not needing the
"magic cookie" argument.</p>
<a name="Moving"></a><h2>Moving</h2>
<pre class="verbatim"><ol><li>    <span class="i">Move</span><span class="s">(</span><span class="w">source</span><span class="cm">,</span> <span class="w">dest</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Copy</span><span class="s">(</span><span class="w">source</span><span class="cm">,</span> <span class="w">dest</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">Zero</span><span class="s">(</span><span class="w">dest</span><span class="cm">,</span> <span class="w">number</span><span class="cm">,</span> <span class="w">type</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>These three macros are used to move, copy, or zero out previously allocated
memory.  The <code class="inline"><span class="w">source</span></code>
 and <code class="inline"><span class="w">dest</span></code>
 arguments point to the source and
destination starting points.  Perl will move, copy, or zero out <code class="inline"><span class="w">number</span></code>

instances of the size of the <code class="inline"><span class="w">type</span></code>
 data structure (using the <code class="inline"><span class="w">sizeof</span></code>

function).</p>
<a name="PerlIO"></a><h1>PerlIO</h1>
<p>The most recent development releases of Perl have been experimenting with
removing Perl's dependency on the "normal" standard I/O suite and allowing
other stdio implementations to be used.  This involves creating a new
abstraction layer that then calls whichever implementation of stdio Perl
was compiled with.  All XSUBs should now use the functions in the PerlIO
abstraction layer and not make any assumptions about what kind of stdio
is being used.</p>
<p>For a complete description of the PerlIO abstraction, consult <a href="perlapio.html">perlapio</a>.</p>
<a name="Compiled-code"></a><h1>Compiled code</h1>
<a name="Code-tree"></a><h2>Code tree</h2>
<p>Here we describe the internal form your code is converted to by
Perl.  Start with a simple example:</p>
<pre class="verbatim"><ol><li>  <span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span><span class="sc">;</span></li></ol></pre><p>This is converted to a tree similar to this one:</p>
<pre class="verbatim"><ol><li>             assign-to</li><li>           /           \</li><li>          +             $a</li><li>        /   \</li><li>      $b     $c</li></ol></pre><p>(but slightly more complicated).  This tree reflects the way Perl
parsed your code, but has nothing to do with the execution order.
There is an additional "thread" going through the nodes of the tree
which shows the order of execution of the nodes.  In our simplified
example above it looks like:</p>
<pre class="verbatim"><ol><li>     <span class="i">$b</span> --<span class="i">-&gt; $c</span> ---&gt; + --<span class="i">-&gt; $a</span> --<span class="i">-&gt; assign</span>-<span class="w">to</span></li></ol></pre><p>But with the actual compile tree for <code class="inline"><span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span></code>
 it is different:
some nodes <i>optimized away</i>.  As a corollary, though the actual tree
contains more nodes than our simplified example, the execution order
is the same as in our example.</p>
<a name="Examining-the-tree"></a><h2>Examining the tree</h2>
<p>If you have your perl compiled for debugging (usually done with
<code class="inline">-<span class="w">DDEBUGGING</span></code>
 on the <code class="inline"><span class="w">Configure</span></code>
 command line), you may examine the
compiled tree by specifying <code class="inline">-<span class="w">Dx</span></code>
 on the Perl command line.  The
output takes several lines per node, and for <code class="inline"><span class="i">$b</span>+<span class="i">$c</span></code>
 it looks like
this:</p>
<pre class="verbatim"><ol><li>    5           TYPE = add  ===&gt; 6</li><li>                TARG = 1</li><li>                FLAGS = (SCALAR,KIDS)</li><li>                {</li><li>                    TYPE = null  ===&gt; (4)</li><li>                      (was rv2sv)</li><li>                    FLAGS = (SCALAR,KIDS)</li><li>                    {</li><li>    3                   TYPE = gvsv  ===&gt; 4</li><li>                        FLAGS = (SCALAR)</li><li>                        GV = main::b</li><li>                    }</li><li>                }</li><li>                {</li><li>                    TYPE = null  ===&gt; (5)</li><li>                      (was rv2sv)</li><li>                    FLAGS = (SCALAR,KIDS)</li><li>                    {</li><li>    4                   TYPE = gvsv  ===&gt; 5</li><li>                        FLAGS = (SCALAR)</li><li>                        GV = main::c</li><li>                    }</li><li>                }</li></ol></pre><p>This tree has 5 nodes (one per <code class="inline"><span class="w">TYPE</span></code>
 specifier), only 3 of them are
not optimized away (one per number in the left column).  The immediate
children of the given node correspond to <code class="inline"><span class="s">{</span><span class="s">}</span></code>
 pairs on the same level
of indentation, thus this listing corresponds to the tree:</p>
<pre class="verbatim"><ol><li>                   add</li><li>                 /     \</li><li>               null    null</li><li>                |       |</li><li>               gvsv    gvsv</li></ol></pre><p>The execution order is indicated by <code class="inline">==<span class="cm">=&gt;</span></code>
 marks, thus it is <code class="inline">3
4 5 6</code> (node <code class="inline"><span class="n">6</span></code>
 is not included into above listing), i.e.,
<code class="inline"><span class="w">gvsv</span> <span class="w">gvsv</span> <span class="w">add</span> <span class="w">whatever</span></code>
.</p>
<p>Each of these nodes represents an op, a fundamental operation inside the
Perl core.  The code which implements each operation can be found in the
<i>pp*.c</i> files; the function which implements the op with type <code class="inline"><span class="w">gvsv</span></code>

is <code class="inline"><span class="w">pp_gvsv</span></code>
, and so on.  As the tree above shows, different ops have
different numbers of children: <code class="inline"><span class="w">add</span></code>
 is a binary operator, as one would
expect, and so has two children.  To accommodate the various different
numbers of children, there are various types of op data structure, and
they link together in different ways.</p>
<p>The simplest type of op structure is <code class="inline"><span class="w">OP</span></code>
: this has no children.  Unary
operators, <code class="inline"><span class="w">UNOP</span></code>
s, have one child, and this is pointed to by the
<code class="inline"><span class="w">op_first</span></code>
 field.  Binary operators (<code class="inline"><span class="w">BINOP</span></code>
s) have not only an
<code class="inline"><span class="w">op_first</span></code>
 field but also an <code class="inline"><span class="w">op_last</span></code>
 field.  The most complex type of
op is a <code class="inline"><span class="w">LISTOP</span></code>
, which has any number of children.  In this case, the
first child is pointed to by <code class="inline"><span class="w">op_first</span></code>
 and the last child by
<code class="inline"><span class="w">op_last</span></code>
.  The children in between can be found by iteratively
following the <code class="inline"><span class="w">OpSIBLING</span></code>
 pointer from the first child to the last (but
see below).</p>
<p>There are also some other op types: a <code class="inline"><span class="w">PMOP</span></code>
 holds a regular expression,
and has no children, and a <code class="inline"><span class="w">LOOP</span></code>
 may or may not have children.  If the
<code class="inline"><span class="w">op_children</span></code>
 field is non-zero, it behaves like a <code class="inline"><span class="w">LISTOP</span></code>
.  To
complicate matters, if a <code class="inline"><span class="w">UNOP</span></code>
 is actually a <code class="inline"><span class="w">null</span></code>
 op after
optimization (see <a href="#Compile-pass-2%3a-context-propagation">Compile pass 2: context propagation</a>) it will still
have children in accordance with its former type.</p>
<p>Finally, there is a <code class="inline"><span class="w">LOGOP</span></code>
, or logic op. Like a <code class="inline"><span class="w">LISTOP</span></code>
, this has one
or more children, but it doesn't have an <code class="inline"><span class="w">op_last</span></code>
 field: so you have to
follow <code class="inline"><span class="w">op_first</span></code>
 and then the <code class="inline"><span class="w">OpSIBLING</span></code>
 chain itself to find the
last child. Instead it has an <code class="inline"><span class="w">op_other</span></code>
 field, which is comparable to
the <code class="inline"><span class="w">op_next</span></code>
 field described below, and represents an alternate
execution path. Operators like <code class="inline">and</code>
, <code class="inline">or</code>
 and <code class="inline">?</code> are <code class="inline"><span class="w">LOGOP</span></code>
s. Note
that in general, <code class="inline"><span class="w">op_other</span></code>
 may not point to any of the direct children
of the <code class="inline"><span class="w">LOGOP</span></code>
.</p>
<p>Starting in version 5.21.2, perls built with the experimental
define <code class="inline">-<span class="w">DPERL_OP_PARENT</span></code>
 add an extra boolean flag for each op,
<code class="inline"><span class="w">op_moresib</span></code>
.  When not set, this indicates that this is the last op in an
<code class="inline"><span class="w">OpSIBLING</span></code>
 chain. This frees up the <code class="inline"><span class="w">op_sibling</span></code>
 field on the last
sibling to point back to the parent op. Under this build, that field is
also renamed <code class="inline"><span class="w">op_sibparent</span></code>
 to reflect its joint role. The macro
<code class="inline"><span class="i">OpSIBLING</span><span class="s">(</span><span class="w">o</span><span class="s">)</span></code>
 wraps this special behaviour, and always returns NULL on
the last sibling.  With this build the <code class="inline"><span class="i">op_parent</span><span class="s">(</span><span class="w">o</span><span class="s">)</span></code>
 function can be
used to find the parent of any op. Thus for forward compatibility, you
should always use the <code class="inline"><span class="i">OpSIBLING</span><span class="s">(</span><span class="w">o</span><span class="s">)</span></code>
 macro rather than accessing
<code class="inline"><span class="w">op_sibling</span></code>
 directly.</p>
<p>Another way to examine the tree is to use a compiler back-end module, such
as <a href="B/Concise.html">B::Concise</a>.</p>
<a name="Compile-pass-1%3a-check-routines"></a><h2>Compile pass 1: check routines</h2>
<p>The tree is created by the compiler while <i>yacc</i> code feeds it
the constructions it recognizes.  Since <i>yacc</i> works bottom-up, so does
the first pass of perl compilation.</p>
<p>What makes this pass interesting for perl developers is that some
optimization may be performed on this pass.  This is optimization by
so-called "check routines".  The correspondence between node names
and corresponding check routines is described in <i>opcode.pl</i> (do not
forget to run <code class="inline"><span class="w">make</span> <span class="w">regen_headers</span></code>
 if you modify this file).</p>
<p>A check routine is called when the node is fully constructed except
for the execution-order thread.  Since at this time there are no
back-links to the currently constructed node, one can do most any
operation to the top-level node, including freeing it and/or creating
new nodes above/below it.</p>
<p>The check routine returns the node which should be inserted into the
tree (if the top-level node was not modified, check routine returns
its argument).</p>
<p>By convention, check routines have names <code class="inline"><span class="w">ck_</span>*</code>
.  They are usually
called from <code class="inline"><span class="w">new</span>*<span class="w">OP</span></code>
 subroutines (or <code class="inline"><span class="w">convert</span></code>
) (which in turn are
called from <i>perly.y</i>).</p>
<a name="Compile-pass-1a%3a-constant-folding"></a><h2>Compile pass 1a: constant folding</h2>
<p>Immediately after the check routine is called the returned node is
checked for being compile-time executable.  If it is (the value is
judged to be constant) it is immediately executed, and a <i>constant</i>
node with the "return value" of the corresponding subtree is
substituted instead.  The subtree is deleted.</p>
<p>If constant folding was not performed, the execution-order thread is
created.</p>
<a name="Compile-pass-2%3a-context-propagation"></a><h2>Compile pass 2: context propagation</h2>
<p>When a context for a part of compile tree is known, it is propagated
down through the tree.  At this time the context can have 5 values
(instead of 2 for runtime context): void, boolean, scalar, list, and
lvalue.  In contrast with the pass 1 this pass is processed from top
to bottom: a node's context determines the context for its children.</p>
<p>Additional context-dependent optimizations are performed at this time.
Since at this moment the compile tree contains back-references (via
"thread" pointers), nodes cannot be free()d now.  To allow
optimized-away nodes at this stage, such nodes are null()ified instead
of free()ing (i.e. their type is changed to OP_NULL).</p>
<a name="Compile-pass-3%3a-peephole-optimization"></a><h2>Compile pass 3: peephole optimization</h2>
<p>After the compile tree for a subroutine (or for an <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> or a file)
is created, an additional pass over the code is performed.  This pass
is neither top-down or bottom-up, but in the execution order (with
additional complications for conditionals).  Optimizations performed
at this stage are subject to the same restrictions as in the pass 2.</p>
<p>Peephole optimizations are done by calling the function pointed to
by the global variable <code class="inline"><span class="w">PL_peepp</span></code>
.  By default, <code class="inline"><span class="w">PL_peepp</span></code>
 just
calls the function pointed to by the global variable <code class="inline"><span class="w">PL_rpeepp</span></code>
.
By default, that performs some basic op fixups and optimisations along
the execution-order op chain, and recursively calls <code class="inline"><span class="w">PL_rpeepp</span></code>
 for
each side chain of ops (resulting from conditionals).  Extensions may
provide additional optimisations or fixups, hooking into either the
per-subroutine or recursive stage, like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">static</span> <span class="w">peep_t</span> <span class="w">prev_peepp</span><span class="sc">;</span></li><li>    <span class="w">static</span> <span class="w">void</span> <span class="i">my_peep</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">OP</span> *<span class="w">o</span><span class="s">)</span></li><li>    <span class="s">{</span></li><li>        <span class="q">/* custom per-subroutine optimisation goes here */</span></li><li>        <span class="i">prev_peepp</span><span class="s">(</span><span class="w">aTHX_</span> <span class="w">o</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="q">/* custom per-subroutine optimisation may also go here */</span></li><li>    <span class="s">}</span></li><li>    <span class="j">BOOT:</span></li><li>        <span class="w">prev_peepp</span> = <span class="w">PL_peepp</span><span class="sc">;</span></li><li>        <span class="w">PL_peepp</span> = <span class="w">my_peep</span><span class="sc">;</span></li><li></li><li>    <span class="w">static</span> <span class="w">peep_t</span> <span class="w">prev_rpeepp</span><span class="sc">;</span></li><li>    <span class="w">static</span> <span class="w">void</span> <span class="i">my_rpeep</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">OP</span> *<span class="w">o</span><span class="s">)</span></li><li>    <span class="s">{</span></li><li>        <span class="w">OP</span> *<span class="w">orig_o</span> = <span class="w">o</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/for.html">for</a><span class="s">(</span><span class="sc">;</span> <span class="w">o</span><span class="sc">;</span> <span class="w">o</span> = <span class="w">o</span><span class="w">-&gt;op_next</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="q">/* custom per-op optimisation goes here */</span></li><li>        <span class="s">}</span></li><li>        <span class="i">prev_rpeepp</span><span class="s">(</span><span class="w">aTHX_</span> <span class="w">orig_o</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="j">BOOT:</span></li><li>        <span class="w">prev_rpeepp</span> = <span class="w">PL_rpeepp</span><span class="sc">;</span></li><li>        <span class="w">PL_rpeepp</span> = <span class="w">my_rpeep</span><span class="sc">;</span></li></ol></pre><a name="Pluggable-runops"></a><h2>Pluggable runops</h2>
<p>The compile tree is executed in a runops function.  There are two runops
functions, in <i>run.c</i> and in <i>dump.c</i>.  <code class="inline"><span class="w">Perl_runops_debug</span></code>
 is used
with DEBUGGING and <code class="inline"><span class="w">Perl_runops_standard</span></code>
 is used otherwise.  For fine
control over the execution of the compile tree it is possible to provide
your own runops function.</p>
<p>It's probably best to copy one of the existing runops functions and
change it to suit your needs.  Then, in the BOOT section of your XS
file, add the line:</p>
<pre class="verbatim"><ol><li>  <span class="w">PL_runops</span> = <span class="w">my_runops</span><span class="sc">;</span></li></ol></pre><p>This function should be as efficient as possible to keep your programs
running as fast as possible.</p>
<a name="Compile-time-scope-hooks"></a><h2>Compile-time scope hooks</h2>
<p>As of perl 5.14 it is possible to hook into the compile-time lexical
scope mechanism using <code class="inline"><span class="w">Perl_blockhook_register</span></code>
.  This is used like
this:</p>
<pre class="verbatim"><ol><li>    <span class="w">STATIC</span> <span class="w">void</span> <span class="i">my_start_hook</span><span class="s">(</span><span class="w">pTHX_</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">full</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">STATIC</span> <span class="w">BHK</span> <span class="w">my_hooks</span><span class="sc">;</span></li><li></li><li>    <span class="j">BOOT:</span></li><li>        <span class="i">BhkENTRY_set</span><span class="s">(</span><span class="i">&amp;my_hooks</span><span class="cm">,</span> <span class="w">bhk_start</span><span class="cm">,</span> <span class="w">my_start_hook</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="i">Perl_blockhook_register</span><span class="s">(</span><span class="w">aTHX_</span> <span class="i">&amp;my_hooks</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This will arrange to have <code class="inline"><span class="w">my_start_hook</span></code>
 called at the start of
compiling every lexical scope.  The available hooks are:</p>
<ul>
<li><a name="void-bhk_start(pTHX_-int-full)"></a><b><code class="inline"><span class="w">void</span> <span class="i">bhk_start</span><span class="s">(</span><span class="w">pTHX_</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">full</span><span class="s">)</span></code>
</b>
<p>This is called just after starting a new lexical scope.  Note that Perl
code like</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$x</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>creates two scopes: the first starts at the <code class="inline">(</code> and has <code class="inline"><span class="w">full</span> == <span class="n">1</span></code>
,
the second starts at the <code class="inline">{</code> and has <code class="inline"><span class="w">full</span> == <span class="n">0</span></code>
.  Both end at the
<code class="inline">}</code>, so calls to <code class="inline"><span class="w">start</span></code>
 and <code class="inline"><span class="w">pre</span>/<span class="w">post_end</span></code>
 will match.  Anything
pushed onto the save stack by this hook will be popped just before the
scope ends (between the <code class="inline"><span class="w">pre_</span></code>
 and <code class="inline"><span class="w">post_end</span></code>
 hooks, in fact).</p>
</li>
<li><a name="void-bhk_pre_end(pTHX_-OP-**o)"></a><b><code class="inline"><span class="w">void</span> <span class="i">bhk_pre_end</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">OP</span> **<span class="w">o</span><span class="s">)</span></code>
</b>
<p>This is called at the end of a lexical scope, just before unwinding the
stack.  <i>o</i> is the root of the optree representing the scope; it is a
double pointer so you can replace the OP if you need to.</p>
</li>
<li><a name="void-bhk_post_end(pTHX_-OP-**o)"></a><b><code class="inline"><span class="w">void</span> <span class="i">bhk_post_end</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">OP</span> **<span class="w">o</span><span class="s">)</span></code>
</b>
<p>This is called at the end of a lexical scope, just after unwinding the
stack.  <i>o</i> is as above.  Note that it is possible for calls to <code class="inline"><span class="w">pre_</span></code>

and <code class="inline"><span class="w">post_end</span></code>
 to nest, if there is something on the save stack that
calls string eval.</p>
</li>
<li><a name="void-bhk_eval(pTHX_-OP-*const-o)"></a><b><code class="inline"><span class="w">void</span> <span class="i">bhk_eval</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">OP</span> *<span class="w">const</span> <span class="w">o</span><span class="s">)</span></code>
</b>
<p>This is called just before starting to compile an <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="w">STRING</span></code>
, <code class="inline"><a class="l_k" href="functions/do.html">do</a>
<span class="w">FILE</span></code>
, <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="functions/use.html">use</a></code>, after the eval has been set up.  <i>o</i> is the
OP that requested the eval, and will normally be an <code class="inline"><span class="w">OP_ENTEREVAL</span></code>
,
<code class="inline"><span class="w">OP_DOFILE</span></code>
 or <code class="inline"><span class="w">OP_REQUIRE</span></code>
.</p>
</li>
</ul>
<p>Once you have your hook functions, you need a <code class="inline"><span class="w">BHK</span></code>
 structure to put
them in.  It's best to allocate it statically, since there is no way to
free it once it's registered.  The function pointers should be inserted
into this structure using the <code class="inline"><span class="w">BhkENTRY_set</span></code>
 macro, which will also set
flags indicating which entries are valid.  If you do need to allocate
your <code class="inline"><span class="w">BHK</span></code>
 dynamically for some reason, be sure to zero it before you
start.</p>
<p>Once registered, there is no mechanism to switch these hooks off, so if
that is necessary you will need to do this yourself.  An entry in <code class="inline"><span class="i">%^H</span></code>

is probably the best way, so the effect is lexically scoped; however it
is also possible to use the <code class="inline"><span class="w">BhkDISABLE</span></code>
 and <code class="inline"><span class="w">BhkENABLE</span></code>
 macros to
temporarily switch entries on and off.  You should also be aware that
generally speaking at least one scope will have opened before your
extension is loaded, so you will see some <code class="inline"><span class="w">pre</span>/<span class="w">post_end</span></code>
 pairs that
didn't have a matching <code class="inline"><span class="w">start</span></code>
.</p>
<a name="Examining-internal-data-structures-with-the-dump-functions"></a><h1>Examining internal data structures with the <code class="inline"><a class="l_k" href="functions/dump.html">dump</a></code> functions</h1>
<p>To aid debugging, the source file <i>dump.c</i> contains a number of
functions which produce formatted output of internal data structures.</p>
<p>The most commonly used of these functions is <code class="inline"><span class="w">Perl_sv_dump</span></code>
; it's used
for dumping SVs, AVs, HVs, and CVs.  The <code class="inline"><span class="w">Devel::Peek</span></code>
 module calls
<code class="inline"><span class="w">sv_dump</span></code>
 to produce debugging output from Perl-space, so users of that
module should already be familiar with its format.</p>
<p><code class="inline"><span class="w">Perl_op_dump</span></code>
 can be used to dump an <code class="inline"><span class="w">OP</span></code>
 structure or any of its
derivatives, and produces output similar to <code class="inline"><span class="w">perl</span> -<span class="w">Dx</span></code>
; in fact,
<code class="inline"><span class="w">Perl_dump_eval</span></code>
 will dump the main root of the code being evaluated,
exactly like <code class="inline">-<span class="w">Dx</span></code>
.</p>
<p>Other useful functions are <code class="inline"><span class="w">Perl_dump_sub</span></code>
, which turns a <code class="inline"><span class="w">GV</span></code>
 into an
op tree, <code class="inline"><span class="w">Perl_dump_packsubs</span></code>
 which calls <code class="inline"><span class="w">Perl_dump_sub</span></code>
 on all the
subroutines in a package like so: (Thankfully, these are all xsubs, so
there is no op tree)</p>
<pre class="verbatim"><ol><li>    (gdb) print Perl_dump_packsubs(PL_defstash)</li><li></li><li>    SUB attributes::bootstrap = (xsub 0x811fedc 0)</li><li></li><li>    SUB UNIVERSAL::can = (xsub 0x811f50c 0)</li><li></li><li>    SUB UNIVERSAL::isa = (xsub 0x811f304 0)</li><li></li><li>    SUB UNIVERSAL::VERSION = (xsub 0x811f7ac 0)</li><li></li><li>    SUB DynaLoader::boot_DynaLoader = (xsub 0x805b188 0)</li></ol></pre><p>and <code class="inline"><span class="w">Perl_dump_all</span></code>
, which dumps all the subroutines in the stash and
the op tree of the main root.</p>
<a name="How-multiple-interpreters-and-concurrency-are-supported"></a><h1>How multiple interpreters and concurrency are supported</h1>
<a name="Background-and-PERL_IMPLICIT_CONTEXT"></a><h2>Background and PERL_IMPLICIT_CONTEXT</h2>
<p>The Perl interpreter can be regarded as a closed box: it has an API
for feeding it code or otherwise making it do things, but it also has
functions for its own use.  This smells a lot like an object, and
there are ways for you to build Perl so that you can have multiple
interpreters, with one interpreter represented either as a C structure,
or inside a thread-specific structure.  These structures contain all
the context, the state of that interpreter.</p>
<p>One macro controls the major Perl build flavor: MULTIPLICITY.  The
MULTIPLICITY build has a C structure that packages all the interpreter
state.  With multiplicity-enabled perls, PERL_IMPLICIT_CONTEXT is also
normally defined, and enables the support for passing in a "hidden" first
argument that represents all three data structures.  MULTIPLICITY makes
multi-threaded perls possible (with the ithreads threading model, related
to the macro USE_ITHREADS.)</p>
<p>Two other "encapsulation" macros are the PERL_GLOBAL_STRUCT and
PERL_GLOBAL_STRUCT_PRIVATE (the latter turns on the former, and the
former turns on MULTIPLICITY.)  The PERL_GLOBAL_STRUCT causes all the
internal variables of Perl to be wrapped inside a single global struct,
struct perl_vars, accessible as (globals) &amp;PL_Vars or PL_VarsPtr or
the function  Perl_GetVars().  The PERL_GLOBAL_STRUCT_PRIVATE goes
one step further, there is still a single struct (allocated in main()
either from heap or from stack) but there are no global data symbols
pointing to it.  In either case the global struct should be initialized
as the very first thing in main() using Perl_init_global_struct() and
correspondingly tear it down after perl_free() using Perl_free_global_struct(),
please see <i>miniperlmain.c</i> for usage details.  You may also need
to use <code class="inline"><span class="w">dVAR</span></code>
 in your coding to "declare the global variables"
when you are using them.  dTHX does this for you automatically.</p>
<p>To see whether you have non-const data you can use a BSD (or GNU)
compatible <code class="inline"><span class="w">nm</span></code>
:</p>
<pre class="verbatim"><ol><li>  <span class="w">nm</span> <span class="w">libperl</span>.<span class="w">a</span> | <a class="l_k" href="functions/grep.html">grep</a> -<span class="w">v</span> <span class="q">' [TURtr] '</span></li></ol></pre><p>If this displays any <code class="inline"><span class="w">D</span></code>
 or <code class="inline"><span class="w">d</span></code>
 symbols (or possibly <code class="inline"><span class="w">C</span></code>
 or <code class="inline"><span class="w">c</span></code>
),
you have non-const data.  The symbols the <code class="inline"><a class="l_k" href="functions/grep.html">grep</a></code> removed are as follows:
<code class="inline"><span class="w">Tt</span></code>
 are <i>text</i>, or code, the <code class="inline"><span class="w">Rr</span></code>
 are <i>read-only</i> (const) data,
and the <code class="inline"><span class="w">U</span></code>
 is &lt;undefined&gt;, external symbols referred to.</p>
<p>The test <i>t/porting/libperl.t</i> does this kind of symbol sanity
checking on <code class="inline"><span class="w">libperl</span>.<span class="w">a</span></code>
.</p>
<p>For backward compatibility reasons defining just PERL_GLOBAL_STRUCT
doesn't actually hide all symbols inside a big global struct: some
PerlIO_xxx vtables are left visible.  The PERL_GLOBAL_STRUCT_PRIVATE
then hides everything (see how the PERLIO_FUNCS_DECL is used).</p>
<p>All this obviously requires a way for the Perl internal functions to be
either subroutines taking some kind of structure as the first
argument, or subroutines taking nothing as the first argument.  To
enable these two very different ways of building the interpreter,
the Perl source (as it does in so many other situations) makes heavy
use of macros and subroutine naming conventions.</p>
<p>First problem: deciding which functions will be public API functions and
which will be private.  All functions whose names begin <code class="inline"><span class="w">S_</span></code>
 are private
(think "S" for "secret" or "static").  All other functions begin with
"Perl_", but just because a function begins with "Perl_" does not mean it is
part of the API.  (See <a href="#Internal-Functions">Internal Functions</a>.)  The easiest way to be <b>sure</b> a
function is part of the API is to find its entry in <a href="perlapi.html">perlapi</a>.
If it exists in <a href="perlapi.html">perlapi</a>, it's part of the API.  If it doesn't, and you
think it should be (i.e., you need it for your extension), send mail via
<a href="perlbug.html">perlbug</a> explaining why you think it should be.</p>
<p>Second problem: there must be a syntax so that the same subroutine
declarations and calls can pass a structure as their first argument,
or pass nothing.  To solve this, the subroutines are named and
declared in a particular way.  Here's a typical start of a static
function used within the Perl guts:</p>
<pre class="verbatim"><ol><li>  STATIC void</li><li>  S_incline(pTHX_ char *s)</li></ol></pre><p>STATIC becomes "static" in C, and may be #define'd to nothing in some
configurations in the future.</p>
<p>A public function (i.e. part of the internal API, but not necessarily
sanctioned for use in extensions) begins like this:</p>
<pre class="verbatim"><ol><li>  <span class="w">void</span></li><li>  <span class="i">Perl_sv_setiv</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">SV</span>* <span class="w">dsv</span><span class="cm">,</span> <span class="w">IV</span> <span class="w">num</span><span class="s">)</span></li></ol></pre><p><code class="inline"><span class="w">pTHX_</span></code>
 is one of a number of macros (in <i>perl.h</i>) that hide the
details of the interpreter's context.  THX stands for "thread", "this",
or "thingy", as the case may be.  (And no, George Lucas is not involved. :-)
The first character could be 'p' for a <b>p</b>rototype, 'a' for <b>a</b>rgument,
or 'd' for <b>d</b>eclaration, so we have <code class="inline"><span class="w">pTHX</span></code>
, <code class="inline"><span class="w">aTHX</span></code>
 and <code class="inline"><span class="w">dTHX</span></code>
, and
their variants.</p>
<p>When Perl is built without options that set PERL_IMPLICIT_CONTEXT, there is no
first argument containing the interpreter's context.  The trailing underscore
in the pTHX_ macro indicates that the macro expansion needs a comma
after the context argument because other arguments follow it.  If
PERL_IMPLICIT_CONTEXT is not defined, pTHX_ will be ignored, and the
subroutine is not prototyped to take the extra argument.  The form of the
macro without the trailing underscore is used when there are no additional
explicit arguments.</p>
<p>When a core function calls another, it must pass the context.  This
is normally hidden via macros.  Consider <code class="inline"><span class="w">sv_setiv</span></code>
.  It expands into
something like this:</p>
<pre class="verbatim"><ol><li>    <span class="c">#ifdef PERL_IMPLICIT_CONTEXT</span></li><li>      <span class="c">#define sv_setiv(a,b)      Perl_sv_setiv(aTHX_ a, b)</span></li><li>      <span class="q">/* can&#39;t do this for vararg functions, see below */</span></li><li>    <span class="c">#else</span></li><li>      <span class="c">#define sv_setiv           Perl_sv_setiv</span></li><li>    <span class="c">#endif</span></li></ol></pre><p>This works well, and means that XS authors can gleefully write:</p>
<pre class="verbatim"><ol><li>    <span class="i">sv_setiv</span><span class="s">(</span><span class="w">foo</span><span class="cm">,</span> <span class="w">bar</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>and still have it work under all the modes Perl could have been
compiled with.</p>
<p>This doesn't work so cleanly for varargs functions, though, as macros
imply that the number of arguments is known in advance.  Instead we
either need to spell them out fully, passing <code class="inline"><span class="w">aTHX_</span></code>
 as the first
argument (the Perl core tends to do this with functions like
Perl_warner), or use a context-free version.</p>
<p>The context-free version of Perl_warner is called
Perl_warner_nocontext, and does not take the extra argument.  Instead
it does dTHX; to get the context from thread-local storage.  We
<code class="inline"><span class="c">#define warner Perl_warner_nocontext</span></code>
 so that extensions get source
compatibility at the expense of performance.  (Passing an arg is
cheaper than grabbing it from thread-local storage.)</p>
<p>You can ignore [pad]THXx when browsing the Perl headers/sources.
Those are strictly for use within the core.  Extensions and embedders
need only be aware of [pad]THX.</p>
<a name="So-what-happened-to-dTHR%3f"></a><h2>So what happened to dTHR?</h2>
<p><code class="inline"><span class="w">dTHR</span></code>
 was introduced in perl 5.005 to support the older thread model.
The older thread model now uses the <code class="inline"><span class="w">THX</span></code>
 mechanism to pass context
pointers around, so <code class="inline"><span class="w">dTHR</span></code>
 is not useful any more.  Perl 5.6.0 and
later still have it for backward source compatibility, but it is defined
to be a no-op.</p>
<a name="How-do-I-use-all-this-in-extensions%3f"></a><h2>How do I use all this in extensions?</h2>
<p>When Perl is built with PERL_IMPLICIT_CONTEXT, extensions that call
any functions in the Perl API will need to pass the initial context
argument somehow.  The kicker is that you will need to write it in
such a way that the extension still compiles when Perl hasn't been
built with PERL_IMPLICIT_CONTEXT enabled.</p>
<p>There are three ways to do this.  First, the easy but inefficient way,
which is also the default, in order to maintain source compatibility
with extensions: whenever <i>XSUB.h</i> is #included, it redefines the aTHX
and aTHX_ macros to call a function that will return the context.
Thus, something like:</p>
<pre class="verbatim"><ol><li>        <span class="i">sv_setiv</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">num</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>in your extension will translate to this when PERL_IMPLICIT_CONTEXT is
in effect:</p>
<pre class="verbatim"><ol><li>        <span class="i">Perl_sv_setiv</span><span class="s">(</span><span class="i">Perl_get_context</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span> <span class="w">sv</span><span class="cm">,</span> <span class="w">num</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>or to this otherwise:</p>
<pre class="verbatim"><ol><li>        <span class="i">Perl_sv_setiv</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">num</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You don't have to do anything new in your extension to get this; since
the Perl library provides Perl_get_context(), it will all just
work.</p>
<p>The second, more efficient way is to use the following template for
your Foo.xs:</p>
<pre class="verbatim"><ol><li>        #define PERL_NO_GET_CONTEXT     /* we want efficiency */</li><li>        #include "EXTERN.h"</li><li>        #include "perl.h"</li><li>        #include "XSUB.h"</li><li></li><li>        STATIC void my_private_function(int arg1, int arg2);</li><li></li><li>        STATIC void</li><li>        my_private_function(int arg1, int arg2)</li><li>        {</li><li>            dTHX;       /* fetch context */</li><li>            ... call many Perl API functions ...</li><li>        }</li><li></li><li>        [... etc ...]</li><li></li><li>        MODULE = Foo            PACKAGE = Foo</li><li></li><li>        /* typical XSUB */</li><li></li><li>        void</li><li>        my_xsub(arg)</li><li>                int arg</li><li>            CODE:</li><li>                my_private_function(arg, 10);</li></ol></pre><p>Note that the only two changes from the normal way of writing an
extension is the addition of a <code class="inline"><span class="c">#define PERL_NO_GET_CONTEXT</span></code>
 before
including the Perl headers, followed by a <code class="inline"><span class="w">dTHX</span><span class="sc">;</span></code>
 declaration at
the start of every function that will call the Perl API.  (You'll
know which functions need this, because the C compiler will complain
that there's an undeclared identifier in those functions.)  No changes
are needed for the XSUBs themselves, because the XS() macro is
correctly defined to pass in the implicit context if needed.</p>
<p>The third, even more efficient way is to ape how it is done within
the Perl guts:</p>
<pre class="verbatim"><ol><li>        #define PERL_NO_GET_CONTEXT     /* we want efficiency */</li><li>        #include "EXTERN.h"</li><li>        #include "perl.h"</li><li>        #include "XSUB.h"</li><li></li><li>        /* pTHX_ only needed for functions that call Perl API */</li><li>        STATIC void my_private_function(pTHX_ int arg1, int arg2);</li><li></li><li>        STATIC void</li><li>        my_private_function(pTHX_ int arg1, int arg2)</li><li>        {</li><li>            /* dTHX; not needed here, because THX is an argument */</li><li>            ... call Perl API functions ...</li><li>        }</li><li></li><li>        [... etc ...]</li><li></li><li>        MODULE = Foo            PACKAGE = Foo</li><li></li><li>        /* typical XSUB */</li><li></li><li>        void</li><li>        my_xsub(arg)</li><li>                int arg</li><li>            CODE:</li><li>                my_private_function(aTHX_ arg, 10);</li></ol></pre><p>This implementation never has to fetch the context using a function
call, since it is always passed as an extra argument.  Depending on
your needs for simplicity or efficiency, you may mix the previous
two approaches freely.</p>
<p>Never add a comma after <code class="inline"><span class="w">pTHX</span></code>
 yourself--always use the form of the
macro with the underscore for functions that take explicit arguments,
or the form without the argument for functions with no explicit arguments.</p>
<p>If one is compiling Perl with the <code class="inline">-<span class="w">DPERL_GLOBAL_STRUCT</span></code>
 the <code class="inline"><span class="w">dVAR</span></code>

definition is needed if the Perl global variables (see <i>perlvars.h</i>
or <i>globvar.sym</i>) are accessed in the function and <code class="inline"><span class="w">dTHX</span></code>
 is not
used (the <code class="inline"><span class="w">dTHX</span></code>
 includes the <code class="inline"><span class="w">dVAR</span></code>
 if necessary).  One notices
the need for <code class="inline"><span class="w">dVAR</span></code>
 only with the said compile-time define, because
otherwise the Perl global variables are visible as-is.</p>
<a name="Should-I-do-anything-special-if-I-call-perl-from-multiple-threads%3f"></a><h2>Should I do anything special if I call perl from multiple threads?</h2>
<p>If you create interpreters in one thread and then proceed to call them in
another, you need to make sure perl's own Thread Local Storage (TLS) slot is
initialized correctly in each of those threads.</p>
<p>The <code class="inline"><span class="w">perl_alloc</span></code>
 and <code class="inline"><span class="w">perl_clone</span></code>
 API functions will automatically set
the TLS slot to the interpreter they created, so that there is no need to do
anything special if the interpreter is always accessed in the same thread that
created it, and that thread did not create or call any other interpreters
afterwards.  If that is not the case, you have to set the TLS slot of the
thread before calling any functions in the Perl API on that particular
interpreter.  This is done by calling the <code class="inline"><span class="w">PERL_SET_CONTEXT</span></code>
 macro in that
thread as the first thing you do:</p>
<pre class="verbatim"><ol><li>	/* do this before doing anything else with some_perl */</li><li>	PERL_SET_CONTEXT(some_perl);</li><li></li><li>	... other Perl API calls on some_perl go here ...</li></ol></pre><a name="Future-Plans-and-PERL_IMPLICIT_SYS"></a><h2>Future Plans and PERL_IMPLICIT_SYS</h2>
<p>Just as PERL_IMPLICIT_CONTEXT provides a way to bundle up everything
that the interpreter knows about itself and pass it around, so too are
there plans to allow the interpreter to bundle up everything it knows
about the environment it's running on.  This is enabled with the
PERL_IMPLICIT_SYS macro.  Currently it only works with USE_ITHREADS on
Windows.</p>
<p>This allows the ability to provide an extra pointer (called the "host"
environment) for all the system calls.  This makes it possible for
all the system stuff to maintain their own state, broken down into
seven C structures.  These are thin wrappers around the usual system
calls (see <i>win32/perllib.c</i>) for the default perl executable, but for a
more ambitious host (like the one that would do fork() emulation) all
the extra work needed to pretend that different interpreters are
actually different "processes", would be done here.</p>
<p>The Perl engine/interpreter and the host are orthogonal entities.
There could be one or more interpreters in a process, and one or
more "hosts", with free association between them.</p>
<a name="Internal-Functions"></a><h1>Internal Functions</h1>
<p>All of Perl's internal functions which will be exposed to the outside
world are prefixed by <code class="inline"><span class="w">Perl_</span></code>
 so that they will not conflict with XS
functions or functions used in a program in which Perl is embedded.
Similarly, all global variables begin with <code class="inline"><span class="w">PL_</span></code>
.  (By convention,
static functions start with <code class="inline"><span class="w">S_</span></code>
.)</p>
<p>Inside the Perl core (<code class="inline"><span class="w">PERL_CORE</span></code>
 defined), you can get at the functions
either with or without the <code class="inline"><span class="w">Perl_</span></code>
 prefix, thanks to a bunch of defines
that live in <i>embed.h</i>.  Note that extension code should <i>not</i> set
<code class="inline"><span class="w">PERL_CORE</span></code>
; this exposes the full perl internals, and is likely to cause
breakage of the XS in each new perl release.</p>
<p>The file <i>embed.h</i> is generated automatically from
<i>embed.pl</i> and <i>embed.fnc</i>.  <i>embed.pl</i> also creates the prototyping
header files for the internal functions, generates the documentation
and a lot of other bits and pieces.  It's important that when you add
a new function to the core or change an existing one, you change the
data in the table in <i>embed.fnc</i> as well.  Here's a sample entry from
that table:</p>
<pre class="verbatim"><ol><li>    Apd |SV**   |av_fetch   |AV* ar|I32 key|I32 lval</li></ol></pre><p>The second column is the return type, the third column the name.  Columns
after that are the arguments.  The first column is a set of flags:</p>
<ul>
<li><a name="A"></a><b>A</b>
<p>This function is a part of the public
API.  All such functions should also
have 'd', very few do not.</p>
</li>
<li><a name="p"></a><b>p</b>
<p>This function has a <code class="inline"><span class="w">Perl_</span></code>
 prefix; i.e. it is defined as
<code class="inline"><span class="w">Perl_av_fetch</span></code>
.</p>
</li>
<li><a name="d"></a><b>d</b>
<p>This function has documentation using the <code class="inline"><span class="w">apidoc</span></code>
 feature which we'll
look at in a second.  Some functions have 'd' but not 'A'; docs are good.</p>
</li>
</ul>
<p>Other available flags are:</p>
<ul>
<li><a name="s"></a><b>s</b>
<p>This is a static function and is defined as <code class="inline"><span class="w">STATIC</span> <span class="w">S_whatever</span></code>
, and
usually called within the sources as <code class="inline"><span class="i">whatever</span><span class="s">(</span>...<span class="s">)</span></code>
.</p>
</li>
<li><a name="n"></a><b>n</b>
<p>This does not need an interpreter context, so the definition has no
<code class="inline"><span class="w">pTHX</span></code>
, and it follows that callers don't use <code class="inline"><span class="w">aTHX</span></code>
.  (See
<a href="#Background-and-PERL_IMPLICIT_CONTEXT">Background and PERL_IMPLICIT_CONTEXT</a>.)</p>
</li>
<li><a name="r"></a><b>r</b>
<p>This function never returns; <code class="inline"><span class="w">croak</span></code>
, <code class="inline"><a class="l_k" href="functions/exit.html">exit</a></code> and friends.</p>
</li>
<li><a name="f"></a><b>f</b>
<p>This function takes a variable number of arguments, <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code> style.
The argument list should end with <code class="inline">...</code>
, like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">Afprd</span>   |<span class="w">void</span>   |<span class="w">croak</span>          |<span class="w">const</span> <span class="w">char</span>* <span class="w">pat</span>|...</li></ol></pre></li>
<li><a name="M"></a><b>M</b>
<p>This function is part of the experimental development API, and may change
or disappear without notice.</p>
</li>
<li><a name="o"></a><b>o</b>
<p>This function should not have a compatibility macro to define, say,
<code class="inline"><span class="w">Perl_parse</span></code>
 to <code class="inline"><span class="w">parse</span></code>
.  It must be called as <code class="inline"><span class="w">Perl_parse</span></code>
.</p>
</li>
<li><a name="x"></a><b>x</b>
<p>This function isn't exported out of the Perl core.</p>
</li>
<li><a name="m"></a><b>m</b>
<p>This is implemented as a macro.</p>
</li>
<li><a name="X"></a><b>X</b>
<p>This function is explicitly exported.</p>
</li>
<li><a name="E"></a><b>E</b>
<p>This function is visible to extensions included in the Perl core.</p>
</li>
<li><a name="b"></a><b>b</b>
<p>Binary backward compatibility; this function is a macro but also has
a <code class="inline"><span class="w">Perl_</span></code>
 implementation (which is exported).</p>
</li>
<li><a name="others"></a><b>others</b>
<p>See the comments at the top of <code class="inline"><span class="w">embed</span>.<span class="w">fnc</span></code>
 for others.</p>
</li>
</ul>
<p>If you edit <i>embed.pl</i> or <i>embed.fnc</i>, you will need to run
<code class="inline"><span class="w">make</span> <span class="w">regen_headers</span></code>
 to force a rebuild of <i>embed.h</i> and other
auto-generated files.</p>
<a name="Formatted-Printing-of-IVs%2c-UVs%2c-and-NVs"></a><h2>Formatted Printing of IVs, UVs, and NVs</h2>
<p>If you are printing IVs, UVs, or NVS instead of the stdio(3) style
formatting codes like <code class="inline"><span class="i">%d</span></code>
, <code class="inline"><span class="i">%ld</span></code>
, <code class="inline"><span class="i">%f</span></code>
, you should use the
following macros for portability</p>
<pre class="verbatim"><ol><li>        <span class="w">IVdf</span>            <span class="w">IV</span> <span class="w">in</span> <span class="w">decimal</span></li><li>        <span class="w">UVuf</span>            <span class="w">UV</span> <span class="w">in</span> <span class="w">decimal</span></li><li>        <span class="w">UVof</span>            <span class="w">UV</span> <span class="w">in</span> <span class="w">octal</span></li><li>        <span class="w">UVxf</span>            <span class="w">UV</span> <span class="w">in</span> <span class="w">hexadecimal</span></li><li>        <span class="w">NVef</span>            <span class="w">NV</span> <span class="i">%e</span>-<span class="w">like</span></li><li>        <span class="w">NVff</span>            <span class="w">NV</span> <span class="i">%f</span>-<span class="w">like</span></li><li>        <span class="w">NVgf</span>            <span class="w">NV</span> <span class="i">%g</span>-<span class="w">like</span></li></ol></pre><p>These will take care of 64-bit integers and long doubles.
For example:</p>
<pre class="verbatim"><ol><li>        printf("IV is %"IVdf"\n", iv);</li></ol></pre><p>The IVdf will expand to whatever is the correct format for the IVs.</p>
<p>Note that there are different "long doubles": Perl will use
whatever the compiler has.</p>
<p>If you are printing addresses of pointers, use UVxf combined
with PTR2UV(), do not use %lx or %p.</p>
<a name="Pointer-To-Integer-and-Integer-To-Pointer"></a><h2>Pointer-To-Integer and Integer-To-Pointer</h2>
<p>Because pointer size does not necessarily equal integer size,
use the follow macros to do it right.</p>
<pre class="verbatim"><ol><li>        PTR2UV(pointer)</li><li>        PTR2IV(pointer)</li><li>        PTR2NV(pointer)</li><li>        INT2PTR(pointertotype, integer)</li></ol></pre><p>For example:</p>
<pre class="verbatim"><ol><li>        <span class="w">IV</span>  <span class="w">iv</span> = ...<span class="sc">;</span></li><li>        <span class="w">SV</span> *<span class="w">sv</span> = <span class="i">INT2PTR</span><span class="s">(</span><span class="w">SV</span>*<span class="cm">,</span> <span class="w">iv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>and</p>
<pre class="verbatim"><ol><li>        <span class="w">AV</span> *<span class="w">av</span> = ...<span class="sc">;</span></li><li>        <span class="w">UV</span>  <span class="w">uv</span> = <span class="i">PTR2UV</span><span class="s">(</span><span class="w">av</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Exception-Handling"></a><h2>Exception Handling</h2>
<p>There are a couple of macros to do very basic exception handling in XS
modules.  You have to define <code class="inline"><span class="w">NO_XSLOCKS</span></code>
 before including <i>XSUB.h</i> to
be able to use these macros:</p>
<pre class="verbatim"><ol><li>        <span class="c">#define NO_XSLOCKS</span></li><li>        <span class="c">#include &quot;XSUB.h&quot;</span></li></ol></pre><p>You can use these macros if you call code that may croak, but you need
to do some cleanup before giving control back to Perl.  For example:</p>
<pre class="verbatim"><ol><li>        dXCPT;    /* set up necessary variables */</li><li></li><li>        XCPT_TRY_START {</li><li>          code_that_may_croak();</li><li>        } XCPT_TRY_END</li><li></li><li>        XCPT_CATCH</li><li>        {</li><li>          /* do cleanup here */</li><li>          XCPT_RETHROW;</li><li>        }</li></ol></pre><p>Note that you always have to rethrow an exception that has been
caught.  Using these macros, it is not possible to just catch the
exception and ignore it.  If you have to ignore the exception, you
have to use the <code class="inline"><span class="w">call_</span>*</code>
 function.</p>
<p>The advantage of using the above macros is that you don't have
to setup an extra function for <code class="inline"><span class="w">call_</span>*</code>
, and that using these
macros is faster than using <code class="inline"><span class="w">call_</span>*</code>
.</p>
<a name="Source-Documentation"></a><h2>Source Documentation</h2>
<p>There's an effort going on to document the internal functions and
automatically produce reference manuals from them -- <a href="perlapi.html">perlapi</a> is one
such manual which details all the functions which are available to XS
writers.  <a href="perlintern.html">perlintern</a> is the autogenerated manual for the functions
which are not part of the API and are supposedly for internal use only.</p>
<p>Source documentation is created by putting POD comments into the C
source, like this:</p>
<pre class="verbatim"><ol><li> <span class="q">/*</span></li><li> <span class="q"> =for apidoc sv_setiv</span></li><li></li><li> <span class="q"> Copies an integer into the given SV.  Does not handle &#39;set&#39; magic.  See</span></li><li> <span class="q"> C&lt;sv_setiv_mg&gt;.</span></li><li></li><li> <span class="q"> =cut</span></li><li> <span class="q"> */</span></li></ol></pre><p>Please try and supply some documentation if you add functions to the
Perl core.</p>
<a name="Backwards-compatibility"></a><h2>Backwards compatibility</h2>
<p>The Perl API changes over time.  New functions are
added or the interfaces of existing functions are
changed.  The <code class="inline"><span class="w">Devel::PPPort</span></code>
 module tries to
provide compatibility code for some of these changes, so XS writers don't
have to code it themselves when supporting multiple versions of Perl.</p>
<p><code class="inline"><span class="w">Devel::PPPort</span></code>
 generates a C header file <i>ppport.h</i> that can also
be run as a Perl script.  To generate <i>ppport.h</i>, run:</p>
<pre class="verbatim"><ol><li>    <span class="w">perl</span> -<span class="w">MDevel::PPPort</span> -<span class="w">eDevel::PPPort::WriteFile</span></li></ol></pre><p>Besides checking existing XS code, the script can also be used to retrieve
compatibility information for various API calls using the <code class="inline">--<span class="w">api</span>-<span class="w">info</span></code>

command line switch.  For example:</p>
<pre class="verbatim"><ol><li>  % perl ppport.h --api-info=sv_magicext</li></ol></pre><p>For details, see <code class="inline"><span class="w">perldoc</span> <span class="w">ppport</span>.<span class="w">h</span></code>
.</p>
<a name="Unicode-Support"></a><h1>Unicode Support</h1>
<p>Perl 5.6.0 introduced Unicode support.  It's important for porters and XS
writers to understand this support and make sure that the code they
write does not corrupt Unicode data.</p>
<a name="What-*is*-Unicode%2c-anyway%3f"></a><h2>What <b>is</b> Unicode, anyway?</h2>
<p>In the olden, less enlightened times, we all used to use ASCII.  Most of
us did, anyway.  The big problem with ASCII is that it's American.  Well,
no, that's not actually the problem; the problem is that it's not
particularly useful for people who don't use the Roman alphabet.  What
used to happen was that particular languages would stick their own
alphabet in the upper range of the sequence, between 128 and 255.  Of
course, we then ended up with plenty of variants that weren't quite
ASCII, and the whole point of it being a standard was lost.</p>
<p>Worse still, if you've got a language like Chinese or
Japanese that has hundreds or thousands of characters, then you really
can't fit them into a mere 256, so they had to forget about ASCII
altogether, and build their own systems using pairs of numbers to refer
to one character.</p>
<p>To fix this, some people formed Unicode, Inc. and
produced a new character set containing all the characters you can
possibly think of and more.  There are several ways of representing these
characters, and the one Perl uses is called UTF-8.  UTF-8 uses
a variable number of bytes to represent a character.  You can learn more
about Unicode and Perl's Unicode model in <a href="perlunicode.html">perlunicode</a>.</p>
<p>(On EBCDIC platforms, Perl uses instead UTF-EBCDIC, which is a form of
UTF-8 adapted for EBCDIC platforms.  Below, we just talk about UTF-8.
UTF-EBCDIC is like UTF-8, but the details are different.  The macros
hide the differences from you, just remember that the particular numbers
and bit patterns presented below will differ in UTF-EBCDIC.)</p>
<a name="How-can-I-recognise-a-UTF-8-string%3f"></a><h2>How can I recognise a UTF-8 string?</h2>
<p>You can't.  This is because UTF-8 data is stored in bytes just like
non-UTF-8 data.  The Unicode character 200, (<code class="inline"><span class="n">0xC8</span></code>
 for you hex types)
capital E with a grave accent, is represented by the two bytes
<code class="inline"><span class="v">v196.172</span></code>
.  Unfortunately, the non-Unicode string <code class="inline"><a class="l_k" href="functions/chr.html">chr(196).chr(172)</a></code>
has that byte sequence as well.  So you can't tell just by looking -- this
is what makes Unicode input an interesting problem.</p>
<p>In general, you either have to know what you're dealing with, or you
have to guess.  The API function <code class="inline"><span class="w">is_utf8_string</span></code>
 can help; it'll tell
you if a string contains only valid UTF-8 characters, and the chances
of a non-UTF-8 string looking like valid UTF-8 become very small very
quickly with increasing string length.  On a character-by-character
basis, <code class="inline"><span class="w">isUTF8_CHAR</span></code>

will tell you whether the current character in a string is valid UTF-8.</p>
<a name="How-does-UTF-8-represent-Unicode-characters%3f"></a><h2>How does UTF-8 represent Unicode characters?</h2>
<p>As mentioned above, UTF-8 uses a variable number of bytes to store a
character.  Characters with values 0...127 are stored in one
byte, just like good ol' ASCII.  Character 128 is stored as
<code class="inline"><span class="v">v194.128</span></code>
; this continues up to character 191, which is
<code class="inline"><span class="v">v194.191</span></code>
.  Now we've run out of bits (191 is binary
<code class="inline"><span class="n">10111111</span></code>
) so we move on; character 192 is <code class="inline"><span class="v">v195.128</span></code>
.  And
so it goes on, moving to three bytes at character 2048.
<a href="perlunicode.html#Unicode-Encodings">Unicode Encodings in perlunicode</a> has pictures of how this works.</p>
<p>Assuming you know you're dealing with a UTF-8 string, you can find out
how long the first character in it is with the <code class="inline"><span class="w">UTF8SKIP</span></code>
 macro:</p>
<pre class="verbatim"><ol><li>    char *utf = "\305\233\340\240\201";</li><li>    I32 len;</li><li></li><li>    len = UTF8SKIP(utf); /* len is 2 here */</li><li>    utf += len;</li><li>    len = UTF8SKIP(utf); /* len is 3 here */</li></ol></pre><p>Another way to skip over characters in a UTF-8 string is to use
<code class="inline"><span class="w">utf8_hop</span></code>
, which takes a string and a number of characters to skip
over.  You're on your own about bounds checking, though, so don't use it
lightly.</p>
<p>All bytes in a multi-byte UTF-8 character will have the high bit set,
so you can test if you need to do something special with this
character like this (the <code class="inline"><span class="i">UTF8_IS_INVARIANT</span><span class="s">(</span><span class="s">)</span></code>
 is a macro that tests
whether the byte is encoded as a single byte even in UTF-8):</p>
<pre class="verbatim"><ol><li>    <span class="w">U8</span> *<span class="w">utf</span><span class="sc">;</span></li><li>    <span class="w">U8</span> *<span class="w">utf_end</span><span class="sc">;</span> <span class="q">/* 1 beyond buffer pointed to by utf */</span></li><li>    <span class="w">UV</span> <span class="w">uv</span><span class="sc">;</span>	<span class="q">/* Note: a UV, not a U8, not a char */</span></li><li>    <span class="w">STRLEN</span> <span class="w">len</span><span class="sc">;</span> <span class="q">/* length of character in bytes */</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span>!<span class="i">UTF8_IS_INVARIANT</span><span class="s">(</span><span class="i">*utf</span><span class="s">)</span><span class="s">)</span></li><li>        /<span class="i">* Must</span> <span class="w">treat</span> <span class="w">this</span> <span class="w">as</span> <span class="w">UTF</span>-<span class="n">8</span> *<span class="q">/</span></li><li>        <span class="q">        uv = utf8_to_uvchr_buf(utf, utf_end, &amp;len);</span></li><li>    <span class="q">    else</span></li><li>        <span class="q">        /</span>* <span class="w">OK</span> <span class="w">to</span> <span class="w">treat</span> <span class="w">this</span> <span class="w">character</span> <span class="w">as</span> <span class="w">a</span> <span class="w">byte</span> *<span class="q">/</span></li><li>        <span class="q">        uv = *utf;</span></li></ol></pre><p>You can also see in that example that we use <code class="inline"><span class="w">utf8_to_uvchr_buf</span></code>
 to get the
value of the character; the inverse function <code class="inline"><span class="w">uvchr_to_utf8</span></code>
 is available
for putting a UV into UTF-8:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span>!<span class="i">UVCHR_IS_INVARIANT</span><span class="s">(</span><span class="w">uv</span><span class="s">)</span><span class="s">)</span></li><li>        /<span class="i">* Must</span> <span class="w">treat</span> <span class="w">this</span> <span class="w">as</span> <span class="w">UTF8</span> *<span class="q">/</span></li><li>        <span class="q">        utf8 = uvchr_to_utf8(utf8, uv);</span></li><li>    <span class="q">    else</span></li><li>        <span class="q">        /</span>* <span class="w">OK</span> <span class="w">to</span> <span class="w">treat</span> <span class="w">this</span> <span class="w">character</span> <span class="w">as</span> <span class="w">a</span> <span class="w">byte</span> *<span class="q">/</span></li><li>        <span class="q">        *utf8++ = uv;</span></li></ol></pre><p>You <b>must</b> convert characters to UVs using the above functions if
you're ever in a situation where you have to match UTF-8 and non-UTF-8
characters.  You may not skip over UTF-8 characters in this case.  If you
do this, you'll lose the ability to match hi-bit non-UTF-8 characters;
for instance, if your UTF-8 string contains <code class="inline"><span class="v">v196.172</span></code>
, and you skip
that character, you can never match a <code class="inline"><a class="l_k" href="functions/chr.html">chr(200)</a></code> in a non-UTF-8 string.
So don't do that!</p>
<p>(Note that we don't have to test for invariant characters in the
examples above.  The functions work on any well-formed UTF-8 input.
It's just that its faster to avoid the function overhead when it's not
needed.)</p>
<a name="How-does-Perl-store-UTF-8-strings%3f"></a><h2>How does Perl store UTF-8 strings?</h2>
<p>Currently, Perl deals with UTF-8 strings and non-UTF-8 strings
slightly differently.  A flag in the SV, <code class="inline"><span class="w">SVf_UTF8</span></code>
, indicates that the
string is internally encoded as UTF-8.  Without it, the byte value is the
codepoint number and vice versa.  This flag is only meaningful if the SV
is <code class="inline"><span class="w">SvPOK</span></code>
 or immediately after stringification via <code class="inline"><span class="w">SvPV</span></code>
 or a
similar macro.  You can check and manipulate this flag with the
following macros:</p>
<pre class="verbatim"><ol><li>    SvUTF8(sv)</li><li>    SvUTF8_on(sv)</li><li>    SvUTF8_off(sv)</li></ol></pre><p>This flag has an important effect on Perl's treatment of the string: if
UTF-8 data is not properly distinguished, regular expressions,
<code class="inline"><a class="l_k" href="functions/length.html">length</a></code>, <code class="inline"><a class="l_k" href="functions/substr.html">substr</a></code> and other string handling operations will have
undesirable (wrong) results.</p>
<p>The problem comes when you have, for instance, a string that isn't
flagged as UTF-8, and contains a byte sequence that could be UTF-8 --
especially when combining non-UTF-8 and UTF-8 strings.</p>
<p>Never forget that the <code class="inline"><span class="w">SVf_UTF8</span></code>
 flag is separate from the PV value; you
need to be sure you don't accidentally knock it off while you're
manipulating SVs.  More specifically, you cannot expect to do this:</p>
<pre class="verbatim"><ol><li>    <span class="w">SV</span> *<span class="w">sv</span><span class="sc">;</span></li><li>    <span class="w">SV</span> *<span class="w">nsv</span><span class="sc">;</span></li><li>    <span class="w">STRLEN</span> <span class="w">len</span><span class="sc">;</span></li><li>    <span class="w">char</span> *<span class="w">p</span><span class="sc">;</span></li><li></li><li>    <span class="w">p</span> = <span class="i">SvPV</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">frobnicate</span><span class="s">(</span><span class="w">p</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">nsv</span> = <span class="i">newSVpvn</span><span class="s">(</span><span class="w">p</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">char</span>*</code>
 string does not tell you the whole story, and you can't
copy or reconstruct an SV just by copying the string value.  Check if the
old SV has the UTF8 flag set (<i>after</i> the <code class="inline"><span class="w">SvPV</span></code>
 call), and act
accordingly:</p>
<pre class="verbatim"><ol><li>    <span class="w">p</span> = <span class="i">SvPV</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">is_utf8</span> = <span class="i">SvUTF8</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">frobnicate</span><span class="s">(</span><span class="w">p</span><span class="cm">,</span> <span class="w">is_utf8</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">nsv</span> = <span class="i">newSVpvn</span><span class="s">(</span><span class="w">p</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">is_utf8</span><span class="s">)</span></li><li>        <span class="i">SvUTF8_on</span><span class="s">(</span><span class="w">nsv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the above, your <code class="inline"><span class="w">frobnicate</span></code>
 function has been changed to be made
aware of whether or not it's dealing with UTF-8 data, so that it can
handle the string appropriately.</p>
<p>Since just passing an SV to an XS function and copying the data of
the SV is not enough to copy the UTF8 flags, even less right is just
passing a <code class="inline"><span class="w">char</span> *</code>
 to an XS function.</p>
<p>For full generality, use the <a href="perlapi.html#DO_UTF8">DO_UTF8 in perlapi</a> macro to see if the
string in an SV is to be <i>treated</i> as UTF-8.  This takes into account
if the call to the XS function is being made from within the scope of
<a href="bytes.html">use bytes </a>.  If so, the underlying bytes that comprise the
UTF-8 string are to be exposed, rather than the character they
represent.  But this pragma should only really be used for debugging and
perhaps low-level testing at the byte level.  Hence most XS code need
not concern itself with this, but various areas of the perl core do need
to support it.</p>
<p>And this isn't the whole story.  Starting in Perl v5.12, strings that
aren't encoded in UTF-8 may also be treated as Unicode under various
conditions (see <a href="perlunicode.html#ASCII-Rules-versus-Unicode-Rules">ASCII Rules versus Unicode Rules in perlunicode</a>).
This is only really a problem for characters whose ordinals are between
128 and 255, and their behavior varies under ASCII versus Unicode rules
in ways that your code cares about (see <a href="perlunicode.html#The-%22Unicode-Bug%22">The Unicode Bug in perlunicode</a>).
There is no published API for dealing with this, as it is subject to
change, but you can look at the code for <code class="inline"><span class="w">pp_lc</span></code>
 in <i>pp.c</i> for an
example as to how it's currently done.</p>
<a name="How-do-I-convert-a-string-to-UTF-8%3f"></a><h2>How do I convert a string to UTF-8?</h2>
<p>If you're mixing UTF-8 and non-UTF-8 strings, it is necessary to upgrade
the non-UTF-8 strings to UTF-8.  If you've got an SV, the easiest way to do
this is:</p>
<pre class="verbatim"><ol><li>    <span class="i">sv_utf8_upgrade</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>However, you must not do this, for example:</p>
<pre class="verbatim"><ol><li>    if (!SvUTF8(left))</li><li>        sv_utf8_upgrade(left);</li></ol></pre><p>If you do this in a binary operator, you will actually change one of the
strings that came into the operator, and, while it shouldn't be noticeable
by the end user, it can cause problems in deficient code.</p>
<p>Instead, <code class="inline"><span class="w">bytes_to_utf8</span></code>
 will give you a UTF-8-encoded <b>copy</b> of its
string argument.  This is useful for having the data available for
comparisons and so on, without harming the original SV.  There's also
<code class="inline"><span class="w">utf8_to_bytes</span></code>
 to go the other way, but naturally, this will fail if
the string contains any characters above 255 that can't be represented
in a single byte.</p>
<a name="How-do-I-compare-strings%3f"></a><h2>How do I compare strings?</h2>
<p><a href="perlapi.html#sv_cmp">sv_cmp in perlapi</a> and <a href="perlapi.html#sv_cmp_flags">sv_cmp_flags in perlapi</a> do a lexigraphic
comparison of two SV's, and handle UTF-8ness properly.  Note, however,
that Unicode specifies a much fancier mechanism for collation, available
via the <a href="Unicode/Collate.html">Unicode::Collate</a> module.</p>
<p>To just compare two strings for equality/non-equality, you can just use
<a href="perlapi.html#memEQ">memEQ() </a> and <a href="perlapi.html#memEQ">memNE() </a> as usual,
except the strings must be both UTF-8 or not UTF-8 encoded.</p>
<p>To compare two strings case-insensitively, use
<a href="perlapi.html#foldEQ_utf8">foldEQ_utf8() </a> (the strings don't have to have
the same UTF-8ness).</p>
<a name="Is-there-anything-else-I-need-to-know%3f"></a><h2>Is there anything else I need to know?</h2>
<p>Not really.  Just remember these things:</p>
<ul>
<li>
<p>There's no way to tell if a <code class="inline"><span class="w">char</span> *</code>
 or <code class="inline"><span class="w">U8</span> *</code>
 string is UTF-8
or not.  But you can tell if an SV is to be treated as UTF-8 by calling
<code class="inline"><span class="w">DO_UTF8</span></code>
 on it, after stringifying it with <code class="inline"><span class="w">SvPV</span></code>
 or a similar
macro.  And, you can tell if SV is actually UTF-8 (even if it is not to
be treated as such) by looking at its <code class="inline"><span class="w">SvUTF8</span></code>
 flag (again after
stringifying it).  Don't forget to set the flag if something should be
UTF-8.
Treat the flag as part of the PV, even though it's not -- if you pass on
the PV to somewhere, pass on the flag too.</p>
</li>
<li>
<p>If a string is UTF-8, <b>always</b> use <code class="inline"><span class="w">utf8_to_uvchr_buf</span></code>
 to get at the value,
unless <code class="inline"><span class="i">UTF8_IS_INVARIANT</span><span class="s">(</span><span class="i">*s</span><span class="s">)</span></code>
 in which case you can use <code class="inline"><span class="i">*s</span></code>
.</p>
</li>
<li>
<p>When writing a character UV to a UTF-8 string, <b>always</b> use
<code class="inline"><span class="w">uvchr_to_utf8</span></code>
, unless <code class="inline"><span class="i">UVCHR_IS_INVARIANT</span><span class="s">(</span><span class="w">uv</span><span class="s">)</span><span class="p">)</span></code>
 in which case
you can use <code class="inline"><span class="i">*s</span> = <span class="w">uv</span></code>
.</p>
</li>
<li>
<p>Mixing UTF-8 and non-UTF-8 strings is
tricky.  Use <code class="inline"><span class="w">bytes_to_utf8</span></code>
 to get
a new string which is UTF-8 encoded, and then combine them.</p>
</li>
</ul>
<a name="Custom-Operators"></a><h1>Custom Operators</h1>
<p>Custom operator support is an experimental feature that allows you to
define your own ops.  This is primarily to allow the building of
interpreters for other languages in the Perl core, but it also allows
optimizations through the creation of "macro-ops" (ops which perform the
functions of multiple ops which are usually executed together, such as
<code class="inline"><span class="w">gvsv</span><span class="cm">,</span> <span class="w">gvsv</span><span class="cm">,</span> <span class="w">add</span></code>
.)</p>
<p>This feature is implemented as a new op type, <code class="inline"><span class="w">OP_CUSTOM</span></code>
.  The Perl
core does not "know" anything special about this op type, and so it will
not be involved in any optimizations.  This also means that you can
define your custom ops to be any op structure -- unary, binary, list and
so on -- you like.</p>
<p>It's important to know what custom operators won't do for you.  They
won't let you add new syntax to Perl, directly.  They won't even let you
add new keywords, directly.  In fact, they won't change the way Perl
compiles a program at all.  You have to do those changes yourself, after
Perl has compiled the program.  You do this either by manipulating the op
tree using a <code class="inline">CHECK</code>
 block and the <code class="inline"><span class="w">B::Generate</span></code>
 module, or by adding
a custom peephole optimizer with the <code class="inline"><span class="w">optimize</span></code>
 module.</p>
<p>When you do this, you replace ordinary Perl ops with custom ops by
creating ops with the type <code class="inline"><span class="w">OP_CUSTOM</span></code>
 and the <code class="inline"><span class="w">op_ppaddr</span></code>
 of your own
PP function.  This should be defined in XS code, and should look like
the PP ops in <code class="inline">pp_*.c</code>.  You are responsible for ensuring that your op
takes the appropriate number of values from the stack, and you are
responsible for adding stack marks if necessary.</p>
<p>You should also "register" your op with the Perl interpreter so that it
can produce sensible error and warning messages.  Since it is possible to
have multiple custom ops within the one "logical" op type <code class="inline"><span class="w">OP_CUSTOM</span></code>
,
Perl uses the value of <code class="inline"><span class="w">o</span><span class="w">-&gt;op_ppaddr</span></code>
 to determine which custom op
it is dealing with.  You should create an <code class="inline"><span class="w">XOP</span></code>
 structure for each
ppaddr you use, set the properties of the custom op with
<code class="inline"><span class="w">XopENTRY_set</span></code>
, and register the structure against the ppaddr using
<code class="inline"><span class="w">Perl_custom_op_register</span></code>
.  A trivial example might look like:</p>
<pre class="verbatim"><ol><li>    <span class="w">static</span> <span class="w">XOP</span> <span class="w">my_xop</span><span class="sc">;</span></li><li>    <span class="w">static</span> <span class="w">OP</span> *<span class="i">my_pp</span><span class="s">(</span><span class="w">pTHX</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="j">BOOT:</span></li><li>        <span class="i">XopENTRY_set</span><span class="s">(</span><span class="i">&amp;my_xop</span><span class="cm">,</span> <span class="w">xop_name</span><span class="cm">,</span> <span class="q">&quot;myxop&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="i">XopENTRY_set</span><span class="s">(</span><span class="i">&amp;my_xop</span><span class="cm">,</span> <span class="w">xop_desc</span><span class="cm">,</span> <span class="q">&quot;Useless custom op&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="i">Perl_custom_op_register</span><span class="s">(</span><span class="w">aTHX_</span> <span class="w">my_pp</span><span class="cm">,</span> <span class="i">&amp;my_xop</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The available fields in the structure are:</p>
<ul>
<li><a name="xop_name"></a><b>xop_name</b>
<p>A short name for your op.  This will be included in some error messages,
and will also be returned as <code class="inline"><span class="i">$op</span><span class="i">-&gt;name</span></code>
 by the <a href="B.html">B</a> module, so
it will appear in the output of module like <a href="B/Concise.html">B::Concise</a>.</p>
</li>
<li><a name="xop_desc"></a><b>xop_desc</b>
<p>A short description of the function of the op.</p>
</li>
<li><a name="xop_class"></a><b>xop_class</b>
<p>Which of the various <code class="inline"><span class="i">*OP</span></code>
 structures this op uses.  This should be one of
the <code class="inline"><span class="w">OA_</span>*</code>
 constants from <i>op.h</i>, namely</p>
<ul>
<li><a name="OA_BASEOP"></a><b>OA_BASEOP</b>
</li>
<li><a name="OA_UNOP"></a><b>OA_UNOP</b>
</li>
<li><a name="OA_BINOP"></a><b>OA_BINOP</b>
</li>
<li><a name="OA_LOGOP"></a><b>OA_LOGOP</b>
</li>
<li><a name="OA_LISTOP"></a><b>OA_LISTOP</b>
</li>
<li><a name="OA_PMOP"></a><b>OA_PMOP</b>
</li>
<li><a name="OA_SVOP"></a><b>OA_SVOP</b>
</li>
<li><a name="OA_PADOP"></a><b>OA_PADOP</b>
</li>
<li><a name="OA_PVOP_OR_SVOP"></a><b>OA_PVOP_OR_SVOP</b>
<p>This should be interpreted as '<code class="inline"><span class="w">PVOP</span></code>
' only.  The <code class="inline"><span class="w">_OR_SVOP</span></code>
 is because
the only core <code class="inline"><span class="w">PVOP</span></code>
, <code class="inline"><span class="w">OP_TRANS</span></code>
, can sometimes be a <code class="inline"><span class="w">SVOP</span></code>
 instead.</p>
</li>
<li><a name="OA_LOOP"></a><b>OA_LOOP</b>
</li>
<li><a name="OA_COP"></a><b>OA_COP</b>
</li>
</ul>
<p>The other <code class="inline"><span class="w">OA_</span>*</code>
 constants should not be used.</p>
</li>
<li><a name="xop_peep"></a><b>xop_peep</b>
<p>This member is of type <code class="inline"><span class="w">Perl_cpeep_t</span></code>
, which expands to <code class="inline"><span class="i">void</span>
<span class="s">(</span><span class="i">*Perl_cpeep_t</span><span class="s">)</span><span class="s">(</span><span class="w">aTHX_</span> <span class="w">OP</span> *<span class="w">o</span><span class="cm">,</span> <span class="w">OP</span> *<span class="w">oldop</span><span class="s">)</span></code>
.  If it is set, this function
will be called from <code class="inline"><span class="w">Perl_rpeep</span></code>
 when ops of this type are encountered
by the peephole optimizer.  <i>o</i> is the OP that needs optimizing;
<i>oldop</i> is the previous OP optimized, whose <code class="inline"><span class="w">op_next</span></code>
 points to <i>o</i>.</p>
</li>
</ul>
<p><code class="inline"><span class="w">B::Generate</span></code>
 directly supports the creation of custom ops by name.</p>
<a name="AUTHORS"></a><h1>AUTHORS</h1>
<p>Until May 1997, this document was maintained by Jeff Okamoto
&lt;okamoto@corp.hp.com&gt;.  It is now maintained as part of Perl
itself by the Perl 5 Porters &lt;perl5-porters@perl.org&gt;.</p>
<p>With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
Stephen McCamant, and Gurusamy Sarathy.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="perlapi.html">perlapi</a>, <a href="perlintern.html">perlintern</a>, <a href="perlxs.html">perlxs</a>, <a href="perlembed.html">perlembed</a></p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Variables">Variables</a><ul><li><a href="#Datatypes">Datatypes</a><li><a href="#What-is-an-%22IV%22%3f">What is an "IV"?</a><li><a href="#Working-with-SVs">Working with SVs</a><li><a href="#Offsets">Offsets</a><li><a href="#What's-Really-Stored-in-an-SV%3f">What's Really Stored in an SV?</a><li><a href="#Working-with-AVs">Working with AVs</a><li><a href="#Working-with-HVs">Working with HVs</a><li><a href="#Hash-API-Extensions">Hash API Extensions</a><li><a href="#AVs%2c-HVs-and-undefined-values">AVs, HVs and undefined values</a><li><a href="#References">References</a><li><a href="#Blessed-References-and-Class-Objects">Blessed References and Class Objects</a><li><a href="#Creating-New-Variables">Creating New Variables</a><li><a href="#Reference-Counts-and-Mortality">Reference Counts and Mortality</a><li><a href="#Stashes-and-Globs">Stashes and Globs</a><li><a href="#Double-Typed-SVs">Double-Typed SVs</a><li><a href="#Read-Only-Values">Read-Only Values</a><li><a href="#Copy-on-Write">Copy on Write</a><li><a href="#Magic-Variables">Magic Variables</a><li><a href="#Assigning-Magic">Assigning Magic</a><li><a href="#Magic-Virtual-Tables">Magic Virtual Tables</a><li><a href="#Finding-Magic">Finding Magic</a><li><a href="#Understanding-the-Magic-of-Tied-Hashes-and-Arrays">Understanding the Magic of Tied Hashes and Arrays</a><li><a href="#Localizing-changes">Localizing changes</a></ul><li><a href="#Subroutines">Subroutines</a><ul><li><a href="#XSUBs-and-the-Argument-Stack">XSUBs and the Argument Stack</a><li><a href="#Autoloading-with-XSUBs">Autoloading with XSUBs</a><li><a href="#Calling-Perl-Routines-from-within-C-Programs">Calling Perl Routines from within C Programs</a><li><a href="#Putting-a-C-value-on-Perl-stack">Putting a C value on Perl stack</a><li><a href="#Scratchpads">Scratchpads</a><li><a href="#Scratchpads-and-recursion">Scratchpads and recursion</a></ul><li><a href="#Memory-Allocation">Memory Allocation</a><ul><li><a href="#Allocation">Allocation</a><li><a href="#Reallocation">Reallocation</a><li><a href="#Moving">Moving</a></ul><li><a href="#PerlIO">PerlIO</a><li><a href="#Compiled-code">Compiled code</a><ul><li><a href="#Code-tree">Code tree</a><li><a href="#Examining-the-tree">Examining the tree</a><li><a href="#Compile-pass-1%3a-check-routines">Compile pass 1: check routines</a><li><a href="#Compile-pass-1a%3a-constant-folding">Compile pass 1a: constant folding</a><li><a href="#Compile-pass-2%3a-context-propagation">Compile pass 2: context propagation</a><li><a href="#Compile-pass-3%3a-peephole-optimization">Compile pass 3: peephole optimization</a><li><a href="#Pluggable-runops">Pluggable runops</a><li><a href="#Compile-time-scope-hooks">Compile-time scope hooks</a></ul><li><a href="#Examining-internal-data-structures-with-the-dump-functions">Examining internal data structures with the dump functions</a><li><a href="#How-multiple-interpreters-and-concurrency-are-supported">How multiple interpreters and concurrency are supported</a><ul><li><a href="#Background-and-PERL_IMPLICIT_CONTEXT">Background and PERL_IMPLICIT_CONTEXT</a><li><a href="#So-what-happened-to-dTHR%3f">So what happened to dTHR?</a><li><a href="#How-do-I-use-all-this-in-extensions%3f">How do I use all this in extensions?</a><li><a href="#Should-I-do-anything-special-if-I-call-perl-from-multiple-threads%3f">Should I do anything special if I call perl from multiple threads?</a><li><a href="#Future-Plans-and-PERL_IMPLICIT_SYS">Future Plans and PERL_IMPLICIT_SYS</a></ul><li><a href="#Internal-Functions">Internal Functions</a><ul><li><a href="#Formatted-Printing-of-IVs%2c-UVs%2c-and-NVs">Formatted Printing of IVs, UVs, and NVs</a><li><a href="#Pointer-To-Integer-and-Integer-To-Pointer">Pointer-To-Integer and Integer-To-Pointer</a><li><a href="#Exception-Handling">Exception Handling</a><li><a href="#Source-Documentation">Source Documentation</a><li><a href="#Backwards-compatibility">Backwards compatibility</a></ul><li><a href="#Unicode-Support">Unicode Support</a><ul><li><a href="#What-*is*-Unicode%2c-anyway%3f">What *is* Unicode, anyway?</a><li><a href="#How-can-I-recognise-a-UTF-8-string%3f">How can I recognise a UTF-8 string?</a><li><a href="#How-does-UTF-8-represent-Unicode-characters%3f">How does UTF-8 represent Unicode characters?</a><li><a href="#How-does-Perl-store-UTF-8-strings%3f">How does Perl store UTF-8 strings?</a><li><a href="#How-do-I-convert-a-string-to-UTF-8%3f">How do I convert a string to UTF-8?</a><li><a href="#How-do-I-compare-strings%3f">How do I compare strings?</a><li><a href="#Is-there-anything-else-I-need-to-know%3f">Is there anything else I need to know?</a></ul><li><a href="#Custom-Operators">Custom Operators</a><li><a href="#AUTHORS">AUTHORS</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlguts';
  perldoc.pageAddress = 'perlguts.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>