2

I have multiple c functions on this format :

int function(const char* input, size_t len, char result[]) ;

where int is the return type;

result[] contains string which the function fill

what is the best to write such function as XS and expose it on perl to:

  1. get the return value of the function

  2. get the char result[] value on perl

I tried the below but the below only return the return value

int
my_function(a,b,c)
    const char *  a
    long            b
    const char *    c
    CODE:
    RETVAL =function(a,b,c)
    OUTPUT:
    RETVAL

Any help will be appreciated ?

4

3 回答 3

4

创建一个新目录,并将以下文件复制到其中:

./example.xs

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"    
#include "const-c.inc"

int my_function(const char* input, size_t len, char result[])
{
    strcpy(result, "the answer");
    return 42;
}

MODULE = Acme::XS::Example        PACKAGE = Acme::XS::Example

INCLUDE: const-xs.inc
PROTOTYPES: DISABLE

void
my_xsub (input, len)
    char *input
    int len
PPCODE:
{
    char result[10];
    int got = my_function(input, len, result);

    /* create a new scalar from an int, and push onto stack */
    PUSHs( newSViv(got) );
    /* create a new scalar from a string, and push onto stack */
    PUSHs( newSVpv(result, 0) );

    XSRETURN(2);  /* two items returned */
}

./lib/Acme/XS/Example.pm

use 5.010001;
use strict;
use warnings;
use XSLoader ();

package Acme::XS::Example;
our $VERSION   = '0.001';
__PACKAGE__->XSLoader::load($VERSION);
1;

./t/basic.t

use strict;
use warnings;
use Test::More;

use_ok('Acme::XS::Example');

my @results = Acme::XS::Example::my_xsub("Hello world", 666);

is_deeply(
    \@results,
    [ 42, "the answer" ],
    "got expected results",
) or diag explain(\@results);

done_testing;

./Makefile.PL

use strict;
use Devel::PPPort;
use ExtUtils::MakeMaker 6.6303;
use ExtUtils::Constant;

Devel::PPPort::WriteFile();

ExtUtils::Constant::WriteConstants(
    NAME         => 'Acme::XS::Example',
    NAMES        => [],
    DEFAULT_TYPE => 'IV',
    C_FILE       => 'const-c.inc',
    XS_FILE      => 'const-xs.inc',
);

WriteMakefile(
    NAME       => 'Acme::XS::Example',
    DISTNAME   => 'Acme-XS-Example',
    VERSION    => '0.001',
    ABSTRACT   => 'an example XS thingy',
    AUTHOR     => ['Toby Inkster <tobyink@cpan.org>'],
    LICENSE    => 'perl_5',
    MIN_PERL_VERSION  => '5.010001',
    PREREQ_PM  => {
        'XSLoader'             => 0,
    },
    TEST_REQUIRES => {
        'Test::More'           => '0.96',
    },
    CONFIGURE_REQUIRES => {
        'Devel::PPPort'        => 0,
        'ExtUtils::Constant'   => 0,
        'ExtUtils::MakeMaker'  => '6.6303',
    },
    LIBS       => [''],
    DEFINE     => '',
    INC        => '-I.',
    test       => { TESTS => "t/*.t" },
    clean      => { FILES => 'const-c.inc const-xs.inc ppport.h' },
);

有你的骨架可以玩。:-)

要构建和测试它:

perl Makefile.PL
make
make test
于 2014-07-19T18:45:33.203 回答
1

该程序h2xs可用于创建一个 perl/XS 模块,该模块充当 C 代码的包装器。我认为您需要创建一个包含函数原型的头文件。

有关详细信息,请阅读手册页h2xs。另外,看看这里

于 2014-07-18T22:50:53.480 回答
0

perl 函数(也是 XS 函数)可以返回多个值。
您将需要使用 PPCODE 而不是 CODE,并将多个变量作为结果压入堆栈。例如,请参阅 PPCODE 的 perlxs 文档。

于 2014-07-19T16:01:01.160 回答