[英]How can Perl's XSUB die?
I have written a Perl XS wrapper for a C library consisting of about ~80 functions. 我已经为C库编写了一个Perl XS包装器,其中包含约80个函数。 Right now my general strategy is to substitute the error from a C function with PL_sv_undef
and the calling Perl code has to check explicitly whether the return is not undef
. 现在,我的一般策略是用PL_sv_undef
替换C函数中的错误,并且调用Perl代码必须显式检查返回是否不是undef
。 (For some C functions it is more complicated as I convert their output into a HV
/ AV
and use empty list to report the error.) (对于某些C函数,将其输出转换为HV
/ AV
并使用空列表报告错误会更加复杂。)
Now as I moved to writing bigger Perl scripts using that library, I want to simplify the error handling and use eg the usual eval {}
/ die
exception-like mechanism to handle errors. 现在,当我开始使用该库编写更大的Perl脚本时,我想简化错误处理,并使用例如通常的eval {}
/ die
-like-like机制来处理错误。
At the moment a simple XSUB in my XS look like that: 目前,我的XS中有一个简单的XSUB看起来像这样:
SV *
simple_function( param1, param2 = 0, param3 = 0)
int param1
int param2
int param3
CODE:
int rc;
rc = simple_function( param1, param2, param3 );
RETVAL = (rc == 0) ? &PL_sv_yes : &PL_sv_undef;
OUTPUT:
RETVAL
I have seen that some modules have global flag like "RaiseError" to die
on errors but failed to find any example I can borrow from. 我已经看到有些模块具有全局标志,例如“ RaiseError”, die
因错误而die
,但是找不到任何我可以借鉴的示例。 The few modules I have found handle the "RaiseError" flag inside the .pm
, not inside the .xs
, and thus allowed to use the Perl's die
. 我发现的几个模块在.pm
而不是.xs
内处理“ RaiseError”标志,因此可以使用Perl的die
。 In my case that is rather hard to implement inside the .pm
as many functions require special error checks. 在我的情况下,在.pm
内很难实现,因为许多功能需要特殊的错误检查。 That would also lead to code duplication as the checks are already present inside the XS. 由于XS内部已经存在检查,因此这也将导致代码重复。
I found nothing relevant in the perlxs
/ perlguts
documentation. 在perlxs
/ perlguts
文档中没有发现任何相关内容。 In particular, I have seen calls to Perl_croak()
in the .c
generated from my .xs
, but failed to locate any documentation for the function. 特别是,我在.xs
生成的.c
看到了对Perl_croak()
调用,但是找不到该函数的任何文档。
What is the XS' analog of the Perl's die
? XS与Perl死机的die
什么? Or how else can the XSUB report to Perl run-time that the function has failed and there is no RETVAL to return? 还是XSUB如何向Perl运行时报告该函数已失败并且没有RETVAL返回? How to properly set the $@
? 如何正确设置$@
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.