Codebase list libffi-platypus-perl / a53c2e97-a0cf-4a1c-900c-c4a92edac820/v2.04
a53c2e97-a0cf-4a1c-900c-c4a92edac820/v2.04

Tree @a53c2e97-a0cf-4a1c-900c-c4a92edac820/v2.04 (Download .tar.gz)

   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
# FFI::Platypus [![Build Status](https://api.cirrus-ci.com/github/PerlFFI/FFI-Platypus.svg)](https://cirrus-ci.com/github/PerlFFI/FFI-Platypus) ![static](https://github.com/PerlFFI/FFI-Platypus/workflows/static/badge.svg) ![linux](https://github.com/PerlFFI/FFI-Platypus/workflows/linux/badge.svg) ![windows](https://github.com/PerlFFI/FFI-Platypus/workflows/windows/badge.svg) ![macos](https://github.com/PerlFFI/FFI-Platypus/workflows/macos/badge.svg) ![msys2-mingw](https://github.com/PerlFFI/FFI-Platypus/workflows/msys2-mingw/badge.svg)

Write Perl bindings to non-Perl libraries with FFI. No XS required.

# SYNOPSIS

```perl
use FFI::Platypus 2.00;

# for all new code you should use api => 2
my $ffi = FFI::Platypus->new(
  api => 2,
  lib => undef, # search libc
);

# call dynamically
$ffi->function( puts => ['string'] => 'int' )->call("hello world");

# attach as a xsub and call (much faster)
$ffi->attach( puts => ['string'] => 'int' );
puts("hello world");
```

# DESCRIPTION

Platypus is a library for creating interfaces to machine code libraries
written in languages like C, [C++](https://metacpan.org/pod/FFI::Platypus::Lang::CPP),
[Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go),
[Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran),
[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust),
[Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal). Essentially anything that gets
compiled into machine code.  This implementation uses [libffi](https://sourceware.org/libffi/) to
accomplish this task.  [libffi](https://sourceware.org/libffi/) is battle tested by a number of other
scripting and virtual machine languages, such as Python and Ruby to
serve a similar role.  There are a number of reasons why you might want
to write an extension with Platypus instead of XS:

- FFI / Platypus does not require messing with the guts of Perl

    XS is less of an API and more of the guts of perl splayed out to do
    whatever you want.  That may at times be very powerful, but it can also
    be a frustrating exercise in hair pulling.

- FFI / Platypus is portable

    Lots of languages have FFI interfaces, and it is subjectively easier to
    port an extension written in FFI in Perl or another language to FFI in
    another language or Perl.  One goal of the Platypus Project is to reduce
    common interface specifications to a common format like JSON that could
    be shared between different languages.

- FFI / Platypus could be a bridge to Raku

    One of those "other" languages could be Raku and Raku already has an
    FFI interface I am told.

- FFI / Platypus can be reimplemented

    In a bright future with multiple implementations of Perl 5, each
    interpreter will have its own implementation of Platypus, allowing
    extensions to be written once and used on multiple platforms, in much
    the same way that Ruby-FFI extensions can be use in Ruby, JRuby and
    Rubinius.

- FFI / Platypus is pure perl (sorta)

    One Platypus script or module works on any platform where the libraries
    it uses are available.  That means you can deploy your Platypus script
    in a shared filesystem where they may be run on different platforms.  It
    also means that Platypus modules do not need to be installed in the
    platform specific Perl library path.

- FFI / Platypus is not C or C++ centric

    XS is implemented primarily as a bunch of C macros, which requires at
    least some understanding of C, the C pre-processor, and some C++ caveats
    (since on some platforms Perl is compiled and linked with a C++
    compiler). Platypus on the other hand could be used to call other
    compiled languages, like [Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran),
    [Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go),
    [Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust),
    [Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal), [C++](https://metacpan.org/pod/FFI::Platypus::Lang::CPP),
    or even [assembly](https://metacpan.org/pod/FFI::Platypus::Lang::ASM), allowing you to focus
    on your strengths.

- FFI / Platypus does not require a parser

    [Inline](https://metacpan.org/pod/Inline) isolates the extension developer from XS to some extent, but
    it also requires a parser.  The various [Inline](https://metacpan.org/pod/Inline) language bindings are
    a great technical achievement, but I think writing a parser for every
    language that you want to interface with is a bit of an anti-pattern.

This document consists of an API reference, a set of examples, some
support and development (for contributors) information.  If you are new
to Platypus or FFI, you may want to skip down to the
[EXAMPLES](#examples) to get a taste of what you can do with Platypus.

Platypus has extensive documentation of types at [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type)
and its custom types API at [FFI::Platypus::API](https://metacpan.org/pod/FFI::Platypus::API).

You are **strongly** encouraged to use API level 1 for all new code.
There are a number of improvements and design fixes that you get
for free.  You should even consider updating existing modules to
use API level 1 where feasible.  How do I do that you might ask?
Simply pass in the API level to the platypus constructor.

```perl
my $ffi = FFI::Platypus->new( api => 2 );
```

The Platypus documentation has already been updated to assume API
level 1.

# CONSTRUCTORS

## new

```perl
my $ffi = FFI::Platypus->new( api => 2, %options);
```

Create a new instance of [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).

Any types defined with this instance will be valid for this instance
only, so you do not need to worry about stepping on the toes of other
CPAN FFI / Platypus Authors.

Any functions found will be out of the list of libraries specified with
the [lib](#lib) attribute.

### options

- api

    \[version 0.91\]

    Sets the API level.  Legal values are

    - `0`

        Original API level.  See [FFI::Platypus::TypeParser::Version0](https://metacpan.org/pod/FFI::Platypus::TypeParser::Version0) for details
        on the differences.

    - `1`

        Enable version 1 API type parser which allows pass-by-value records
        and type decoration on basic types.

    - `2`

        Enable version 2 API. All new code should be written with this set to 1!
        The Platypus documentation assumes this api level is set.

        API version 2 is identical to version 1, except:

        - Pointer functions that return `NULL` will return `undef` instead of empty list

            This fixes a long standing design bug in Platypus.

        - Array references may be passed to pointer argument types

            This replicates the behavior of array argument types with no size.  So the types `sint8*` and `sint8[]`
            behave identically when an array reference is passed in.  They differ in that, as before, you can
            pass a scalar reference into type `sint8*`.

        - The fixed string type can be specified without pointer modifier

            That is you can use `string(10)` instead of `string(10)*` as you were previously able to
            in API 0.

- lib

    Either a pathname (string) or a list of pathnames (array ref of strings)
    to pre-populate the [lib](#lib) attribute.  Use `[undef]` to search the
    current process for symbols.

    0.48

    `undef` (without the array reference) can be used to search the current
    process for symbols.

- ignore\_not\_found

    \[version 0.15\]

    Set the [ignore\_not\_found](#ignore_not_found) attribute.

- lang

    \[version 0.18\]

    Set the [lang](#lang) attribute.

# ATTRIBUTES

## lib

```perl
$ffi->lib($path1, $path2, ...);
my @paths = $ffi->lib;
```

The list of libraries to search for symbols in.

The most portable and reliable way to find dynamic libraries is by using
[FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib), like this:

```perl
use FFI::CheckLib 0.06;
$ffi->lib(find_lib_or_die lib => 'archive');
  # finds libarchive.so on Linux
  #       libarchive.bundle on OS X
  #       libarchive.dll (or archive.dll) on Windows
  #       cygarchive-13.dll on Cygwin
  #       ...
  # and will die if it isn't found
```

[FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) has a number of options, such as checking for specific
symbols, etc.  You should consult the documentation for that module.

As a special case, if you add `undef` as a "library" to be searched,
Platypus will also search the current process for symbols. This is
mostly useful for finding functions in the standard C library, without
having to know the name of the standard c library for your platform (as
it turns out it is different just about everywhere!).

You may also use the ["find\_lib"](#find_lib) method as a shortcut:

```perl
$ffi->find_lib( lib => 'archive' );
```

## ignore\_not\_found

\[version 0.15\]

```perl
$ffi->ignore_not_found(1);
my $ignore_not_found = $ffi->ignore_not_found;
```

Normally the [attach](#attach) and [function](#function) methods will
throw an exception if it cannot find the name of the function you
provide it.  This will change the behavior such that
[function](#function) will return `undef` when the function is not
found and [attach](#attach) will ignore functions that are not found.
This is useful when you are writing bindings to a library and have many
optional functions and you do not wish to wrap every call to
[function](#function) or [attach](#attach) in an `eval`.

## lang

\[version 0.18\]

```
$ffi->lang($language);
```

Specifies the foreign language that you will be interfacing with. The
default is C.  The foreign language specified with this attribute
changes the default native types (for example, if you specify
[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust), you will get `i32` as an alias for
`sint32` instead of `int` as you do with [C](https://metacpan.org/pod/FFI::Platypus::Lang::C)).

If the foreign language plugin supports it, this will also enable
Platypus to find symbols using the demangled names (for example, if you
specify [CPP](https://metacpan.org/pod/FFI::Platypus::Lang::CPP) for C++ you can use method names
like `Foo::get_bar()` with ["attach"](#attach) or ["function"](#function).

## api

\[version 1.11\]

```perl
my $level = $ffi->api;
```

Returns the API level of the Platypus instance.

# METHODS

## type

```perl
$ffi->type($typename);
$ffi->type($typename => $alias);
```

Define a type.  The first argument is the native or C name of the type.
The second argument (optional) is an alias name that you can use to
refer to this new type.  See [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) for legal type
definitions.

Examples:

```perl
$ffi->type('sint32');            # only checks to see that sint32 is a valid type
$ffi->type('sint32' => 'myint'); # creates an alias myint for sint32
$ffi->type('bogus');             # dies with appropriate diagnostic
```

## custom\_type

```perl
$ffi->custom_type($alias => {
  native_type         => $native_type,
  native_to_perl      => $coderef,
  perl_to_native      => $coderef,
  perl_to_native_post => $coderef,
});
```

Define a custom type.  See [FFI::Platypus::Type#Custom-Types](https://metacpan.org/pod/FFI::Platypus::Type#Custom-Types) for details.

## load\_custom\_type

```perl
$ffi->load_custom_type($name => $alias, @type_args);
```

Load the custom type defined in the module _$name_, and make an alias
_$alias_. If the custom type requires any arguments, they may be passed
in as _@type\_args_. See [FFI::Platypus::Type#Custom-Types](https://metacpan.org/pod/FFI::Platypus::Type#Custom-Types) for
details.

If _$name_ contains `::` then it will be assumed to be a fully
qualified package name. If not, then `FFI::Platypus::Type::` will be
prepended to it.

## types

```perl
my @types = $ffi->types;
my @types = FFI::Platypus->types;
```

Returns the list of types that FFI knows about.  This will include the
native `libffi` types (example: `sint32`, `opaque` and `double`) and
the normal C types (example: `unsigned int`, `uint32_t`), any types
that you have defined using the [type](#type) method, and custom types.

The list of types that Platypus knows about varies somewhat from
platform to platform, [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) includes a list of the core
types that you can always count on having access to.

It can also be called as a class method, in which case, no user defined
or custom types will be included in the list.

## type\_meta

```perl
my $meta = $ffi->type_meta($type_name);
my $meta = FFI::Platypus->type_meta($type_name);
```

Returns a hash reference with the meta information for the given type.

It can also be called as a class method, in which case, you won't be
able to get meta data on user defined types.

The format of the meta data is implementation dependent and subject to
change.  It may be useful for display or debugging.

Examples:

```perl
my $meta = $ffi->type_meta('int');        # standard int type
my $meta = $ffi->type_meta('int[64]');    # array of 64 ints
$ffi->type('int[128]' => 'myintarray');
my $meta = $ffi->type_meta('myintarray'); # array of 128 ints
```

## mangler

```
$ffi->mangler(\&mangler);
```

Specify a customer mangler to be used for symbol lookup.  This is usually useful
when you are writing bindings for a library where all of the functions have the
same prefix.  Example:

```perl
$ffi->mangler(sub {
  my($symbol) = @_;
  return "foo_$symbol";
});

$ffi->function( get_bar => [] => 'int' );  # attaches foo_get_bar

my $f = $ffi->function( set_baz => ['int'] => 'void' );
$f->call(22); # calls foo_set_baz
```

## function

```perl
my $function = $ffi->function($name => \@argument_types => $return_type);
my $function = $ffi->function($address => \@argument_types => $return_type);
my $function = $ffi->function($name => \@argument_types => $return_type, \&wrapper);
my $function = $ffi->function($address => \@argument_types => $return_type, \&wrapper);
```

Returns an object that is similar to a code reference in that it can be
called like one.

Caveat: many situations require a real code reference, so at the price
of a performance penalty you can get one like this:

```perl
my $function = $ffi->function(...);
my $coderef = sub { $function->(@_) };
```

It may be better, and faster to create a real Perl function using the
[attach](#attach) method.

In addition to looking up a function by name you can provide the address
of the symbol yourself:

```perl
my $address = $ffi->find_symbol('my_function');
my $function = $ffi->function($address => ...);
```

Under the covers, [function](#function) uses [find\_symbol](#find_symbol)
when you provide it with a name, but it is useful to keep this in mind
as there are alternative ways of obtaining a functions address.
Example: a C function could return the address of another C function
that you might want to call.

\[version 0.76\]

If the last argument is a code reference, then it will be used as a
wrapper around the function when called.  The first argument to the wrapper
will be the inner function, or if it is later attached an xsub.  This can be
used if you need to verify/modify input/output data.

Examples:

```perl
my $function = $ffi->function('my_function_name', ['int', 'string'] => 'string');
my $return_string = $function->(1, "hi there");
```

\[version 0.91\]

```perl
my $function = $ffi->function( $name => \@fixed_argument_types => \@var_argument_types => $return_type);
my $function = $ffi->function( $name => \@fixed_argument_types => \@var_argument_types => $return_type, \&wrapper);
my $function = $ffi->function( $name => \@fixed_argument_types => \@var_argument_types);
my $function = $ffi->function( $name => \@fixed_argument_types => \@var_argument_types => \&wrapper);
```

Version 0.91 and later allows you to creat functions for c variadic functions
(such as printf, scanf, etc) which can take a variable number of arguments.
The first set of arguments are the fixed set, the second set are the variable
arguments to bind with.  The variable argument types must be specified in order
to create a function object, so if you need to call variadic function with
different set of arguments then you will need to create a new function object
each time:

```perl
# int printf(const char *fmt, ...);
$ffi->function( printf => ['string'] => ['int'] => 'int' )
    ->call("print integer %d\n", 42);
$ffi->function( printf => ['string'] => ['string'] => 'int' )
    ->call("print string %s\n", 'platypus');
```

Some older versions of libffi and possibly some platforms may not support
variadic functions.  If you try to create a one, then an exception will be
thrown.

\[version 1.26\]

If the return type is omitted then `void` will be the assumed return type.

## attach

```perl
$ffi->attach($name => \@argument_types => $return_type);
$ffi->attach([$c_name => $perl_name] => \@argument_types => $return_type);
$ffi->attach([$address => $perl_name] => \@argument_types => $return_type);
$ffi->attach($name => \@argument_types => $return_type, \&wrapper);
$ffi->attach([$c_name => $perl_name] => \@argument_types => $return_type, \&wrapper);
$ffi->attach([$address => $perl_name] => \@argument_types => $return_type, \&wrapper);
```

Find and attach a C function as a real live Perl xsub.  The advantage of
attaching a function over using the [function](#function) method is that
it is much much much faster since no object resolution needs to be done.
The disadvantage is that it locks the function and the [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus)
instance into memory permanently, since there is no way to deallocate an
xsub.

If just one _$name_ is given, then the function will be attached in
Perl with the same name as it has in C.  The second form allows you to
give the Perl function a different name.  You can also provide an
address (the third form), just like with the [function](#function)
method.

Examples:

```perl
$ffi->attach('my_function_name', ['int', 'string'] => 'string');
$ffi->attach(['my_c_function_name' => 'my_perl_function_name'], ['int', 'string'] => 'string');
my $string1 = my_function_name($int);
my $string2 = my_perl_function_name($int);
```

\[version 0.20\]

If the last argument is a code reference, then it will be used as a
wrapper around the attached xsub.  The first argument to the wrapper
will be the inner xsub.  This can be used if you need to verify/modify
input/output data.

Examples:

```perl
$ffi->attach('my_function', ['int', 'string'] => 'string', sub {
  my($my_function_xsub, $integer, $string) = @_;
  $integer++;
  $string .= " and another thing";
  my $return_string = $my_function_xsub->($integer, $string);
  $return_string =~ s/Belgium//; # HHGG remove profanity
  $return_string;
});
```

\[version 0.91\]

```perl
$ffi->attach($name => \@fixed_argument_types => \@var_argument_types, $return_type);
$ffi->attach($name => \@fixed_argument_types => \@var_argument_types, $return_type, \&wrapper);
```

As of version 0.91 you can attach a variadic functions, if it is supported
by the platform / libffi that you are using.  For details see the `function`
documentation.  If not supported by the implementation then an exception
will be thrown.

## closure

```perl
my $closure = $ffi->closure($coderef);
my $closure = FFI::Platypus->closure($coderef);
```

Prepares a code reference so that it can be used as a FFI closure (a
Perl subroutine that can be called from C code).  For details on
closures, see [FFI::Platypus::Type#Closures](https://metacpan.org/pod/FFI::Platypus::Type#Closures) and [FFI::Platypus::Closure](https://metacpan.org/pod/FFI::Platypus::Closure).

## cast

```perl
my $converted_value = $ffi->cast($original_type, $converted_type, $original_value);
```

The `cast` function converts an existing _$original\_value_ of type
_$original\_type_ into one of type _$converted\_type_.  Not all types
are supported, so care must be taken.  For example, to get the address
of a string, you can do this:

```perl
my $address = $ffi->cast('string' => 'opaque', $string_value);
```

Something that won't work is trying to cast an array to anything:

```perl
my $address = $ffi->cast('int[10]' => 'opaque', \@list);  # WRONG
```

## attach\_cast

```perl
$ffi->attach_cast("cast_name", $original_type, $converted_type);
$ffi->attach_cast("cast_name", $original_type, $converted_type, \&wrapper);
my $converted_value = cast_name($original_value);
```

This function attaches a cast as a permanent xsub.  This will make it
faster and may be useful if you are calling a particular cast a lot.

\[version 1.26\]

A wrapper may be added as the last argument to `attach_cast` and works
just like the wrapper for `attach` and `function` methods.

## sizeof

```perl
my $size = $ffi->sizeof($type);
my $size = FFI::Platypus->sizeof($type);
```

Returns the total size of the given type in bytes.  For example to get
the size of an integer:

```perl
my $intsize = $ffi->sizeof('int');   # usually 4
my $longsize = $ffi->sizeof('long'); # usually 4 or 8 depending on platform
```

You can also get the size of arrays

```perl
my $intarraysize = $ffi->sizeof('int[64]');  # usually 4*64
my $intarraysize = $ffi->sizeof('long[64]'); # usually 4*64 or 8*64
                                             # depending on platform
```

Keep in mind that "pointer" types will always be the pointer / word size
for the platform that you are using.  This includes strings, opaque and
pointers to other types.

This function is not very fast, so you might want to save this value as
a constant, particularly if you need the size in a loop with many
iterations.

## alignof

\[version 0.21\]

```perl
my $align = $ffi->alignof($type);
```

Returns the alignment of the given type in bytes.

## kindof

\[version 1.24\]

```perl
my $kind = $ffi->kindof($type);
```

Returns the kind of a type.  This is a string with a value of one of

- `void`
- `scalar`
- `string`
- `closure`
- `record`
- `record-value`
- `pointer`
- `array`
- `object`

## countof

\[version 1.24\]

```perl
my $count = $ffi->countof($type);
```

For array types returns the number of elements in the array (returns 0 for variable length array).
For the `void` type returns 0.  Returns 1 for all other types.

## def

\[version 1.24\]

```perl
$ffi->def($package, $type, $value);
my $value = $ff->def($package, $type);
```

This method allows you to store data for types.  If the `$package` is not provided, then the
caller's package will be used.  `$type` must be a legal Platypus type for the [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus)
instance.

## unitof

\[version 1.24\]

```perl
my $unittype = $ffi->unitof($type);
```

For array and pointer types, returns the basic type without the array or pointer part.
In other words, for `sin16[]` or `sint16*` it will return `sint16`.

## find\_lib

\[version 0.20\]

```perl
$ffi->find_lib( lib => $libname );
```

This is just a shortcut for calling [FFI::CheckLib#find\_lib](https://metacpan.org/pod/FFI::CheckLib#find_lib) and
updating the ["lib"](#lib) attribute appropriately.  Care should be taken
though, as this method simply passes its arguments to
[FFI::CheckLib#find\_lib](https://metacpan.org/pod/FFI::CheckLib#find_lib), so if your module or script is depending on a
specific feature in [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) then make sure that you update your
prerequisites appropriately.

## find\_symbol

```perl
my $address = $ffi->find_symbol($name);
```

Return the address of the given symbol (usually function).

## bundle

\[version 0.96 api = 1+\]

```
$ffi->bundle($package, \@args);
$ffi->bundle(\@args);
$ffi->bundle($package);
$ffi->bundle;
```

This is an interface for bundling compiled code with your
distribution intended to eventually replace the `package` method documented
above.  See [FFI::Platypus::Bundle](https://metacpan.org/pod/FFI::Platypus::Bundle) for details on how this works.

## package

\[version 0.15 api = 0\]

```perl
$ffi->package($package, $file); # usually __PACKAGE__ and __FILE__ can be used
$ffi->package;                  # autodetect
```

**Note**: This method is officially discouraged in favor of `bundle`
described above.

If you use [FFI::Build](https://metacpan.org/pod/FFI::Build) (or the older deprecated [Module::Build::FFI](https://metacpan.org/pod/Module::Build::FFI)
to bundle C code with your distribution, you can use this method to tell
the [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus) instance to look for symbols that came with the
dynamic library that was built when your distribution was installed.

## abis

```perl
my $href = $ffi->abis;
my $href = FFI::Platypus->abis;
```

Get the legal ABIs supported by your platform and underlying
implementation.  What is supported can vary a lot by CPU and by
platform, or even between 32 and 64 bit on the same CPU and platform.
They keys are the "ABI" names, also known as "calling conventions".  The
values are integers used internally by the implementation to represent
those ABIs.

## abi

```
$ffi->abi($name);
```

Set the ABI or calling convention for use in subsequent calls to
["function"](#function) or ["attach"](#attach).  May be either a string name or integer
value from the ["abis"](#abis) method above.

# EXAMPLES

Here are some examples.  These examples are provided in full with the
Platypus distribution in the "examples" directory.  There are also some
more examples in [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) that are related to types.

## Passing and Returning Integers

### C Source

```
int add(int a, int b) {
  return a+b;
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::CheckLib qw( find_lib_or_die );
use File::Basename qw( dirname );

my $ffi = FFI::Platypus->new( api => 2, lib => './add.so' );
$ffi->attach( add => ['int', 'int'] => 'int' );

print add(1,2), "\n";  # prints 3
```

### Execute

```
$ cc -shared -o add.so add.c
$ perl add.pl
3
```

### Discussion

Basic types like integers and floating points are the easiest to pass
across the FFI boundary.  Because they are values that are passed on
the stack (or through registers) you don't need to worry about memory
allocations or ownership.

Here we are building our own C dynamic library using the native C
compiler on a Unix like platform.  The exact incantation that you
will use to do this would unfortunately depend on your platform and
C compiler.

By default, Platypus uses the
[Platypus C language plugin](https://metacpan.org/pod/FFI::Platypus::Lang::C), which gives you
easy access to many of the basic types used by C APIs.  (for example
`int`, `unsigned long`, `double`, `size_t` and others).

If you are working with another language like
[Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran#Passing-and-Returning-Integers),
[Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go#Passing-and-Returning-Integers),
[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust#Passing-and-Returning-Integers) or
[Zig](https://metacpan.org/pod/FFI::Platypus::Lang::Zig#Passing-and-Returning-Integers),
you will find similar examples where you can use the Platypus language
plugin for that language and use the native types.

## String Arguments (with puts)

### C API

[cppreference - puts](https://en.cppreference.com/w/c/io/puts)

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new( api => 2, lib => undef );
$ffi->attach( puts => ['string'] => 'int' );

puts("hello world");
```

### Execute

```
$ perl puts.pl
hello world
```

### Discussion

Passing strings into a C function as an argument is also pretty easy
using Platypus.  Just use the `string` type, which is equivalent to
the C <char \*> or `const char *` types.

In this example we are using the C Standard Library's `puts` function,
so we don't need to build our own C code.  We do still need to tell
Platypus where to look for the `puts` symbol though, which is why
we set `lib` to `undef`.  This is a special value which tells
Platypus to search the Perl runtime executable itself (including any
dynamic libraries) for symbols.  That helpfully includes the C Standard
Library.

## Returning Strings

### C Source

```
#include <string.h>
#include <stdlib.h>

const char *
string_reverse(const char *input)
{
  static char *output = NULL;
  int i, len;

  if(output != NULL)
    free(output);

  if(input == NULL)
    return NULL;

  len = strlen(input);
  output = malloc(len+1);

  for(i=0; input[i]; i++)
    output[len-i-1] = input[i];
  output[len] = '\0';

  return output;
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './string_reverse.so',
);

$ffi->attach( string_reverse => ['string'] => 'string' );

print string_reverse("\nHello world");

string_reverse(undef);
```

### Execute

```
$ cc -shared -o string_reverse.so string_reverse.c
$ perl string_reverse.pl
dlrow olleH
```

### Discussion

The C code here takes an input ASCII string and reverses it, returning
the result.  Note that it retains ownership of the string, the caller
is expected to use it before the next call to `reverse_string`, or
copy it.

The Perl code simply declares the return value as `string` and is very
simple.  This does bring up an inconsistency though, strings passed in
to a function as arguments are passed by reference, whereas the return
value is copied!  This is usually what you want because C APIs usually
follow this pattern where you are expected to make your own copy of
the string.

At the end of the program we call `reverse_string` with `undef`, which
gets translated to C as `NULL`.  This allows it to free the output buffer
so that the memory will not leak.

## Returning and Freeing Strings with Embedded NULLs

### C Source

```
#include <string.h>
#include <stdlib.h>

char *
string_crypt(const char *input, int len, const char *key)
{
  char *output;
  int i, n;

  if(input == NULL)
    return NULL;

  output = malloc(len+1);
  output[len] = '\0';

  for(i=0, n=0; i<len; i++, n++) {
    if(key[n] == '\0')
      n = 0;
    output[i] = input[i] ^ key[n];
  }

  return output;
}

void
string_crypt_free(char *output)
{
  if(output != NULL)
    free(output);
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::Platypus::Buffer qw( buffer_to_scalar );
use YAML ();

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './xor_cipher.so',
);

$ffi->attach( string_crypt_free => ['opaque'] );

$ffi->attach( string_crypt => ['string','int','string'] => 'opaque' => sub{
  my($xsub, $input, $key) = @_;
  my $ptr = $xsub->($input, length($input), $key);
  my $output = buffer_to_scalar $ptr, length($input);
  string_crypt_free($ptr);
  return $output;
});

my $orig = "hello world";
my $key  = "foobar";

print YAML::Dump($orig);
my $encrypted = string_crypt($orig, $key);
print YAML::Dump($encrypted);
my $decrypted = string_crypt($encrypted, $key);
print YAML::Dump($decrypted);
```

### Execute

```
$ cc -shared -o xor_cipher.so xor_cipher.c
$ perl xor_cipher.pl
--- hello world
--- "\x0e\n\x03\x0e\x0eR\x11\0\x1d\x0e\x05"
--- hello world
```

### Discussion

The C code here also returns a string, but it has some different expectations,
so we can't just use the `string` type like we did in the previous example
and copy the string.

This C code implements a simple XOR cipher.  Given an input string and a key
it returns an encrypted or decrypted output string where the characters are
XORd with the key.  There are some challenges here though.  First the input
and output strings can have embedded `NULL`s in them.  For the string passed
in, we can provide the length of the input string.  For the output, the
`string` type expects a `NULL` terminated string, so we can't use that.  So
instead we get a pointer to the output using the `opaque` type.  Because we
know that the output string is the same length as the input string we can
convert the pointer to a regular Perl string using the `buffer_to_scalar`
function.  (For more details about working with buffers and strings see
[FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer)).

Next, the C code here does not keep the pointer to the output string, as in
the previous example.  We are expected to call `string_encrypt_free` when
we are done.  Since we are getting the pointer back from the C code instead
of copying the string that is easy to do.

Finally, we are using a wrapper to hide a lot of this complexity from our
caller.  The last argument to the `attach` call is a code reference which will
wrap around the C function, which is passed in as the first argument of
the wrapper.  This is a good practice when writing modules, to hide the
complexity of C.

## Pointers

### C Source

```
void
swap(int *a, int *b)
{
  int tmp = *b;
  *b = *a;
  *a = tmp;
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 1,
  lib => './swap.so',
);

$ffi->attach( swap => ['int*','int*'] );

my $a = 1;
my $b = 2;

print "[a,b] = [$a,$b]\n";

swap( \$a, \$b );

print "[a,b] = [$a,$b]\n";
```

### Execute

```
$ cc -shared -o swap.so swap.c
$ perl swap.pl
[a,b] = [1,2]
[a,b] = [2,1]
```

### Discussion

Pointers are often use in C APIs to return simple values like this.  Platypus
provides access to pointers to primitive types by appending `*` to the
primitive type.  Here for example we are using `int*` to create a function
that takes two pointers to integers and swaps their values.

When calling the function from Perl we pass in a reference to a scalar.
Strictly speaking Perl allows modifying the argument values to subroutines, so
we could have allowed just passing in a scalar, but in the design of Platypus
we decided that forcing the use of a reference here emphasizes that you are
passing a reference to the variable, not just the value.

Not pictured in this example, but you can also pass in `undef` for a pointer
value and that will be translated into `NULL` on the C side.  You can also
return a pointer to a primitive type from a function, again this will be
returned to Perl as a reference to a scalar.  Platypus also supports string
pointers (`string*`).  (Though the C equivalent to a `string*` is a double
pointer to char `char**`).

## Opaque Pointers (objects)

### C Source

```
#include <string.h>
#include <stdlib.h>

typedef struct person_t {
  char *name;
  unsigned int age;
} person_t;

person_t *
person_new(const char *name, unsigned int age) {
  person_t *self = malloc(sizeof(person_t));
  self->name = strdup(name);
  self->age  = age;
}

const char *
person_name(person_t *self) {
  return self->name;
}

unsigned int
person_age(person_t *self) {
  return self->age;
}

void
person_free(person_t *self) {
  free(self->name);
  free(self);
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './person.so',
);

$ffi->type( 'opaque' => 'person_t' );

$ffi->attach( person_new =>  ['string','unsigned int'] => 'person_t'       );
$ffi->attach( person_name => ['person_t']              => 'string'       );
$ffi->attach( person_age =>  ['person_t']              => 'unsigned int' );
$ffi->attach( person_free => ['person_t']                                  );

my $person = person_new( 'Roger Frooble Bits', 35 );

print "name = ", person_name($person), "\n";
print "age  = ", person_age($person),  "\n";

person_free($person);
```

### Execute

```
$ cc -shared -o person.so person.c
$ perl person.pl
name = Roger Frooble Bits
age  = 35
```

### Discussion

An opaque pointer is a pointer (memory address) that is pointing to _something_
but you do not know the structure of that something.  In C this is usually a
`void*`, but it could also be a pointer to a `struct` without a defined body.

This is often used to as an abstraction around objects in C.  Here in the C
code we have a `person_t` struct with functions to create (a constructor), free
(a destructor) and query it (methods).

The Perl code can then use the constructor, methods and destructors without having
to understand the internals.  The `person_t` internals can also be changed
without having to modify the calling code.

We use the Platypus [type method](#type) to create an alias of `opaque` called
`person_t`.  While this is not necessary, it does make the Perl code easier
to understand.

In later examples we will see how to hide the use of `opaque` types further
using the `object` type, but for some code direct use of `opaque` is
appropriate.

## Opaque Pointers (buffers and strings)

### C API

- [cppreference - free](https://en.cppreference.com/w/c/memory/free)
- [cppreference - malloc](https://en.cppreference.com/w/c/memory/malloc)
- [cppreference - memcpy](https://en.cppreference.com/w/c/string/byte/memcpy)
- [cppreference - strdup](https://en.cppreference.com/w/c/string/byte/strdup)

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::Platypus::Memory qw( malloc free memcpy strdup );

my $ffi = FFI::Platypus->new( api => 2 );
my $buffer = malloc 14;
my $ptr_string = strdup("hello there!!\n");

memcpy $buffer, $ptr_string, 15;

print $ffi->cast('opaque' => 'string', $buffer);

free $ptr_string;
free $buffer;
```

### Execute

```
$ perl malloc.pl
hello there!!
```

### Discussion

Another useful application of the `opaque` type is for dealing with buffers,
and C strings that you do not immediately need to convert into Perl strings.
This example is completely contrived, but we are using `malloc` to create a
buffer of 14 bytes.  We create a C string using `strdup`, and then copy it
into the buffer using `memcpy`.  When we are done with the `opaque` pointers
we can free them using `free` since they. (This is generally only okay when
freeing memory that was allocated by `malloc`, which is the case for `strdup`).

These memory tools, along with others are provided by the [FFI::Platypus::Memory](https://metacpan.org/pod/FFI::Platypus::Memory)
module, which is worth reviewing when you need to manipulate memory from
Perl when writing your FFI code.

Just to verify that the `memcpy` did the right thing we convert the
buffer into a Perl string and print it out using the Platypus [cast method](#cast).

## Arrays

### C Source

```
void
array_reverse(int a[], int len) {
  int tmp, i;

  for(i=0; i < len/2; i++) {
    tmp = a[i];
    a[i] = a[len-i-1];
    a[len-i-1] = tmp;
  }
}

void
array_reverse10(int a[10]) {
  array_reverse(a, 10);
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 1,
  lib => './array_reverse.so',
);

$ffi->attach( array_reverse   => ['int[]','int'] );
$ffi->attach( array_reverse10 => ['int[10]'] );

my @a = (1..10);
array_reverse10( \@a );
print "$_ " for @a;
print "\n";

@a = (1..20);
array_reverse( \@a, 20 );
print "$_ " for @a;
print "\n";
```

### Execute

```
$ cc -shared -o array_reverse.so array_reverse.c
$ perl array_reverse.pl
10 9 8 7 6 5 4 3 2 1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
```

### Discussion

Arrays in C are passed as pointers, so the C code here reverses the array
in place, rather than returning it.  Arrays can also be fixed or variable
length.  If the array is variable length the length of the array must be
provided in some way.  In this case we explicitly pass in a length.  Another
way might be to end the array with `0`, if you don't otherwise expect any
`0` to appear in your data.  For this reason, Platypus adds a zero (or
`NULL` in the case of pointers) element at the end of the array when passing
it into a variable length array type, although we do not use it here.

With Platypus you can declare an array type as being either fixed or variable
length.  Because Perl stores arrays in completely differently than C, a
temporary array is created by Platypus, passed into the C function as a pointer.
When the function returns the array is re-read by Platypus and the Perl array
is updated with the new values.  The temporary array is then freed.

You can use any primitive type for arrays, even `string`.  You can also
return an array from a function.  As in our discussion about strings, when
you return an array the value is copied, which is usually what you want.

## Pointers as Arrays

### C Source

```
#include <stdlib.h>

int
array_sum(const int *a) {
  int i, sum;
  if(a == NULL)
    return -1;
  for(i=0, sum=0; a[i] != 0; i++)
    sum += a[i];
  return sum;
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './array_sum.so',
);

$ffi->attach( array_sum => ['int*'] => 'int' );

print array_sum(undef), "\n";     # -1
print array_sum([0]), "\n";       # 0
print array_sum([1,2,3,0]), "\n"; # 6
```

### Execute

```
$ cc -shared -o array_sum.so array_sum.c
$ perl array_sum.pl
-1
0
6
```

### Discussion

Starting with the Platypus version 2 API, you can also pass an array reference
in to a pointer argument.

In C pointer and array arguments are often used somewhat interchangeably.  In
this example we have an `array_sum` function that takes a zero terminated
array of integers and computes the sum.  If the pointer to the array is zero
(`0`) then we return `-1` to indicate an error.

This is the main advantage from Perl for using pointer argument rather than
an array one: the array argument will not let you pass in `undef` / `NULL`.

## Sending Strings to GUI on Unix with libnotify

### C API

[Libnotify Reference Manual](https://developer-old.gnome.org/libnotify/unstable)

### Perl Source

```perl
use FFI::CheckLib;
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => find_lib_or_die(lib => 'notify'),
);

$ffi->attach( notify_init              => ['string']                                  );
$ffi->attach( notify_uninit            => []                                          );
$ffi->attach( notify_notification_new  => ['string', 'string', 'string']  => 'opaque' );
$ffi->attach( notify_notification_show => ['opaque', 'opaque']                        );

my $message = join "\n",
  "Hello from Platypus!",
  "Welcome to the fun",
  "world of FFI";

notify_init('Platypus Hello');
my $n = notify_notification_new('Platypus Hello World', $message, 'dialog-information');
notify_notification_show($n, undef);
notify_uninit();
```

### Execute

```
$ perl notify.pl
```

<div>
    <p>And this is what it will look like:</p>
    <div style="display: flex">
    <div style="margin: 3px; flex: 1 1 50%">
    <img alt="Test" src="/examples//notify.png">
    </div>
    </div>
</div>

### Discussion

The GNOME project provides an API to send notifications to its desktop environment.
Nothing here is particularly new: all of the types and techniques are ones that we
have seen before, except we are using a third party library, instead of using our
own C code or the standard C library functions.

When using a third party library you have to know the name or location of it, which
is not typically portable, so here we use [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib)'s
[find\_lib\_or\_die function](https://metacpan.org/pod/FFI::CheckLib#find_lib_or_die).  If the library is not
found the script will die with a useful diagnostic.  [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) has a number
of useful features and will integrate nicely with [Alien::Build](https://metacpan.org/pod/Alien::Build) based [Alien](https://metacpan.org/pod/Alien)s.

## The Win32 API with MessageBoxW

### Win32 API

[MessageBoxW function (winuser.h)](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw)

### Perl Source

```perl
use utf8;
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api  => 2,
  lib  => [undef],
);

# see FFI::Platypus::Lang::Win32
$ffi->lang('Win32');

# Send a Unicode string to the Windows API MessageBoxW function.
use constant MB_OK                   => 0x00000000;
use constant MB_DEFAULT_DESKTOP_ONLY => 0x00020000;
$ffi->attach( [MessageBoxW => 'MessageBox'] => [ 'HWND', 'LPCWSTR', 'LPCWSTR', 'UINT'] => 'int' );
MessageBox(undef, "I ❤️ Platypus", "Confession", MB_OK|MB_DEFAULT_DESKTOP_ONLY);
```

### Execute

```
$ perl win32_messagebox.pl
```

<div>
    <p>And this is what it will look like:</p>
    <div style="display: flex">
    <div style="margin: 3px; flex: 1 1 50%">
    <img alt="Test" src="/examples/win32_messagebox.png">
    </div>
    </div>
</div>

### Discussion

The API used by Microsoft Windows presents some unique
challenges.  On 32 bit systems a different ABI is used than what
is used by the standard C library.  It also provides a rats nest of
type aliases.  Finally if you want to talk Unicode to any of the
Windows API you will need to use `UTF-16LE` instead of `UTF-8`
which is native to Perl.  (The Win32 API refers to these as
`LPWSTR` and `LPCWSTR` types).  As much as possible the Win32
"language" plugin attempts to handle these challenges transparently.
For more details see [FFI::Platypus::Lang::Win32](https://metacpan.org/pod/FFI::Platypus::Lang::Win32).

### Discussion

The libnotify library is a desktop GUI notification system for the
GNOME Desktop environment. This script sends a notification event that
should show up as a balloon, for me it did so in the upper right hand
corner of my screen.

## Structured Data Records (by pointer or by reference)

### C API

[cppreference - localtime](https://en.cppreference.com/w/c/chrono/localtime)

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::C;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => [undef],
);
FFI::C->ffi($ffi);

package Unix::TimeStruct {

  FFI::C->struct(tm => [
    tm_sec    => 'int',
    tm_min    => 'int',
    tm_hour   => 'int',
    tm_mday   => 'int',
    tm_mon    => 'int',
    tm_year   => 'int',
    tm_wday   => 'int',
    tm_yday   => 'int',
    tm_isdst  => 'int',
    tm_gmtoff => 'long',
    _tm_zone  => 'opaque',
  ]);

  # For now 'string' is unsupported by FFI::C, but we
  # can cast the time zone from an opaque pointer to
  # string.
  sub tm_zone {
    my $self = shift;
    $ffi->cast('opaque', 'string', $self->_tm_zone);
  }

  # attach the C localtime function
  $ffi->attach( localtime => ['time_t*'] => 'tm', sub {
    my($inner, $class, $time) = @_;
    $time = time unless defined $time;
    $inner->(\$time);
  });
}

# now we can actually use our Unix::TimeStruct class
my $time = Unix::TimeStruct->localtime;
printf "time is %d:%d:%d %s\n",
  $time->tm_hour,
  $time->tm_min,
  $time->tm_sec,
  $time->tm_zone;
```

### Execute

```
$ perl time_struct.pl
time is 3:48:19 MDT
```

### Discussion

C and other machine code languages frequently provide interfaces that
include structured data records (defined using the `struct` keyword
in C).  Some libraries will provide an API which you are expected to read
or write before and/or after passing them along to the library.

For C pointers to `strict`, `union`, nested `struct` and nested
`union` structures, the easiest interface to use is via [FFI::C](https://metacpan.org/pod/FFI::C).
If you are working with a `struct` that must be passed by value
(not pointers), then you will want to use [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record)
class instead.  We will discuss an example of that next.

The C `localtime` function takes a pointer to a C struct.  We simply define
the members of the struct using the [FFI::C](https://metacpan.org/pod/FFI::C) `struct` method.  Because
we used the `ffi` method to tell [FFI::C](https://metacpan.org/pod/FFI::C) to use our local instance of
[FFI::Platypus](https://metacpan.org/pod/FFI::Platypus) it registers the `tm` type for us, and we can just start
using it as a return type!

## Structured Data Records (on stack or by value)

### C Source

```
#include <stdint.h>
#include <string.h>

typedef struct color_t {
   char    name[8];
   uint8_t red;
   uint8_t green;
   uint8_t blue;
} color_t;

color_t
color_increase_red(color_t color, uint8_t amount)
{
  strcpy(color.name, "reddish");
  color.red += amount;
  return color;
}
```

### Perl Source

```perl
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './color.so'
);

package Color {

  use FFI::Platypus::Record;
  use overload
    '""' => sub { shift->as_string },
    bool => sub { 1 }, fallback => 1;

  record_layout_1($ffi,
    'string(8)' => 'name', qw(
    uint8     red
    uint8     green
    uint8     blue
  ));

  sub as_string {
    my($self) = @_;
    sprintf "%s: [red:%02x green:%02x blue:%02x]",
      $self->name, $self->red, $self->green, $self->blue;
  }

}

$ffi->type('record(Color)' => 'color_t');
$ffi->attach( color_increase_red => ['color_t','uint8'] => 'color_t' );

my $gray = Color->new(
  name  => 'gray',
  red   => 0xDC,
  green => 0xDC,
  blue  => 0xDC,
);

my $slightly_red = color_increase_red($gray, 20);

print "$gray\n";
print "$slightly_red\n";
```

### Execute

```
$ cc -shared -o color.so color.c
$ perl color.pl
gray: [red:dc green:dc blue:dc]
reddish: [red:f0 green:dc blue:dc]
```

### Discussion

In the C source of this example, we pass a C `struct` by value by
copying it onto the stack.  On the Perl side we create a `Color` class
using [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record), which allows us to pass the structure
the way the C source wants us to.

Generally you should only reach for [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record) if you
need to pass small records on the stack like this.  For more complicated
(including nested) data you want to use [FFI::C](https://metacpan.org/pod/FFI::C) using pointers.

## Avoiding Copy Using Memory Windows (with libzmq3)

### C API

[ØMQ/3.2.6 API Reference](http://api.zeromq.org/3-2:_start)

### Perl Source

```perl
use constant ZMQ_IO_THREADS  => 1;
use constant ZMQ_MAX_SOCKETS => 2;
use constant ZMQ_REQ => 3;
use constant ZMQ_REP => 4;
use FFI::CheckLib qw( find_lib_or_die );
use FFI::Platypus 2.00;
use FFI::Platypus::Memory qw( malloc );
use FFI::Platypus::Buffer qw( scalar_to_buffer window );

my $endpoint = "ipc://zmq-ffi-$$";
my $ffi = FFI::Platypus->new(
  api => 2,
  lib => find_lib_or_die lib => 'zmq',
);

$ffi->attach(zmq_version => ['int*', 'int*', 'int*'] => 'void');

my($major,$minor,$patch);
zmq_version(\$major, \$minor, \$patch);
print "libzmq version $major.$minor.$patch\n";
die "this script only works with libzmq 3 or better" unless $major >= 3;

$ffi->type('opaque'       => 'zmq_context');
$ffi->type('opaque'       => 'zmq_socket');
$ffi->type('opaque'       => 'zmq_msg_t');
$ffi->attach(zmq_ctx_new  => [] => 'zmq_context');
$ffi->attach(zmq_ctx_set  => ['zmq_context', 'int', 'int'] => 'int');
$ffi->attach(zmq_socket   => ['zmq_context', 'int'] => 'zmq_socket');
$ffi->attach(zmq_connect  => ['opaque', 'string'] => 'int');
$ffi->attach(zmq_bind     => ['zmq_socket', 'string'] => 'int');
$ffi->attach(zmq_send     => ['zmq_socket', 'opaque', 'size_t', 'int'] => 'int');
$ffi->attach(zmq_msg_init => ['zmq_msg_t'] => 'int');
$ffi->attach(zmq_msg_recv => ['zmq_msg_t', 'zmq_socket', 'int'] => 'int');
$ffi->attach(zmq_msg_data => ['zmq_msg_t'] => 'opaque');
$ffi->attach(zmq_errno    => [] => 'int');
$ffi->attach(zmq_strerror => ['int'] => 'string');

my $context = zmq_ctx_new();
zmq_ctx_set($context, ZMQ_IO_THREADS, 1);

my $socket1 = zmq_socket($context, ZMQ_REQ);
zmq_connect($socket1, $endpoint);

my $socket2 = zmq_socket($context, ZMQ_REP);
zmq_bind($socket2, $endpoint);

{ # send
  our $sent_message = "hello there";
  my($pointer, $size) = scalar_to_buffer $sent_message;
  my $r = zmq_send($socket1, $pointer, $size, 0);
  die zmq_strerror(zmq_errno()) if $r == -1;
}

{ # recv
  my $msg_ptr  = malloc 100;
  zmq_msg_init($msg_ptr);
  my $size     = zmq_msg_recv($msg_ptr, $socket2, 0);
  die zmq_strerror(zmq_errno()) if $size == -1;
  my $data_ptr = zmq_msg_data($msg_ptr);
  window(my $recv_message, $data_ptr, $size);
  print "recv_message = $recv_message\n";
}
```

### Execute

```
$ perl zmq3.pl
libzmq version 4.3.4
recv_message = hello there
```

### Discussion

ØMQ is a high-performance asynchronous messaging library. There are a
few things to note here.

Firstly, sometimes there may be multiple versions of a library in the
wild and you may need to verify that the library on a system meets your
needs (alternatively you could support multiple versions and configure
your bindings dynamically).  Here we use `zmq_version` to ask libzmq
which version it is.

`zmq_version` returns the version number via three integer pointer
arguments, so we use the pointer to integer type: `int *`.  In order to
pass pointer types, we pass a reference. In this case it is a reference
to an undefined value, because zmq\_version will write into the pointers
the output values, but you can also pass in references to integers,
floating point values and opaque pointer types.  When the function
returns the `$major` variable (and the others) has been updated and we
can use it to verify that it supports the API that we require.

Finally we attach the necessary functions, send and receive a message.
When we receive we use the [FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer) function `window`
instead of `buffer_to_scalar`.  They have a similar effect in that
the provide a scalar from a region of memory, but `window` doesn't
have to copy any data, so it is cheaper to call.  The only downside
is that a windowed scalar like this is read-only.

## libarchive

### C Documentation

[https://www.libarchive.org/](https://www.libarchive.org/)

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::CheckLib qw( find_lib_or_die );

# This example uses FreeBSD's libarchive to list the contents of any
# archive format that it suppors.  We've also filled out a part of
# the ArchiveWrite class that could be used for writing archive formats
# supported by libarchive

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => find_lib_or_die(lib => 'archive'),
);
$ffi->type('object(Archive)'      => 'archive_t');
$ffi->type('object(ArchiveRead)'  => 'archive_read_t');
$ffi->type('object(ArchiveWrite)' => 'archive_write_t');
$ffi->type('object(ArchiveEntry)' => 'archive_entry_t');

package Archive {
  # base class is "abstract" having no constructor or destructor

  $ffi->mangler(sub {
    my($name) = @_;
    "archive_$name";
  });
  $ffi->attach( error_string => ['archive_t'] => 'string' );
}

package ArchiveRead {
  our @ISA = qw( Archive );

  $ffi->mangler(sub {
    my($name) = @_;
    "archive_read_$name";
  });

  $ffi->attach( new                   => ['string']                        => 'archive_read_t' );
  $ffi->attach( [ free => 'DESTROY' ] => ['archive_t']                                         );
  $ffi->attach( support_filter_all    => ['archive_t']                     => 'int'            );
  $ffi->attach( support_format_all    => ['archive_t']                     => 'int'            );
  $ffi->attach( open_filename         => ['archive_t','string','size_t']   => 'int'            );
  $ffi->attach( next_header2          => ['archive_t', 'archive_entry_t' ] => 'int'            );
  $ffi->attach( data_skip             => ['archive_t']                     => 'int'            );
  # ... define additional read methods
}

package ArchiveWrite {

  our @ISA = qw( Archive );

  $ffi->mangler(sub {
    my($name) = @_;
    "archive_write_$name";
  });

  $ffi->attach( new                   => ['string'] => 'archive_write_t' );
  $ffi->attach( [ free => 'DESTROY' ] => ['archive_write_t'] );
  # ... define additional write methods
}

package ArchiveEntry {

  $ffi->mangler(sub {
    my($name) = @_;
    "archive_entry_$name";
  });

  $ffi->attach( new => ['string']     => 'archive_entry_t' );
  $ffi->attach( [ free => 'DESTROY' ] => ['archive_entry_t'] );
  $ffi->attach( pathname              => ['archive_entry_t'] => 'string' );
  # ... define additional entry methods
}

use constant ARCHIVE_OK => 0;

# this is a Perl version of the C code here:
# https://github.com/libarchive/libarchive/wiki/Examples#List_contents_of_Archive_stored_in_File

my $archive_filename = shift @ARGV;
unless(defined $archive_filename)
{
  print "usage: $0 archive.tar\n";
  exit;
}

my $archive = ArchiveRead->new;
$archive->support_filter_all;
$archive->support_format_all;

my $r = $archive->open_filename($archive_filename, 1024);
die "error opening $archive_filename: ", $archive->error_string
  unless $r == ARCHIVE_OK;

my $entry = ArchiveEntry->new;

while($archive->next_header2($entry) == ARCHIVE_OK)
{
  print $entry->pathname, "\n";
  $archive->data_skip;
}
```

### Execute

```
$ perl archive_object.pl archive.tar
archive.pl
archive_object.pl
```

### Discussion

libarchive is the implementation of `tar` for FreeBSD provided as a
library and available on a number of platforms.

One interesting thing about libarchive is that it provides a kind of
object oriented interface via opaque pointers.  This example creates an
abstract class `Archive`, and concrete classes `ArchiveWrite`,
`ArchiveRead` and `ArchiveEntry`.  The concrete classes can even be
inherited from and extended just like any Perl classes because of the
way the custom types are implemented.  We use Platypus's `object`
type for this implementation, which is a wrapper around an `opaque`
(can also be an integer) type that is blessed into a particular class.

Another advanced feature of this example is that we define a mangler
to modify the symbol resolution for each class.  This means we can do
this when we define a method for Archive:

```perl
$ffi->attach( support_filter_all => ['archive_t'] => 'int' );
```

Rather than this:

```perl
$ffi->attach(
  [ archive_read_support_filter_all => 'support_read_filter_all' ] =>
  ['archive_t'] => 'int' );
);
```

As nice as `libarchive` is, note that we have to shoehorn then
`archive_free` function name into the Perl convention of using
`DESTROY` as the destructor.  We can easily do that for just this
one function with:

```perl
$ffi->attach( [ free => 'DESTROY' ] => ['archive_t'] );
```

The `libarchive` is a large library with hundreds of methods.
For comprehensive FFI bindings for `libarchive` see [Archive::Libarchive](https://metacpan.org/pod/Archive::Libarchive).

## unix open

### C API

[Input-output system calls in C](https://www.geeksforgeeks.org/input-output-system-calls-c-create-open-close-read-write/)

### Perl Source

```perl
use FFI::Platypus 2.00;

{
  package FD;

  use constant O_RDONLY => 0;
  use constant O_WRONLY => 1;
  use constant O_RDWR   => 2;

  use constant IN  => bless \do { my $in=0  }, __PACKAGE__;
  use constant OUT => bless \do { my $out=1 }, __PACKAGE__;
  use constant ERR => bless \do { my $err=2 }, __PACKAGE__;

  my $ffi = FFI::Platypus->new( api => 2, lib => [undef]);

  $ffi->type('object(FD,int)' => 'fd');

  $ffi->attach( [ 'open' => 'new' ] => [ 'string', 'int', 'mode_t' ] => 'fd' => sub {
    my($xsub, $class, $fn, @rest) = @_;
    my $fd = $xsub->($fn, @rest);
    die "error opening $fn $!" if $$fd == -1;
    $fd;
  });

  $ffi->attach( write => ['fd', 'string', 'size_t' ] => 'ssize_t' );
  $ffi->attach( read  => ['fd', 'string', 'size_t' ] => 'ssize_t' );
  $ffi->attach( close => ['fd'] => 'int' );
}

my $fd = FD->new("file_handle.txt", FD::O_RDONLY);

my $buffer = "\0" x 10;

while(my $br = $fd->read($buffer, 10))
{
  FD::OUT->write($buffer, $br);
}

$fd->close;
```

### Execute

```
$ perl file_handle.pl
Hello World
```

### Discussion

The Unix file system calls use an integer handle for each open file.
We can use the same `object` type that we used for libarchive above,
except we let platypus know that the underlying type is `int` instead
of `opaque` (the latter being the default for the `object` type).
Mainly just for demonstration since Perl has much better IO libraries,
but now we have an OO interface to the Unix IO functions.

## Varadic Functions (with libcurl)

### C API

- [curl\_easy\_init](https://curl.se/libcurl/c/curl_easy_init.html)
- [curl\_easy\_setopt](https://curl.se/libcurl/c/curl_easy_setopt.html)
- [curl\_easy\_perform](https://curl.se/libcurl/c/curl_easy_perform.html)
- [curl\_easy\_cleanup](https://curl.se/libcurl/c/curl_easy_cleanup.html)
- [CURLOPT\_URL](https://curl.se/libcurl/c/CURLOPT_URL.html)

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::CheckLib qw( find_lib_or_die );
use constant CURLOPT_URL => 10002;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => find_lib_or_die(lib => 'curl'),
);

my $curl_handle = $ffi->function( 'curl_easy_init' => [] => 'opaque' )
                      ->call;

$ffi->function( 'curl_easy_setopt' => ['opaque', 'enum' ] => ['string'] )
    ->call($curl_handle, CURLOPT_URL, "https://pl.atypus.org" );

$ffi->function( 'curl_easy_perform' => ['opaque' ] => 'enum' )
    ->call($curl_handle);

$ffi->function( 'curl_easy_cleanup' => ['opaque' ] )
    ->call($curl_handle);
```

### Execute

```
$ perl curl.pl
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>pl.atypus.org - Home for the Perl Platypus Project</title>
...
```

### Discussion

The `libcurl` library makes extensive use of "varadic" functions.

The C programming language and ABI have the concept of "varadic" functions
that can take a variable number and variable type of arguments.  Assuming
you have a `libffi` that supports it (and most modern systems should),
then you can create bindings to a varadic function by providing two sets
of array references, one for the fixed arguments (for reasons, C varadic
functions must have at least one) and one for variable arguments.  In
this example we call `curl_easy_setopt` as a varadic function.

For functions that have a large or infinite number of possible signatures
it may be impracticable or impossible to attach them all.  You can instead
do as we did in this example, create a function object using the
[function method](#function) and call it immediately.  This is not as
performant either when you create or call as using the [attach method](#attach),
but in some cases the performance penalty may be worth it or unavoidable.

## Callbacks (with libcurl>

### C API

- [curl\_easy\_init](https://curl.se/libcurl/c/curl_easy_init.html)
- [curl\_easy\_setopt](https://curl.se/libcurl/c/curl_easy_setopt.html)
- [curl\_easy\_perform](https://curl.se/libcurl/c/curl_easy_perform.html)
- [curl\_easy\_cleanup](https://curl.se/libcurl/c/curl_easy_cleanup.html)
- [CURLOPT\_URL](https://curl.se/libcurl/c/CURLOPT_URL.html)
- [CURLOPT\_WRITEFUNCTION](https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html)

### Perl Source

```perl
use FFI::Platypus 2.00;
use FFI::CheckLib qw( find_lib_or_die );
use FFI::Platypus::Buffer qw( window );
use constant CURLOPT_URL           => 10002;
use constant CURLOPT_WRITEFUNCTION => 20011;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => find_lib_or_die(lib => 'curl'),
);

my $curl_handle = $ffi->function( 'curl_easy_init' => [] => 'opaque' )
                      ->call;

$ffi->function( 'curl_easy_setopt' => [ 'opaque', 'enum' ] => ['string'] )
    ->call($curl_handle, CURLOPT_URL, "https://pl.atypus.org" );

my $html;

my $closure = $ffi->closure(sub {
  my($ptr, $len, $num, $user) = @_;
  window(my $buf, $ptr, $len*$num);
  $html .= $buf;
  return $len*$num;
});

$ffi->function( 'curl_easy_setopt' => [ 'opaque', 'enum' ] => ['(opaque,size_t,size_t,opaque)->size_t'] => 'enum' )
    ->call($curl_handle, CURLOPT_WRITEFUNCTION, $closure);

$ffi->function( 'curl_easy_perform' => [ 'opaque' ] => 'enum' )
    ->call($curl_handle);

$ffi->function( 'curl_easy_cleanup' => [ 'opaque' ] )
    ->call($curl_handle);

if($html =~ /<title>(.*?)<\/title>/) {
  print "$1\n";
}
```

### Execute

```
$ perl curl_callback.pl
pl.atypus.org - Home for the Perl Platypus Project
```

### Discussion

This example is similar to the previous one, except instead of letting
[libcurl](https://curl.se) write the content body to `STDOUT`, we give
it a callback to send the data to instead.  The [closure method](#closure)
can be used to create a callback function pointer that can be called from
C.  The type for the callback is in the form `(arg_type,arg_type,etc)->return_type`
where the argument types are in parentheticals with an arrow between the
argument types and the return type.

Inside the closure or callback we use the [window function](https://metacpan.org/pod/FFI::Platypus::Buffer#window)
from [FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer) again to avoid an _extra_ copy.  We still
have to copy the buffer to append it to `$hmtl` but it is at least one
less copy.

## bundle your own code

### C Source

`ffi/foo.c`:

```
#include <ffi_platypus_bundle.h>
#include <string.h>

typedef struct {
  char *name;
  int value;
} foo_t;

foo_t*
foo__new(const char *class_name, const char *name, int value) {
  (void)class_name;
  foo_t *self = malloc( sizeof( foo_t ) );
  self->name = strdup(name);
  self->value = value;
  return self;
}

const char *
foo__name(foo_t *self) {
  return self->name;
}

int
foo__value(foo_t *self) {
  return self->value;
}

void
foo__DESTROY(foo_t *self) {
  free(self->name);
  free(self);
}
```

### Perl Source

`lib/Foo.pm`:

```perl
package Foo;

use strict;
use warnings;
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new( api => 2 );

$ffi->type('object(Foo)' => 'foo_t');
$ffi->mangler(sub {
  my $name = shift;
  $name =~ s/^/foo__/;
  $name;
});

$ffi->bundle;

$ffi->attach( new =>     [ 'string', 'string', 'int' ] => 'foo_t'  );
$ffi->attach( name =>    [ 'foo_t' ]                   => 'string' );
$ffi->attach( value =>   [ 'foo_t' ]                   => 'int'    );
$ffi->attach( DESTROY => [ 'foo_t' ]                   => 'void'   );

1;
```

`t/foo.t`:

```perl
use Test2::V0;
use Foo;

my $foo = Foo->new("platypus", 10);
isa_ok $foo, 'Foo';
is $foo->name, "platypus";
is $foo->value, 10;

done_testing;
```

`Makefile.PL`:

```perl
use ExtUtils::MakeMaker;
use FFI::Build::MM;
my $fbmm = FFI::Build::MM->new;
WriteMakefile(
  $fbmm->mm_args(
    NAME     => 'Foo',
    DISTNAME => 'Foo',
    VERSION  => '1.00',
    # ...
  )
);

sub MY::postamble
{
  $fbmm->mm_postamble;
}
```

### Execute

With prove:

```
$ prove -lvm
t/foo.t ..
# Seeded srand with seed '20221105' from local date.
ok 1 - Foo=SCALAR->isa('Foo')
ok 2
ok 3
1..3
ok
All tests successful.
Files=1, Tests=3,  0 wallclock secs ( 0.00 usr  0.00 sys +  0.10 cusr  0.00 csys =  0.10 CPU)
Result: PASS
```

With [ExtUtils::MakeMaker](https://metacpan.org/pod/ExtUtils::MakeMaker):

```
$ perl Makefile.PL
Generating a Unix-style Makefile
Writing Makefile for Foo
Writing MYMETA.yml and MYMETA.json
$ make
cp lib/Foo.pm blib/lib/Foo.pm
"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5" -MFFI::Build::MM=cmd -e fbx_build
CC ffi/foo.c
LD blib/lib/auto/share/dist/Foo/lib/libFoo.so
$ make test
"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5" -MFFI::Build::MM=cmd -e fbx_build
"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5" -MFFI::Build::MM=cmd -e fbx_test
PERL_DL_NONLAZY=1 "/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/foo.t .. ok
All tests successful.
Files=1, Tests=3,  1 wallclock secs ( 0.00 usr  0.00 sys +  0.03 cusr  0.00 csys =  0.03 CPU)
Result: PASS
```

### Discussion

You can bundle your own C code with your Perl extension.  There are a number
of reasons you might want to do this  Sometimes you need to optimize a
tight loop for speed.  Or you might need a little bit of glue code for your
bindings to a library that isn't inherently FFI friendly.  Either way
what you want is the [FFI::Build](https://metacpan.org/pod/FFI::Build) system on the install step and the
[FFI::Platypus::Bundle](https://metacpan.org/pod/FFI::Platypus::Bundle) interface on the runtime step.  If you are using
[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla) for your distribution, you will also want to check out the
[Dist::Zilla::Plugin::FFI::Build](https://metacpan.org/pod/Dist::Zilla::Plugin::FFI::Build) plugin to make this as painless as possible.

One of the nice things about the bundle interface is that it is smart enough to
work with either [App::Prove](https://metacpan.org/pod/App::Prove) or [ExtUtils::MakeMaker](https://metacpan.org/pod/ExtUtils::MakeMaker).  This means, unlike
XS, you do not need to explicitly compile your C code in development mode, that
will be done for you when you call `$ffi->bundle`

# FAQ

## How do I get constants defined as macros in C header files

This turns out to be a challenge for any language calling into C, which
frequently uses `#define` macros to define constants like so:

```
#define FOO_STATIC  1
#define FOO_DYNAMIC 2
#define FOO_OTHER   3
```

As macros are expanded and their definitions are thrown away by the C pre-processor
there isn't any way to get the name/value mappings from the compiled dynamic
library.

You can manually create equivalent constants in your Perl source:

```perl
use constant FOO_STATIC  => 1;
use constant FOO_DYNAMIC => 2;
use constant FOO_OTHER   => 3;
```

If there are a lot of these types of constants you might want to consider using
a tool ([Convert::Binary::C](https://metacpan.org/pod/Convert::Binary::C) can do this) that can extract the constants for you.

See also the "Integer constants" example in [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type).

You can also use the new Platypus bundle interface to define Perl constants
from C space.  This is more reliable, but does require a compiler at install
time.  It is recommended mainly for writing bindings against libraries that
have constants that can vary widely from platform to platform.  See
[FFI::Platypus::Constant](https://metacpan.org/pod/FFI::Platypus::Constant) for details.

## What about enums?

The C enum types are integers.  The underlying type is up to the platform, so
Platypus provides `enum` and `senum` types for unsigned and singed enums
respectively.  At least some compilers treat signed and unsigned enums as
different types.  The enum _values_ are essentially the same as macro constants
described above from an FFI perspective.  Thus the process of defining enum values
is identical to the process of defining macro constants in Perl.

For more details on enumerated types see ["Enum types" in FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type#Enum-types).

There is also a type plugin ([FFI::Platypus::Type::Enum](https://metacpan.org/pod/FFI::Platypus::Type::Enum)) that can be helpful
in writing interfaces that use enums.

## Memory leaks

There are a couple places where memory is allocated, but never deallocated that may
look like memory leaks by tools designed to find memory leaks like valgrind.  This
memory is intended to be used for the lifetime of the perl process so there normally
this isn't a problem unless you are embedding a Perl interpreter which doesn't closely
match the lifetime of your overall application.

Specifically:

- type cache

    some types are cached and not freed.  These are needed as long as there are FFI
    functions that could be called.

- attached functions

    Attaching a function as an xsub will definitely allocate memory that won't be freed
    because the xsub could be called at any time, including in `END` blocks.

The Platypus team plans on adding a hook to free some of this "leaked" memory
for use cases where Perl and Platypus are embedded in a larger application
where the lifetime of the Perl process is significantly smaller than the
overall lifetime of the whole process.

## I get seg faults on some platforms but not others with a library using pthreads.

On some platforms, Perl isn't linked with `libpthreads` if Perl threads are not
enabled.  On some platforms this doesn't seem to matter, `libpthreads` can be
loaded at runtime without much ill-effect.  (Linux from my experience doesn't seem
to mind one way or the other).  Some platforms are not happy about this, and about
the only thing that you can do about it is to build Perl such that it links with
`libpthreads` even if it isn't a threaded Perl.

This is not really an FFI issue, but a Perl issue, as you will have the same
problem writing XS code for the such libraries.

## Doesn't work on Perl 5.10.0.

The first point release of Perl 5.10 was buggy, and is not supported by Platypus.
Please upgrade to a newer Perl.

# CAVEATS

Platypus and Native Interfaces like libffi rely on the availability of
dynamic libraries.  Things not supported include:

- Systems that lack dynamic library support

    Like MS-DOS

- Systems that are not supported by libffi

    Like OpenVMS

- Languages that do not support using dynamic libraries from other languages

    This used to be the case with Google's Go, but is no longer the case.  This is
    a problem for C / XS code as well.

- Languages that do not compile to machine code

    Like .NET based languages and Java.

The documentation has a bias toward using FFI / Platypus with C.  This
is my fault, as my background in mainly in C/C++ programmer (when I am
not writing Perl).  In many places I use "C" as a short form for "any
language that can generate machine code and is callable from C".  I
welcome pull requests to the Platypus core to address this issue.  In an
attempt to ease usage of Platypus by non C programmers, I have written a
number of foreign language plugins for various popular languages (see
the SEE ALSO below).  These plugins come with examples specific to those
languages, and documentation on common issues related to using those
languages with FFI.  In most cases these are available for easy adoption
for those with the know-how or the willingness to learn.  If your
language doesn't have a plugin YET, that is just because you haven't
written it yet.

# SUPPORT

IRC: #native on irc.perl.org

[(click for instant chat room login)](http://chat.mibbit.com/#native@irc.perl.org)

If something does not work the way you think it should, or if you have a
feature request, please open an issue on this project's GitHub Issue
tracker:

[https://github.com/perlFFI/FFI-Platypus/issues](https://github.com/perlFFI/FFI-Platypus/issues)

# CONTRIBUTING

If you have implemented a new feature or fixed a bug then you may make a
pull request on this project's GitHub repository:

[https://github.com/PerlFFI/FFI-Platypus/pulls](https://github.com/PerlFFI/FFI-Platypus/pulls)

This project is developed using [Dist::Zilla](https://metacpan.org/pod/Dist::Zilla).  The project's git
repository also comes with the `Makefile.PL` file necessary
for building, testing (and even installing if necessary) without
[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla).  Please keep in mind though that these files are
generated so if changes need to be made to those files they should be
done through the project's `dist.ini` file.  If you do use
[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla) and already have the necessary plugins installed, then I
encourage you to run `dzil test` before making any pull requests.  This
is not a requirement, however, I am happy to integrate especially
smaller patches that need tweaking to fit the project standards.  I may
push back and ask you to write a test case or alter the formatting of a
patch depending on the amount of time I have and the amount of code that
your patch touches.

This project's GitHub issue tracker listed above is not Write-Only.  If
you want to contribute then feel free to browse through the existing
issues and see if there is something you feel you might be good at and
take a whack at the problem.  I frequently open issues myself that I
hope will be accomplished by someone in the future but do not have time
to immediately implement myself.

Another good area to help out in is documentation.  I try to make sure
that there is good document coverage, that is there should be
documentation describing all the public features and warnings about
common pitfalls, but an outsider's or alternate view point on such
things would be welcome; if you see something confusing or lacks
sufficient detail I encourage documentation only pull requests to
improve things.

The Platypus distribution comes with a test library named `libtest`
that is normally automatically built by `./Build test`.  If you prefer
to use `prove` or run tests directly, you can use the `./Build
libtest` command to build it.  Example:

```
% perl Makefile.PL
% make
% make ffi-test
% prove -bv t
# or an individual test
% perl -Mblib t/ffi_platypus_memory.t
```

The build process also respects these environment variables:

- FFI\_PLATYPUS\_DEBUG\_FAKE32

    When building Platypus on 32 bit Perls, it will use the [Math::Int64](https://metacpan.org/pod/Math::Int64) C
    API and make [Math::Int64](https://metacpan.org/pod/Math::Int64) a prerequisite.  Setting this environment
    variable will force Platypus to build with both of those options on a 64
    bit Perl as well.

    ```
    % env FFI_PLATYPUS_DEBUG_FAKE32=1 perl Makefile.PL
    DEBUG_FAKE32:
      + making Math::Int64 a prereq
      + Using Math::Int64's C API to manipulate 64 bit values
    Generating a Unix-style Makefile
    Writing Makefile for FFI::Platypus
    Writing MYMETA.yml and MYMETA.json
    %
    ```

- FFI\_PLATYPUS\_NO\_ALLOCA

    Platypus uses the non-standard and somewhat controversial C function
    `alloca` by default on platforms that support it.  I believe that
    Platypus uses it responsibly to allocate small amounts of memory for
    argument type parameters, and does not use it to allocate large
    structures like arrays or buffers.  If you prefer not to use `alloca`
    despite these precautions, then you can turn its use off by setting this
    environment variable when you run `Makefile.PL`:

    ```perl
    helix% env FFI_PLATYPUS_NO_ALLOCA=1 perl Makefile.PL
    NO_ALLOCA:
      + alloca() will not be used, even if your platform supports it.
    Generating a Unix-style Makefile
    Writing Makefile for FFI::Platypus
    Writing MYMETA.yml and MYMETA.json
    ```

- V

    When building platypus may hide some of the excessive output when
    probing and building, unless you set `V` to a true value.

    ```
    % env V=1 perl Makefile.PL
    % make V=1
    ...
    ```

## Coding Guidelines

- Do not hesitate to make code contribution.  Making useful contributions
is more important than following byzantine bureaucratic coding
regulations.  We can always tweak things later.
- Please make an effort to follow existing coding style when making pull
requests.
- Platypus supports all production Perl releases since 5.8.1.  For that
reason, please do not introduce any code that requires a newer version
of Perl.

## Performance Testing

As Mark Twain was fond of saying there are four types of lies: lies,
damn lies, statistics and benchmarks.  That being said, it can sometimes
be helpful to compare the runtime performance of Platypus if you are
making significant changes to the Platypus Core.  For that I use
\`FFI-Performance\`, which can be found in my GitHub repository here:

- [https://github.com/Perl5-FFI/FFI-Performance](https://github.com/Perl5-FFI/FFI-Performance)

## System integrators

This distribution uses [Alien::FFI](https://metacpan.org/pod/Alien::FFI) in fallback mode, meaning if
the system doesn't provide `pkg-config` and `libffi` it will attempt
to download `libffi` and build it from source.  If you are including
Platypus in a larger system (for example a Linux distribution) you
only need to make sure to declare `pkg-config` or `pkgconf` and
the development package for `libffi` as prereqs for this module.

# SEE ALSO

## Extending Platypus

- [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type)

    Type definitions for Platypus.

- [FFI::C](https://metacpan.org/pod/FFI::C)

    Interface for defining structured data records for use with
    Platypus.  It supports C `struct`, `union`, nested structures
    and arrays of all of those.  It only supports passing these
    types by reference or pointer, so if you need to pass structured
    data by value see [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record) below.

- [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record)

    Interface for defining structured data records for use with
    Platypus.  Included in the Platypus core.  Supports pass by
    value which is uncommon in C, but frequently used in languages
    like Rust and Go.  Consider using [FFI::C](https://metacpan.org/pod/FFI::C) instead if you
    don't need to pass by value.

- [FFI::Platypus::API](https://metacpan.org/pod/FFI::Platypus::API)

    The custom types API for Platypus.

- [FFI::Platypus::Memory](https://metacpan.org/pod/FFI::Platypus::Memory)

    Memory functions for FFI.

## Languages

- [FFI::Platypus::Lang::C](https://metacpan.org/pod/FFI::Platypus::Lang::C)

    Documentation and tools for using Platypus with the C programming
    language

- [FFI::Platypus::Lang::CPP](https://metacpan.org/pod/FFI::Platypus::Lang::CPP)

    Documentation and tools for using Platypus with the C++ programming
    language

- [FFI::Platypus::Lang::Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran)

    Documentation and tools for using Platypus with Fortran

- [FFI::Platypus::Lang::Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go)

    Documentation and tools for using Platypus with Go

- [FFI::Platypus::Lang::Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal)

    Documentation and tools for using Platypus with Free Pascal

- [FFI::Platypus::Lang::Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust)

    Documentation and tools for using Platypus with the Rust programming
    language

- [FFI::Platypus::Lang::ASM](https://metacpan.org/pod/FFI::Platypus::Lang::ASM)

    Documentation and tools for using Platypus with the Assembly

- [FFI::Platypus::Lang::Win32](https://metacpan.org/pod/FFI::Platypus::Lang::Win32)

    Documentation and tools for using Platypus with the Win32 API.

- [FFI::Platypus::Lang::Zig](https://metacpan.org/pod/FFI::Platypus::Lang::Zig)

    Documentation and tools for using Platypus with the Zig programming
    language

- [Wasm](https://metacpan.org/pod/Wasm) and [Wasm::Wasmtime](https://metacpan.org/pod/Wasm::Wasmtime)

    Modules for writing WebAssembly bindings in Perl.  This allows you to call
    functions written in any language supported by WebAssembly.  These modules
    are also implemented using Platypus.

## Other Tools Related Tools Useful for FFI

- [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib)

    Find dynamic libraries in a portable way.

- [Convert::Binary::C](https://metacpan.org/pod/Convert::Binary::C)

    A great interface for decoding C data structures, including `struct`s,
    `enum`s, `#define`s and more.

- [pack and unpack](https://metacpan.org/pod/perlpacktut)

    Native to Perl functions that can be used to decode C `struct` types.

- [C::Scan](https://metacpan.org/pod/C::Scan)

    This module can extract constants and other useful objects from C header
    files that may be relevant to an FFI application.  One downside is that
    its use may require development packages to be installed.

## Other Foreign Function Interfaces

- [Dyn](https://metacpan.org/pod/Dyn)

    A wrapper around [dyncall](https://dyncall.org), which is itself an alternative to
    [libffi](https://sourceware.org/libffi/).

- [NativeCall](https://metacpan.org/pod/NativeCall)

    Promising interface to Platypus inspired by Raku.

- [Win32::API](https://metacpan.org/pod/Win32::API)

    Microsoft Windows specific FFI style interface.

- [FFI](https://metacpan.org/pod/FFI)

    Older, simpler, less featureful FFI.  It used to be implemented
    using FSF's `ffcall`.  Because `ffcall` has been unsupported for
    some time, I reimplemented this module using [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).

- [C::DynaLib](https://metacpan.org/pod/C::DynaLib)

    Another FFI for Perl that doesn't appear to have worked for a long time.

- [C::Blocks](https://metacpan.org/pod/C::Blocks)

    Embed a tiny C compiler into your Perl scripts.

- [P5NCI](https://metacpan.org/pod/P5NCI)

    Yet another FFI like interface that does not appear to be supported or
    under development anymore.

## Other

- [Alien::FFI](https://metacpan.org/pod/Alien::FFI)

    Provides libffi for Platypus during its configuration and build stages.

# ACKNOWLEDGMENTS

In addition to the contributors mentioned below, I would like to
acknowledge Brock Wilcox (AWWAIID) and Meredith Howard (MHOWARD) whose
work on `FFI::Sweet` not only helped me get started with FFI but
significantly influenced the design of Platypus.

Dan Book, who goes by Grinnz on IRC for answering user questions about
FFI and Platypus.

In addition I'd like to thank Alessandro Ghedini (ALEXBIO) whose work
on another Perl FFI library helped drive some of the development ideas
for [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).

# AUTHOR

Author: Graham Ollis <plicease@cpan.org>

Contributors:

Bakkiaraj Murugesan (bakkiaraj)

Dylan Cali (calid)

pipcet

Zaki Mughal (zmughal)

Fitz Elliott (felliott)

Vickenty Fesunov (vyf)

Gregor Herrmann (gregoa)

Shlomi Fish (shlomif)

Damyan Ivanov

Ilya Pavlov (Ilya33)

Petr Písař (ppisar)

Mohammad S Anwar (MANWAR)

Håkon Hægland (hakonhagland, HAKONH)

Meredith (merrilymeredith, MHOWARD)

Diab Jerius (DJERIUS)

Eric Brine (IKEGAMI)

szTheory

José Joaquín Atria (JJATRIA)

Pete Houston (openstrike, HOUSTON)

# COPYRIGHT AND LICENSE

This software is copyright (c) 2015-2022 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.