/usr/lib/x86_64-linux-gnu/perl5/5.24/FFI/Platypus.pm is in libffi-platypus-perl 0.45-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 | package FFI::Platypus;
use strict;
use warnings;
use 5.008001;
use Carp qw( croak );
# ABSTRACT: Write Perl bindings to non-Perl libraries with FFI. No XS required.
our $VERSION = '0.45'; # VERSION
# Platypus Man,
# Platypus Man,
# Does Everything The Platypus Can
# ...
# Watch Out!
# Here Comes The Platypus Man
# From the original FFI::Platypus prototype:
# Kinda like gluing a duckbill to an adorable mammal
our @CARP_NOT = qw( FFI::Platypus::Declare );
require XSLoader;
XSLoader::load(
'FFI::Platypus', eval q{ $VERSION } || do {
# this is for testing without dzil
# it expects MYMETA.json for FFI::Platypus
# to be in the current working directory.
require JSON::PP;
my $fh;
open($fh, '<', 'MYMETA.json') || die "unable to read MYMETA.json";
my $config = JSON::PP::decode_json(do { local $/; <$fh> });
close $fh;
$config->{version};
}
);
sub new
{
my($class, %args) = @_;
my @lib;
if(defined $args{lib})
{
if(!ref($args{lib}))
{
push @lib, $args{lib};
}
elsif(ref($args{lib}) eq 'ARRAY')
{
push @lib, @{$args{lib}};
}
else
{
croak "lib argument must be a scalar or array reference";
}
}
bless {
lib => \@lib,
handles => {},
types => {},
lang => $args{lang} || 'C',
abi => -1,
ignore_not_found => defined $args{ignore_not_found} ? $args{ignore_not_found} : 0,
}, $class;
}
sub _lang_class ($)
{
my($lang) = @_;
my $class = "FFI::Platypus::Lang::$lang";
unless($class->can('native_type_map'))
{
eval qq{ use $class };
croak "unable to load $class: $@" if $@;
}
croak "$class does not provide native_type_map method"
unless $class->can("native_type_map");
$class;
}
sub _type_map
{
my($self) = @_;
unless(defined $self->{type_map})
{
my $class = _lang_class($self->{lang});
my %type_map;
foreach my $key (keys %{ $class->native_type_map })
{
my $value = $class->native_type_map->{$key};
next unless _have_type($value);
$type_map{$key} = $value;
}
# include the standard libffi types
$type_map{$_} = $_ for grep { _have_type($_) }
qw( void sint8 uint8 sint16 uint16 sint32 uint32 sint64 uint64 float double string opaque
longdouble complex_float complex_double );
$type_map{pointer} = 'opaque';
$self->{type_map} = \%type_map;
}
$self->{type_map};
}
sub lib
{
my($self, @new) = @_;
if(@new)
{
push @{ $self->{lib} }, @new;
delete $self->{mangler};
}
@{ $self->{lib} };
}
sub ignore_not_found
{
my($self, $value) = @_;
if(defined $value)
{
$self->{ignore_not_found} = $value;
}
$self->{ignore_not_found};
}
sub lang
{
my($self, $value) = @_;
if(defined $value)
{
$self->{lang} = $value;
delete $self->{type_map};
my $class = _lang_class($self->{lang});
$self->abi($class->abi) if $class->can('abi');
}
$self->{lang};
}
sub type
{
my($self, $name, $alias) = @_;
croak "usage: \$ffi->type(name => alias) (alias is optional)" unless defined $self && defined $name;
croak "spaces not allowed in alias" if defined $alias && $alias =~ /\s/;
croak "allowed characters for alias: [A-Za-z0-9_]+" if defined $alias && $alias =~ /[^A-Za-z0-9_]/;
my $type_map = $self->_type_map;
croak "alias conflicts with existing type" if defined $alias && (defined $type_map->{$alias} || defined $self->{types}->{$alias});
if($name =~ /-\>/ || $name =~ /^record\s*\([0-9A-Z:a-z_]+\)$/
|| $name =~ /^string(_rw|_ro|\s+rw|\s+ro|\s*\([0-9]+\))$/)
{
# for closure and record types we do not try to convet into the
# basic type so you can have many many many copies of a given
# closure type if you do not spell it exactly the same each time.
# Recommended that you use an alias for a closure type anyway.
$self->{types}->{$name} ||= FFI::Platypus::Type->new($name, $self);
}
else
{
my $basic = $name;
my $extra = '';
if($basic =~ s/\s*((\*|\[|\<).*)$//)
{
$extra = " $1";
}
croak "unknown type: $basic" unless defined $type_map->{$basic};
$self->{types}->{$name} = $self->{types}->{$type_map->{$basic}.$extra} ||= FFI::Platypus::Type->new($type_map->{$basic}.$extra, $self);
}
if(defined $alias)
{
$self->{types}->{$alias} = $self->{types}->{$name};
}
$self;
}
sub custom_type
{
my($self, $name, $cb) = @_;
my $type = $cb->{native_type};
$type ||= 'opaque';
my $argument_count = $cb->{argument_count} || 1;
croak "argument_count must be >= 1"
unless $argument_count >= 1;
croak "Usage: \$ffi->custom_type(\$name, { ... })"
unless defined $name && ref($cb) eq 'HASH';
croak "must define at least one of native_to_perl, perl_to_native, or perl_to_native_post"
unless defined $cb->{native_to_perl} || defined $cb->{perl_to_native} || defined $cb->{perl_to_native_post};
my $type_map = $self->_type_map;
croak "$type is not a native type" unless defined $type_map->{$type} || $type eq 'string';
croak "name conflicts with existing type" if defined $type_map->{$name} || defined $self->{types}->{$name};
$self->{types}->{$name} = FFI::Platypus::Type->_new_custom_perl(
$type_map->{$type},
$cb->{perl_to_native},
$cb->{native_to_perl},
$cb->{perl_to_native_post},
$argument_count,
);
$self;
}
sub load_custom_type
{
my($self, $name, $alias, @type_args) = @_;
croak "usage: \$ffi->load_custom_type(\$name, \$alias, ...)"
unless defined $name && defined $alias;
$name = "FFI::Platypus::Type$name" if $name =~ /^::/;
$name = "FFI::Platypus::Type::$name" unless $name =~ /::/;
unless($name->can("ffi_custom_type_api_1"))
{
eval qq{ use $name () };
warn $@ if $@;
}
unless($name->can("ffi_custom_type_api_1"))
{
croak "$name does not appear to conform to the custom type API";
}
my $cb = $name->ffi_custom_type_api_1($self, @type_args);
$self->custom_type($alias => $cb);
$self;
}
sub _type_lookup
{
my($self, $name) = @_;
$self->type($name) unless defined $self->{types}->{$name};
$self->{types}->{$name};
}
sub types
{
my($self) = @_;
$self = $self->new unless ref $self && eval { $self->isa('FFI::Platypus') };
my %types = map { $_ => 1 } keys %{ $self->_type_map };
$types{$_} ||= 1 foreach keys %{ $self->{types} };
sort keys %types;
}
sub type_meta
{
my($self, $name) = @_;
$self = $self->new unless ref $self && eval { $self->isa('FFI::Platypus') };
my $type = $self->{types}->{$name} || $self->_type_lookup($name);
$type->meta;
}
sub function
{
my($self, $name, $args, $ret) = @_;
croak "usage \$ffi->function( name, [ arguments ], return_type)" unless @_ == 4;
my @args = map { $self->_type_lookup($_) || croak "unknown type: $_" } @$args;
$ret = $self->_type_lookup($ret) || croak "unknown type: $ret";
my $address = $name =~ /^-?[0-9]+$/ ? $name : $self->find_symbol($name);
croak "unable to find $name" unless defined $address || $self->ignore_not_found;
return unless defined $address;
FFI::Platypus::Function->new($self, $address, $self->{abi}, $ret, @args);
}
my $inner_counter=0;
sub attach
{
my $wrapper;
$wrapper = pop if ref $_[-1] eq 'CODE';
my($self, $name, $args, $ret, $proto) = @_;
$ret = 'void' unless defined $ret;
my($c_name, $perl_name) = ref($name) ? @$name : ($name, $name);
croak "you tried to provide a perl name that looks like an address"
if $perl_name =~ /^-?[0-9]+$/;
my $function = $self->function($c_name, $args, $ret);
if(defined $function)
{
my($caller, $filename, $line) = caller;
$perl_name = join '::', $caller, $perl_name
unless $perl_name =~ /::/;
my $attach_name = $perl_name;
if($wrapper)
{
$attach_name = "FFI::Platypus::Inner::xsub$inner_counter";
$inner_counter++;
}
$function->attach($attach_name, "$filename:$line", $proto);
if($wrapper)
{
my $inner_coderef = \&{$attach_name};
no strict 'refs';
# TODO: Sub::Name ?
*{$perl_name} = sub { $wrapper->($inner_coderef, @_) };
}
}
$self;
}
sub closure
{
my($self, $coderef) = @_;
croak "not a coderef" unless ref $coderef eq 'CODE';
FFI::Platypus::Closure->new($coderef);
}
sub cast
{
$_[0]->function(0 => [$_[1]] => $_[2])->call($_[3]);
}
sub attach_cast
{
my($self, $name, $type1, $type2) = @_;
my $caller = caller;
$name = join '::', $caller, $name unless $name =~ /::/;
$self->attach([0 => $name] => [$type1] => $type2 => '$');
$self;
}
sub sizeof
{
my($self,$name) = @_;
my $type = $self->{types}->{$name} || $self->_type_lookup($name);
FFI::Platypus::Type::sizeof($type);
}
sub alignof
{
my($self, $name) = @_;
my $meta = $self->type_meta($name);
croak "cannot determine alignment of record"
if $meta->{type} eq 'record';
require FFI::Platypus::ConfigData;
my $ffi_type;
if($meta->{type} eq 'pointer')
{
$ffi_type = 'pointer';
}
elsif($meta->{type} eq 'string' && $meta->{fixed_size})
{
$ffi_type = 'uint8';
}
else
{
$ffi_type = $meta->{ffi_type};
}
FFI::Platypus::ConfigData->config('align')->{$ffi_type};
}
sub find_lib
{
my $self = shift;
require FFI::CheckLib;
$self->lib(FFI::CheckLib::find_lib(@_));
$self;
}
sub find_symbol
{
my($self, $name) = @_;
unless(defined $self->{mangler})
{
my $class = _lang_class($self->{lang});
if($class->can('mangler'))
{
$self->{mangler} = $class->mangler($self->lib);
}
else
{
$self->{mangler} = sub { $_[0] };
}
}
foreach my $path (@{ $self->{lib} })
{
my $handle = do { no warnings; $self->{handles}->{$path||0} } || FFI::Platypus::dl::dlopen($path);
unless($handle)
{
warn "error loading $path: ", FFI::Platypus::dl::dlerror()
if $ENV{FFI_PLATYPUS_DLERROR};
next;
}
my $address = FFI::Platypus::dl::dlsym($handle, $self->{mangler}->($name));
if($address)
{
$self->{handles}->{$path||0} = $handle;
return $address;
}
else
{
FFI::Platypus::dl::dlclose($handle) unless $self->{handles}->{$path||0};
}
}
return;
}
sub package
{
my($self, $module, $modlibname) = @_;
require FFI::Platypus::ConfigData;
my @dlext = @{ FFI::Platypus::ConfigData->config("config_dlext") };
($module, $modlibname) = caller() unless defined $modlibname;
my @modparts = split /::/, $module;
my $modfname = $modparts[-1];
my $modpname = join('/',@modparts);
my $c = @modparts;
$modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
foreach my $dlext (@dlext)
{
my $file = "$modlibname/auto/$modpname/$modfname.$dlext";
unless(-e $file)
{
$modlibname =~ s,[\\/][^\\/]+$,,;
$file = "$modlibname/arch/auto/$modpname/$modfname.$dlext";
}
if(-e $file)
{
$self->lib($file);
return $self;
}
}
$self;
}
sub abis
{
require FFI::Platypus::ConfigData;
FFI::Platypus::ConfigData->config("abi");
}
sub abi
{
my($self, $newabi) = @_;
unless($newabi =~ /^[0-9]+$/)
{
unless(defined $self->abis->{$newabi})
{
croak "no such ABI: $newabi";
}
$newabi = $self->abis->{$newabi};
}
unless(FFI::Platypus::ABI::verify($newabi))
{
croak "no such ABI: $newabi";
}
$self->{abi} = $newabi;
$self;
}
sub DESTROY
{
my($self) = @_;
foreach my $handle (values %{ $self->{handles} })
{
next unless $handle;
FFI::Platypus::dl::dlclose($handle);
}
delete $self->{handles};
}
sub _have_pm
{
my($class) = @_;
my $ok = eval qq{ use $class; 1 };
$ok = $ok ? $ok : 0;
$ok;
}
package FFI::Platypus::Function;
our $VERSION = '0.45'; # VERSION
use overload '&{}' => sub {
my $ffi = shift;
sub { $ffi->call(@_) };
};
use overload 'bool' => sub {
my $ffi = shift;
return $ffi;
};
package FFI::Platypus::Closure;
use Scalar::Util qw( refaddr);
use Carp qw( croak );
use overload '&{}' => sub {
my $self = shift;
sub { $self->{code}->(@_) };
};
our $VERSION = '0.45'; # VERSION
sub new
{
my($class, $coderef) = @_;
croak "not a coderef" unless ref($coderef) eq 'CODE';
my $self = bless { code => $coderef, cbdata => {} }, $class;
$self;
}
sub add_data
{
my($self, $payload, $type) = @_;
$self->{cbdata}{$type} = bless \$payload, 'FFI::Platypus::ClosureData';
}
sub get_data
{
my($self, $type) = @_;
if (exists $self->{cbdata}{$type}) {
return ${$self->{cbdata}{$type}};
}
return 0;
}
package FFI::Platypus::ClosureData;
our $VERSION = '0.45'; # VERSION
package FFI::Platypus::Type;
use Carp qw( croak );
our $VERSION = '0.45'; # VERSION
sub new
{
my($class, $type, $platypus) = @_;
# the platypus object is only needed for closures, so
# that it can lookup existing types.
if($type =~ m/^\((.*)\)\s*-\>\s*(.*)\s*$/)
{
croak "passing closure into a closure not supported" if $1 =~ /(\(|\)|-\>)/;
my @argument_types = map { $platypus->_type_lookup($_) } map { s/^\s+//; s/\s+$//; $_ } split /,/, $1;
my $return_type = $platypus->_type_lookup($2);
return $class->_new_closure($return_type, @argument_types);
}
my $ffi_type;
my $platypus_type;
my $size = 0;
my $classname;
my $rw = 0;
if($type =~ /^string(_rw|_ro|\s+ro|\s+rw|\s*\([0-9]+\)|)$/)
{
my $extra = $1;
$ffi_type = 'pointer';
$platypus_type = 'string';
$rw = 1 if $extra =~ /rw$/;
$size = $1 if $extra =~ /\(([0-9]+)\)$/;
}
elsif($type =~ /^record\s*\(([0-9:A-Za-z_]+)\)$/)
{
$ffi_type = 'pointer';
$platypus_type = 'record';
if($1 =~ /^([0-9]+)$/)
{
$size = $1;
}
else
{
$classname = $1;
unless($classname->can('ffi_record_size') || $classname->can('_ffi_record_size'))
{
eval qq{ use $classname };
warn "error requiring $classname: $@" if $@;
}
if($classname->can('ffi_record_size'))
{
$size = $classname->ffi_record_size;
}
elsif($classname->can('_ffi_record_size'))
{
$size = $classname->_ffi_record_size;
}
else
{
croak "$classname has not ffi_record_size or _ffi_record_size method";
}
}
}
elsif($type =~ s/\s+\*$//) {
$ffi_type = $type;
$platypus_type = 'pointer';
}
elsif($type =~ s/\s+\[([0-9]*)\]$//)
{
$ffi_type = $type;
$platypus_type = 'array';
$size = $1 ? $1 : 0;
}
else
{
$ffi_type = $type;
$platypus_type = 'ffi';
}
$class->_new($ffi_type, $platypus_type, $size, $classname, $rw);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
FFI::Platypus - Write Perl bindings to non-Perl libraries with FFI. No XS required.
=head1 VERSION
version 0.45
=head1 SYNOPSIS
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->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");
=head1 DESCRIPTION
Platypus is a library for creating interfaces to machine code libraries
written in languages like C, L<C++|FFI::Platypus::Lang::CPP>,
L<Fortran|FFI::Platypus::Lang::Fortran>,
L<Rust|FFI::Platypus::Lang::Rust>,
L<Pascal|FFI::Platypus::Lang::Pascal>. Essentially anything that gets
compiled into machine code. This implementation uses C<libffi> to
accomplish this task. C<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:
=over 4
=item 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.
=item 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.
=item FFI / Platypus could be a bridge to Perl 6
One of those "other" languages could be Perl 6 and Perl 6 already has an
FFI interface I am told.
=item 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.
=item 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.
=item 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 L<Fortran|FFI::Platypus::Lang::Fortran>,
L<Rust|FFI::Platypus::Lang::Rust>,
L<Pascal|FFI::Platypus::Lang::Pascal>, L<C++|FFI::Platypus::Lang::CPP>,
or even L<assembly|FFI::Platypus::Lang::ASM>, allowing you to focus
on your strengths.
=item FFI / Platypus does not require a parser
L<Inline> isolates the extension developer from XS to some extent, but
it also requires a parser. The various L<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.
=back
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
L<EXAMPLES|/EXAMPLES> to get a taste of what you can do with Platypus.
Platypus has extensive documentation of types at L<FFI::Platypus::Type>
and its custom types API at L<FFI::Platypus::API>.
=for stopwords ØMQ
=head1 CONSTRUCTORS
=head2 new
my $ffi = FFI::Platypus->new(%options);
Create a new instance of L<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 L<lib|/lib> attribute.
=head3 options
=over 4
=item lib
Either a pathname (string) or a list of pathnames (array ref of strings)
to pre-populate the L<lib|/lib> attribute.
=item ignore_not_found
[version 0.15]
Set the L<ignore_not_found|/ignore_not_found> attribute.
=item lang
[version 0.18]
Set the L<lang|/lang> attribute.
=back
=head1 ATTRIBUTES
=head2 lib
$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
L<FFI::CheckLib>, like this:
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
L<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 C<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 L</find_lib> method as a shortcut:
$ffi->find_lib( lib => 'archive' );
=head2 ignore_not_found
[version 0.15]
$ffi->ignore_not_found(1);
my $ignore_not_found = $ffi->ignore_not_found;
Normally the L<attach|/attach> and L<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
L<function|/function> will return C<undef> when the function is not
found and L<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
L<function|/function> or L<attach|/attach> in an C<eval>.
=head2 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
L<Rust|FFI::Platypus::Lang::Rust>, you will get C<i32> as an alias for
C<sint32> instead of C<int> as you do with L<C|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 L<CPP|FFI::Platypus::Lang::CPP> for C++ you can use method names
like C<Foo::get_bar()> with L</attach> or L</function>.
=head1 METHODS
=head2 type
$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 L<FFI::Platypus::Type> for legal type
definitions.
Examples:
$ffi->type('sint32'); # oly 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
=head2 custom_type
$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 L<FFI::Platypus::Type#Custom-Types> for details.
=head2 load_custom_type
$ffi->load_custom_type($name => $alias, @type_args);
Load the custom type defined in the module I<$name>, and make an alias
I<$alias>. If the custom type requires any arguments, they may be passed
in as I<@type_args>. See L<FFI::Platypus::Type#Custom-Types> for
details.
If I<$name> contains C<::> then it will be assumed to be a fully
qualified package name. If not, then C<FFI::Platypus::Type::> will be
prepended to it.
=head2 types
my @types = $ffi->types;
my @types = FFI::Platypus->types;
Returns the list of types that FFI knows about. This will include the
native C<libffi> types (example: C<sint32>, C<opaque> and C<double>) and
the normal C types (example: C<unsigned int>, C<uint32_t>), any types
that you have defined using the L<type|/type> method, and custom types.
The list of types that Platypus knows about varies somewhat from
platform to platform, L<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.
=head2 type_meta
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:
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
=head2 function
my $function = $ffi->function($name => \@argument_types => $return_type);
my $function = $ffi->function($address => \@argument_types => $return_type);
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:
my $function = $ffi->function(...);
my $coderef = sub { $function->(@_) };
It may be better, and faster to create a real Perl function using the
L<attach|/attach> method.
In addition to looking up a function by name you can provide the address
of the symbol yourself:
my $address = $ffi->find_symbol('my_functon');
my $function = $ffi->function($address => ...);
Under the covers, L<function|/function> uses L<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, or modules such as L<FFI::TinyCC> produce
machine code at runtime that you can call from Platypus.
Examples:
my $function = $ffi->function('my_function_name', ['int', 'string'] => 'string');
my $return_string = $function->(1, "hi there");
=head2 attach
$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, sub { ... });
$ffi->attach([$c_name => $perl_name] => \@argument_types => $return_type, sub { ... });
$ffi->attach([$address => $perl_name] => \@argument_types => $return_type, sub { ... });
Find and attach a C function as a real live Perl xsub. The advantage of
attaching a function over using the L<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 L<FFI::Platypus>
instance into memory permanently, since there is no way to deallocate an
xsub.
If just one I<$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 L<function|/function>
method.
Examples:
$ffi->attach('my_functon_name', ['int', 'string'] => 'string');
$ffi->attach(['my_c_functon_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:
$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;
});
=head2 closure
my $closure = $ffi->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 L<FFI::Platypus::Type#Closures>.
=head2 cast
my $converted_value = $ffi->cast($original_type, $converted_type, $original_value);
The C<cast> function converts an existing I<$original_value> of type
I<$original_type> into one of type I<$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:
my $address = $ffi->cast('string' => 'opaque', $string_value);
Something that won't work is trying to cast an array to anything:
my $address = $ffi->cast('int[10]' => 'opaque', \@list); # WRONG
=head2 attach_cast
$ffi->attach_cast("cast_name", $original_type, $converted_type);
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.
=head2 sizeof
my $size = $ffi->sizeof($type);
Returns the total size of the given type in bytes. For example to get
the size of an integer:
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
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.
=head2 alignof
[version 0.21]
my $align = $ffi->alignof($type);
Returns the alignment of the given type in bytes.
=head2 find_lib
[version 0.20]
$ffi->find_lib( lib => $libname );
This is just a shortcut for calling L<FFI::CheckLib#find_lib> and
updating the L</lib> attribute appropriately. Care should be taken
though, as this method simply passes its arguments to
L<FFI::CheckLib#find_lib>, so if your module or script is depending on a
specific feature in L<FFI::CheckLib> then make sure that you update your
prerequisites appropriately.
=head2 find_symbol
my $address = $ffi->find_symbol($name);
Return the address of the given symbol (usually function).
=head2 package
[version 0.15]
$ffi->package($package, $file); # usually __PACKAGE__ and __FILE__ can be used
$ffi->package; # autodetect
If you have used L<Module::Build::FFI> to bundle C code with your
distribution, you can use this method to tell the L<FFI::Platypus>
instance to look for symbols that came with the dynamic library that was
built when your distribution was installed.
=head2 abis
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.
=head2 abi
$ffi->abi($name);
Set the ABI or calling convention for use in subsequent calls to
L</function> or L</attach>. May be either a string name or integer
value from the L</abis> method above.
=head1 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 L<FFI::Platypus::Type>
that are related to types.
=head2 Integer conversions
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
$ffi->attach(puts => ['string'] => 'int');
$ffi->attach(atoi => ['string'] => 'int');
puts(atoi('56'));
B<Discussion>: C<puts> and C<atoi> should be part of the standard C
library on all platforms. C<puts> prints a string to standard output,
and C<atoi> converts a string to integer. Specifying C<undef> as a
library tells Platypus to search the current process for symbols, which
includes the standard c library.
=head2 libnotify
use FFI::CheckLib;
use FFI::Platypus;
# NOTE: I ported this from the like named eg/notify.pl that came with FFI::Raw
# and it seems to work most of the time, but also seems to SIGSEGV sometimes.
# I saw the same behavior in the FFI::Raw version, and am not really familiar
# with the libnotify API to say what is the cause. Patches welcome to fix it.
my $ffi = FFI::Platypus->new;
$ffi->lib(find_lib_or_exit lib => 'notify');
$ffi->attach(notify_init => ['string'] => 'void');
$ffi->attach(notify_uninit => [] => 'void');
$ffi->attach([notify_notification_new => 'notify_new'] => ['string', 'string', 'string'] => 'opaque');
$ffi->attach([notify_notification_update => 'notify_update'] => ['opaque', 'string', 'string', 'string'] => 'void');
$ffi->attach([notify_notification_show => 'notify_show'] => ['opaque', 'opaque'] => 'void');
notify_init('FFI::Platypus');
my $n = notify_new('','','');
notify_update($n, 'FFI::Platypus', 'It works!!!', 'media-playback-start');
notify_show($n, undef);
notify_uninit();
B<Discussion>: libnotify is a desktop GUI notification library 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.
The most portable way to find the correct name and location of a dynamic
library is via the L<FFI::CheckLib#find_lib> family of functions. If
you are putting together a CPAN distribution, you should also consider
using L<FFI::CheckLib#check_lib_or_exit> function in your C<Build.PL> or
C<Makefile.PL> file (If you are using L<Dist::Zilla>, check out the
L<Dist::Zilla::Plugin::FFI::CheckLib> plugin). This will provide a user
friendly diagnostic letting the user know that the required library is
missing, and reduce the number of bogus CPAN testers results that you
will get.
Also in this example, we rename some of the functions when they are
placed into Perl space to save typing:
attach [notify_notification_new => 'notify_new']
=> [string,string,string]
=> opaque;
When you specify a list reference as the "name" of the function the
first element is the symbol name as understood by the dynamic library.
The second element is the name as it will be placed in Perl space.
Later, when we call C<notify_new>:
my $n = notify_new('','','');
We are really calling the C function C<notify_notification_new>.
=head2 Allocating and freeing memory
use FFI::Platypus;
use FFI::Platypus::Memory qw( malloc free memcpy );
my $ffi = FFI::Platypus->new;
my $buffer = malloc 12;
memcpy $buffer, $ffi->cast('string' => 'opaque', "hello there"), length "hello there\0";
print $ffi->cast('opaque' => 'string', $buffer), "\n";
free $buffer;
B<Discussion>: C<malloc> and C<free> are standard memory allocation
functions available from the standard c library and. Interfaces to
these and other memory related functions are provided by the
L<FFI::Platypus::Memory> module.
=head2 structured data records
package My::UnixTime;
use FFI::Platypus::Record;
record_layout(qw(
int 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
long tm_gmtoff
string tm_zone
));
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
# define a record class My::UnixTime and alias it to "tm"
$ffi->type("record(My::UnixTime)" => 'tm');
# attach the C localtime function as a constructor
$ffi->attach( localtime => ['time_t*'] => 'tm', sub {
my($inner, $class, $time) = @_;
$time = time unless defined $time;
$inner->(\$time);
});
package main;
# now we can actually use our My::UnixTime class
my $time = My::UnixTime->localtime;
printf "time is %d:%d:%d %s\n",
$time->tm_hour,
$time->tm_min,
$time->tm_sec,
$time->tm_zone;
B<Discussion>: C and other machine code languages frequently provide
interfaces that include structured data records (known as "structs" in
C). They sometimes provide an API in which you are expected to
manipulate these records before and/or after passing them along to C
functions. There are a few ways of dealing with such interfaces, but
the easiest way is demonstrated here defines a record class using a
specific layout. For more details see L<FFI::Platypus::Record>.
(L<FFI::Platypus::Type> includes some other ways of manipulating
structured data records).
=head2 libuuid
use FFI::CheckLib;
use FFI::Platypus;
use FFI::Platypus::Memory qw( malloc free );
my $ffi = FFI::Platypus->new;
$ffi->lib(find_lib_or_exit lib => 'uuid');
$ffi->type('string(37)' => 'uuid_string');
$ffi->type('record(16)' => 'uuid_t');
$ffi->attach(uuid_generate => ['uuid_t'] => 'void');
$ffi->attach(uuid_unparse => ['uuid_t','uuid_string'] => 'void');
my $uuid = "\0" x 16; # uuid_t
uuid_generate($uuid);
my $string = "\0" x 37; # 36 bytes to store a UUID string
# + NUL termination
uuid_unparse($uuid, $string);
print "$string\n";
B<Discussion>: libuuid is a library used to generate unique identifiers
(UUID) for objects that may be accessible beyond the local system. The
library is or was part of the Linux e2fsprogs package.
Knowing the size of objects is sometimes important. In this example, we
use the L<sizeof|/sizeof> function to get the size of 16 characters (in
this case it is simply 16 bytes). We also know that the strings
"deparsed" by C<uuid_unparse> are exactly 37 bytes.
=head2 puts and getpid
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
$ffi->attach(puts => ['string'] => 'int');
$ffi->attach(getpid => [] => 'int');
puts(getpid());
B<Discussion>: C<puts> is part of standard C library on all platforms.
C<getpid> is available on Unix type platforms.
=head2 Math library
use FFI::Platypus;
use FFI::CheckLib;
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
$ffi->attach(puts => ['string'] => 'int');
$ffi->attach(fdim => ['double','double'] => 'double');
puts(fdim(7.0, 2.0));
$ffi->attach(cos => ['double'] => 'double');
puts(cos(2.0));
$ffi->attach(fmax => ['double', 'double'] => 'double');
puts(fmax(2.0,3.0));
B<Discussion>: On UNIX the standard c library math functions are
frequently provided in a separate library C<libm>, so you could search
for those symbols in "libm.so", but that won't work on non-UNIX
platforms like Microsoft Windows. Fortunately Perl uses the math
library so these symbols are already in the current process so you can
use C<undef> as the library to find them.
=head2 Strings
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
$ffi->attach(puts => ['string'] => 'int');
$ffi->attach(strlen => ['string'] => 'int');
puts(strlen('somestring'));
$ffi->attach(strstr => ['string','string'] => 'string');
puts(strstr('somestring', 'string'));
#attach puts => [string] => int;
puts(puts("lol"));
$ffi->attach(strerror => ['int'] => 'string');
puts(strerror(2));
B<Discussion>: Strings are not a native type to C<libffi> but the are
handled seamlessly by Platypus.
=head2 Attach function from pointer
use FFI::TinyCC;
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
my $tcc = FFI::TinyCC->new;
$tcc->compile_string(q{
int
add(int a, int b)
{
return a+b;
}
});
my $address = $tcc->get_symbol('add');
$ffi->attach( [ $address => 'add' ] => ['int','int'] => 'int' );
print add(1,2), "\n";
B<Discussion>: Sometimes you will have a pointer to a function from a
source other than Platypus that you want to call. You can use that
address instead of a function name for either of the
L<function|/function> or L<attach|/attach> methods. In this example we
use L<FFI::TinyCC> to compile a short piece of C code and to give us the
address of one of its functions, which we then use to create a perl xsub
to call it.
L<FFI::TinyCC> embeds the Tiny C Compiler (tcc) to provide a
just-in-time (JIT) compilation service for FFI.
=head2 libzmq
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_exit );
use FFI::Platypus;
use FFI::Platypus::Memory qw( malloc );
use FFI::Platypus::Buffer qw( scalar_to_buffer buffer_to_scalar );
my $endpoint = "ipc://zmq-ffi-$$";
my $ffi = FFI::Platypus->new;
$ffi->lib(undef); # for puts
$ffi->attach(puts => ['string'] => 'int');
$ffi->lib(find_lib_or_exit lib => 'zmq');
$ffi->attach(zmq_version => ['int*', 'int*', 'int*'] => 'void');
my($major,$minor,$patch);
zmq_version(\$major, \$minor, \$patch);
puts("libzmq version $major.$minor.$patch");
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);
do { # 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;
};
do { # 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);
my $recv_message = buffer_to_scalar $data_ptr, $size;
print "recv_message = $recv_message\n";
};
B<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 C<zmq_version> to ask libzmq
which version it is.
C<zmq_version> returns the version number via three integer pointer
arguments, so we use the pointer to integer type: C<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 C<$major> variable (and the others) has been updated and we
can use it to verify that it supports the API that we require.
Notice that we define three aliases for the C<opaque> type:
C<zmq_context>, C<zmq_socket> and C<zmq_msg_t>. While this isn't
strictly necessary, since Platypus and C treat all three of these types
the same, it is useful form of documentation that helps describe the
functionality of the interface.
Finally we attach the necessary functions, send and receive a message.
If you are interested, there is a fully fleshed out ØMQ Perl interface
implemented using FFI called L<ZMQ::FFI>.
=head2 libarchive
use FFI::Platypus ();
use FFI::Platypus::API ();
use FFI::CheckLib ();
# 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 = My::Platypus->new;
$ffi->lib(FFI::CheckLib::find_lib_or_exit lib => 'archive');
$ffi->custom_type(archive => {
native_type => 'opaque',
perl_to_native => sub { ${$_[0]} },
native_to_perl => sub {
# this works because archive_read_new ignores any arguments
# and we pass in the class name which we can get here.
my $class = FFI::Platypus::API::arguments_get_string(0);
bless \$_[0], $class;
},
});
$ffi->custom_type(archive_entry => {
native_type => 'opaque',
perl_to_native => sub { ${$_[0]} },
native_to_perl => sub {
# works likewise for archive_entry objects
my $class = FFI::Platypus::API::arguments_get_string(0);
bless \$_[0], $class,
},
});
package My::Platypus;
use base qw( FFI::Platypus );
sub find_symbol
{
my($self, $name) = @_;
my $prefix = lcfirst caller(2);
$prefix =~ s{([A-Z])}{"_" . lc $1}eg;
$self->SUPER::find_symbol(join '_', $prefix, $name);
}
package Archive;
# base class is "abstract" having no constructor or destructor
$ffi->attach( error_string => ['archive'] => 'string' );
package ArchiveRead;
our @ISA = qw( Archive );
$ffi->attach( new => ['string'] => 'archive' );
$ffi->attach( [ free => 'DESTROY' ] => ['archive'] => 'void' );
$ffi->attach( support_filter_all => ['archive'] => 'int' );
$ffi->attach( support_format_all => ['archive'] => 'int' );
$ffi->attach( open_filename => ['archive','string','size_t'] => 'int' );
$ffi->attach( next_header2 => ['archive', 'archive_entry' ] => 'int' );
$ffi->attach( data_skip => ['archive'] => 'int' );
# ... define additional read methods
package ArchiveWrite;
our @ISA = qw( Archive );
$ffi->attach( new => ['string'] => 'archive' );
$ffi->attach( [ free => 'DESTROY' ] => ['archive'] => 'void' );
# ... define additional write methods
package ArchiveEntry;
$ffi->attach( new => ['string'] => 'archive_entry' );
$ffi->attach( [ free => 'DESTROY' ] => ['archive_entry'] => 'void' );
$ffi->attach( pathname => ['archive_entry'] => 'string' );
# ... define additional entry methods
package main;
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;
}
B<Discussion>: libarchive is the implementation of C<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 C<Archive>, and concrete classes C<ArchiveWrite>,
C<ArchiveRead> and C<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. For more details on custom types
see L<FFI::Platypus::Type> and L<FFI::Platypus::API>.
Another advanced feature of this example is that we extend the
L<FFI::Platypus> class to define our own find_symbol method that
prefixes the symbol names depending on the class in which they are
defined. This means we can do this when we define a method for Archive:
$ffi->attach( support_filter_all => ['archive'] => 'int' );
Rather than this:
$ffi->attach(
[ archive_read_support_filter_all => 'support_read_filter_all' ] =>
['archive'] => 'int' );
);
If you didn't want to create an entire new class just for this little
trick you could also use something like L<Object::Method> to extend
C<find_symbol>.
=head2 bzip2
use FFI::Platypus 0.20 (); # 0.20 required for using wrappers
use FFI::CheckLib qw( find_lib_or_die );
use FFI::Platypus::Buffer qw( scalar_to_buffer buffer_to_scalar );
use FFI::Platypus::Memory qw( malloc free );
my $ffi = FFI::Platypus->new;
$ffi->lib(find_lib_or_die lib => 'bz2');
$ffi->attach(
[ BZ2_bzBuffToBuffCompress => 'compress' ] => [
'opaque', # dest
'unsigned int *', # dest length
'opaque', # source
'unsigned int', # source length
'int', # blockSize100k
'int', # verbosity
'int', # workFactor
] => 'int',
sub {
my $sub = shift;
my($source,$source_length) = scalar_to_buffer $_[0];
my $dest_length = int(length($source)*1.01) + 1 + 600;
my $dest = malloc $dest_length;
my $r = $sub->($dest, \$dest_length, $source, $source_length, 9, 0, 30);
die "bzip2 error $r" unless $r == 0;
my $compressed = buffer_to_scalar($dest, $dest_length);
free $dest;
$compressed;
},
);
$ffi->attach(
[ BZ2_bzBuffToBuffDecompress => 'decompress' ] => [
'opaque', # dest
'unsigned int *', # dest length
'opaque', # source
'unsigned int', # source length
'int', # small
'int', # verbosity
] => 'int',
sub {
my $sub = shift;
my($source, $source_length) = scalar_to_buffer $_[0];
my $dest_length = $_[1];
my $dest = malloc $dest_length;
my $r = $sub->($dest, \$dest_length, $source, $source_length, 0, 0);
die "bzip2 error $r" unless $r == 0;
my $decompressed = buffer_to_scalar($dest, $dest_length);
free $dest;
$decompressed;
},
);
my $original = "hello compression world\n";
my $compressed = compress($original);
print decompress($compressed, length $original);
B<Discussion>: bzip2 is a compression library. For simple one shot
attempts at compression/decompression when you expect the original and
the result to fit within memory it provides two convenience functions
C<BZ2_bzBuffToBuffCompress> and C<BZ2_bzBuffToBuffDecompress>.
The first four arguments of both of these C functions are identical, and
represent two buffers. One buffer is the source, the second is the
destination. For the destination, the length is passed in as a pointer
to an integer. On input this integer is the size of the destination
buffer, and thus the maximum size of the compressed or decompressed
data. When the function returns the actual size of compressed or
compressed data is stored in this integer.
This is normal stuff for C, but in Perl our buffers are scalars and they
already know how large they are. In this sort of situation, wrapping
the C function in some Perl code can make your interface a little more
Perl like. In order to do this, just provide a code reference as the
last argument to the L</attach> method. The first argument to this
wrapper will be a code reference to the C function. The Perl arguments
will come in after that. This allows you to modify / convert the
arguments to conform to the C API. What ever value you return from the
wrapper function will be returned back to the original caller.
=head2 Java
Java:
// On Linux build .so with
// % gcj -fPIC -shared -o libexample.so Example.java
public class Example
{
public static void print_hello()
{
System.out.println("hello world");
}
public static int add(int a, int b)
{
return a + b;
}
}
C++:
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>
extern "C" void
gcj_start()
{
using namespace java::lang;
JvCreateJavaVM(NULL);
JvInitClass(&System::class$);
}
extern "C" void
gcj_end()
{
JvDetachCurrentThread();
}
Perl:
use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lib('./libexample.so');
# Java methods are mangled by gcj using the same format as g++
$ffi->attach(
[ _ZN7Example11print_helloEJvv => 'print_hello' ] => [] => 'void'
);
$ffi->attach(
[ _ZN7Example3addEJiii => 'add' ] => ['int', 'int'] => 'int'
);
# Initialize the Java runtime
$ffi->function( gcj_start => [] => 'void' )->call;
print_hello();
print add(1,2), "\n";
# Wind the java runtime down
$ffi->function( gcj_end => [] => 'void' )->call;
Makefile:
GCJ=gcj
CXX=g++
CFLAGS=-fPIC
LDFLAGS=-shared
RM=rm -f
libexample.so: between.o Example.o
$(GCJ) $(LDFLAGS) -o libexample.so between.o Example.o
between.o: between.cpp
$(CXX) $(CFLAGS) -c -o between.o between.cpp
Example.o: Example.java
$(GCJ) $(CFLAGS) -c -o Example.o Example.java
clean:
$(RM) *.o *.so
Output:
% make
g++ -fPIC -c -o between.o between.cpp
gcj -fPIC -c -o Example.o Example.java
gcj -shared -o libexample.so between.o Example.o
% perl example.pl
hello world
3
B<Discussion>: You can't call Java .class files directly from FFI /
Platypus, but you can compile Java source and .class files into a shared
library using the GNU Java Compiler C<gcj>. Because we are calling Java
functions from a program (Perl!) that was not started from a Java
C<main()> we have to initialize the Java runtime ourselves
(L<details|https://gcc.gnu.org/onlinedocs/gcj/Invocation.html>).
This can most easily be accomplished from C++.
The GNU Java Compiler uses the same format to mangle method names as GNU
C++. The L<C++ plugin|FFI::Platypus::Lang::CPP> for handles this more
transparently by extracting the symbols from the shared library and
using either L<FFI::Platypus::Lang::CPP::Demangle::XS> or C<c++filt> to
determined the unmangled names.
Although the Java source is compiled ahead of time with optimizations,
it will not necessarily perform better than a real JVM just because it
is compiled. In fact the gcj developers warn than gcj will optimize
Java source better than Java .class files. The GNU Java Compiler also
lags behind modern Java.
Even so this enables you to call Java from Perl and potentially other
Java based languages such as Scala, Groovy or JRuby.
=head1 CAVEATS
Platypus and Native Interfaces like libffi rely on the availability of
dynamic libraries. Things not supported include:
=over 4
=item Systems that lack dynamic library support
Like MS-DOS
=item Systems that are not supported by libffi
Like OpenVMS
=item Languages that do not support using dynamic libraries from other languages
Like Google's Go. Although I believe that XS won't help in this
regard.
=item Languages that do not compile to machine code
Like .NET based languages and Java that can't be understood by gcj.
=back
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.
=head1 SUPPORT
IRC: #native on irc.perl.org
L<(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:
L<https://github.com/plicease/FFI-Platypus/issues>
=head1 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:
L<https://github.com/plicease/FFI-Platypus/pulls>
This project is developed using L<Dist::Zilla>. The project's git
repository also comes with C<Build.PL> and C<cpanfile> files necessary
for building, testing (and even installing if necessary) without
L<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 C<dist.ini> file. If you do use
L<Dist::Zilla> and already have the necessary plugins installed, then I
encourage you to run C<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 C<libtest>
that is normally automatically built by C<./Build test>. If you prefer
to use C<prove> or run tests directly, you can use the C<./Build
libtest> command to build it. Example:
% perl Build.PL
% ./Build
% ./Build libtest
% prove -bv t
# or an individual test
% perl -Mblib t/ffi_platypus_memory.t
The build process also respects these environment variables:
=over 4
=item FFI_PLATYPUS_DEBUG
Build the XS code portion of Platypus with -g3 instead of what ever
optimizing flags that your Perl normally uses. This is useful if you
need to debug the C or XS code that comes with Platypus, but do not have
a debugging Perl.
% env FFI_PLATYPUS_DEBUG=1 perl Build.PL
DEBUG:
- $Config{lddlflags} = -shared -O2 -L/usr/local/lib -fstack-protector
+ $Config{lddlflags} = -shared -g3 -L/usr/local/lib -fstack-protector
- $Config{optimize} = -O2
+ $Config{optimize} = -g3
Created MYMETA.yml and MYMETA.json
Creating new 'Build' script for 'FFI-Platypus' version '0.10'
=item FFI_PLATYPUS_DEBUG_FAKE32
When building Platypus on 32 bit Perls, it will use the L<Math::Int64> C
API and make L<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 Build.PL
DEBUG_FAKE32:
+ making Math::Int64 a prerequisite (not normally done on 64 bit Perls)
+ using Math::Int64's C API to manipulate 64 bit values (not normally done on 64 bit Perls)
Created MYMETA.yml and MYMETA.json
Creating new 'Build' script for 'FFI-Platypus' version '0.10'
=item FFI_PLATYPUS_NO_ALLOCA
Platypus uses the non-standard and somewhat controversial C function
C<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 C<alloca>
despite these precautions, then you can turn its use off by setting this
environment variable when you run C<Build.PL>:
% env FFI_PLATYPUS_NO_ALLOCA=1 perl Build.PL
NO_ALLOCA:
+ alloca() will not be used, even if your platform supports it.
Created MYMETA.yml and MYMETA.json
Creating new 'Build' script for 'FFI-Platypus' version '0.10'
=back
=head2 Coding Guidelines
=over 4
=item
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.
=item
Please make an effort to follow existing coding style when making pull
requests.
=item
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.
=back
=head2 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:
=over 4
=item L<https://github.com/plicease/FFI-Performance>
=back
=head2 System integrators
If you are including Platypus in a larger system (for example a Linux
distribution), and you already have libffi as part of your system, you
may be interested in L<Alt::Alien::FFI::System>. This is an alternative
to L<Alien::FFI> that does not require L<Alien::Base>. In fact it has
zero non-Core dependencies, and doesn't even need to be installed.
Simply include L<Alt::Alien::FFI::System>'s C<lib> directory in your
C<PERL5LIB> path when you build Platypus. For example:
% export PERL5LIB=/path/to/Alt-Alien-FFI-System/lib
% cpanm FFI::Platypus
=head1 SEE ALSO
=over 4
=item L<NativeCall>
Promising interface to Platypus inspired by Perl 6.
=item L<FFI::Platypus::Type>
Type definitions for Platypus.
=item L<FFI::Platypus::Record>
Define structured data records (C "structs") for use with
Platypus.
=item L<FFI::Platypus::API>
The custom types API for Platypus.
=item L<FFI::Platypus::Memory>
Memory functions for FFI.
=item L<FFI::CheckLib>
Find dynamic libraries in a portable way.
=item L<Module::Build::FFI>
Bundle C code with your FFI extension.
=item L<FFI::TinyCC>
JIT compiler for FFI.
=item L<FFI::Platypus::Lang::C>
Documentation and tools for using Platypus with the C programming
language
=item L<FFI::Platypus::Lang::CPP>
Documentation and tools for using Platypus with the C++ programming
language
=item L<FFI::Platypus::Lang::Fortran>
Documentation and tools for using Platypus with Fortran
=item L<FFI::Platypus::Lang::Pascal>
Documentation and tools for using Platypus with Free Pascal
=item L<FFI::Platypus::Lang::Rust>
Documentation and tools for using Platypus with the Rust programming
language
=item L<FFI::Platypus::Lang::ASM>
Documentation and tools for using Platypus with the Assembly
=item L<Convert::Binary::C>
A great interface for decoding C data structures, including C<struct>s,
C<enum>s, C<#define>s and more.
=item L<pack and unpack|perlpacktut>
Native to Perl functions that can be used to decode C C<struct> types.
=item L<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.
=item L<FFI::Raw>
Alternate interface to libffi with fewer features. It notably lacks the
ability to create real xsubs, which may make L<FFI::Platypus> much
faster. Also lacking are pointers to native types, arrays and custom
types. In its favor, it has been around for longer that Platypus, and
has been battle tested to some success.
=item L<Win32::API>
Microsoft Windows specific FFI style interface.
=item L<Ctypes|https://gitorious.org/perl-ctypes>
Ctypes was intended as a FFI style interface for Perl, but was never
part of CPAN, and at least the last time I tried it did not work with
recent versions of Perl.
=item L<FFI>
Foreign function interface based on (nomenclature is everything) FSF's
C<ffcall>. It hasn't worked for quite some time, and C<ffcall> is no
longer supported or distributed.
=item L<C::DynaLib>
Another FFI for Perl that doesn't appear to have worked for a long time.
=item L<C::Blocks>
Embed a tiny C compiler into your Perl scripts.
=item L<Alien::FFI>
Provides libffi for Platypus during its configuration and build stages.
=item L<Alt::Alien::FFI::System>
An alternative for L<Alien::FFI> intended mainly for system integrators.
=item L<P5NCI>
Yet another FFI like interface that does not appear to be supported or
under development anymore.
=back
=head1 ACKNOWLEDGMENTS
In addition to the contributors mentioned below, I would like to
acknowledge Brock Wilcox (AWWAIID) and Meredith Howard (MHOWARD) whose
work on L<FFI::Sweet|https://github.com/merrilymeredith/p5-FFI-Sweet>
not only helped me get started with FFI but significantly influenced the
design of Platypus.
In addition I'd like to thank Alessandro Ghedini (ALEXBIO) who was
always responsive to bug reports and pull requests for L<FFI::Raw>,
which was important in the development of the ideas on which Platypus is
based.
=head1 AUTHOR
Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
Contributors:
Bakkiaraj Murugesan (bakkiaraj)
Dylan Cali (calid)
pipcet
Zaki Mughal (zmughal)
Fitz Elliott (felliott)
Vickenty Fesunov (vyf)
Gregor Herrmann (gregoa)
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 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.
=cut
|