Perl Inline :: C失败时返回pdl或0

Joe*_*ger 2 perl return-type pdl inline-c

我正在构建一个模块,它连接到相机,拍照,并将数据读入一个小提琴.所有这些都发生在Inline :: C命令中.使用PDL文档中的过程,我可以创建pdl *并返回它.然而,相机可能无法拍照,在这种情况下,我希望0按照通常的协议返回my $pic_pdl = $Camera->TakePicture or die "Failed to take image".这似乎意味着我将需要使用该Inline_Stack_Push机制,但我不知道如何正确地将其pdl *转换为SV*.如果可能的话,我也想设置$!错误代码.可以在Inline中完成吗?

ike*_*ami 6

pdl*通过在typemap中找到的代码将其转换为SV.

$ cat `perl -E'use PDL::Core::Dev; say PDL_TYPEMAP'`
TYPEMAP
pdl*    T_PDL
pdl *   T_PDL
Logical T_IV
float   T_NV

INPUT

T_PDL
        $var = PDL->SvPDLV($arg)


OUTPUT

T_PDL
        PDL->SetSV_PDL($arg,$var);
Run Code Online (Sandbox Code Playgroud)

如果我读得对,你应该可以这样做:

SV* my_new {
    pdl* p = NULL;

    ...

    if (error) {
        if (p)
            free(p);  /* I think */
        return &PL_sv_undef;
    } else {
        SV* rv = newSV(0);
        PDL->SetSV_PDL(rv, p);
        return rv;
    }
}
Run Code Online (Sandbox Code Playgroud)

至于$!它,它只是C的接口errno.简单的设置errno.

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 2
2
No such file or directory

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 3
3
No such process

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 4
4
Interrupted system call
Run Code Online (Sandbox Code Playgroud)