简体   繁体   English

检查数组元素是否未从 XSUB 中定义

[英]Check if array elements are undefined from an XSUB

I am trying to check if an array element is undef from an XSUB like this:我正在尝试检查数组元素是否来自XSUBundef ,如下所示:

void
print_array(array)
    AV *array

    PREINIT:
        int len;
        SV **sv_ptr;
        SV *sv;
        int i;

    CODE:
        len = av_len(array) + 1;
        printf("[");
        for (i = 0; i < len; i++) {
            sv_ptr = av_fetch( array, i, 0 );
            if (!sv_ptr) {
                printf("empty");
            }
            else {
                sv = *sv_ptr;
                if (sv == &PL_sv_undef) {
                    printf("undef");
                }
                else {
                    printf("*");
                }
            }

            if (i < (len - 1)) {
                printf(", ");
            }
        }

        printf("]\n");

If I run this sub from a Perl script:如果我从 Perl 脚本运行这个子:

use strict;
use warnings;
use ArrayPrint;

my $array = [];
$array->[4] = undef;
ArrayPrint::print_array($array);

The output is: output 是:

[empty, empty, empty, empty, *]

Why is the last element not showing undef ?为什么最后一个元素没有显示undef

An SV can hold an undefined value but still be a different SV than PL_sv_undef. SV 可以保存未定义的值,但仍然是与 PL_sv_undef 不同的 SV。 You need to replace the PL_sv_undef test with您需要将 PL_sv_undef 测试替换为

SvGETMAGIC(sv);
if (!SvOK(sv)) { printf "undef" } else { printf "*" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM