c语言 - Perl XS:这个转储值对对象 - t_obj是什么意思 - $VAR 1 = bless( do{\(my $o = 41032464)}, 'Math::Test1' )



我尝试编写使用perl xs使用C API的Perl代码。我将对象的转储输出为 -

t_obj is -$ var1 = bless(do {(我的$ o = 41032464)},'Math :: test1');

我写了一个简单的库,称为Math :: test1 for练习,尽管我的模块不同,而且更复杂。

看来从perl到C的转换很好,但是当我将数据回到perl上时,当我期望对象的属性带有一些添加信息时,数据以这种困惑形式。

粘贴我的代码 -

test1.xs

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <sys/vfs.h>
typedef struct Num {
    int x;
    int y;
} *Math__Test1;
MODULE = Math::Test1            PACKAGE = Math::Test1
PROTOTYPES:     ENABLE
void
hello()
    CODE:
        printf("Hello World!n");
Math::Test1
new_xs(class, u, v)
    SV*  class
    int     u
    int     v
    CODE:
    Newz(0, RETVAL, 1, struct Num);
    RETVAL->x = u;
    RETVAL->y = v;
    printf("struct members are %d and %dn", RETVAL->x, RETVAL->y);
    OUTPUT:
    RETVAL

SV *
display(in)
    Math::Test1  in
    INIT:
        printf("INIT: Sectionn");
        printf("number is %d and %dn", in->x, in->y);
    CODE:
        printf("CODE:Sectionn");

int
add(in)
    Math::Test1  in
    CODE:
        RETVAL = in->x + in->y;
    OUTPUT:
        RETVAL
void
DESTROY(self)
        Math::Test1 self
        CODE:
        Safefree(self);

typemap

TYPEMAP
Math::Test1      T_PTROBJ
INPUT
T_PTROBJ
    if(SvROK($arg) && (sv_isa($arg, "${ntype}"))) {
        $var = ($type) SvIV((SV*)SvRV($arg));
    } else {
        croak("$arg is not reference and type of $type");
    }
OUTPUT
T_PTROBJ
    sv_setref_pv($arg, "${ntype}", (void*) $var);

数学/test1.pm

package Math::Test1;
use 5.014002;
use strict;
use warnings;
use Data::Dumper;
our $VERSION = '0.01';
require XSLoader;
XSLoader::load('Math::Test1', $VERSION);
use Moose;
has 'a' => (
    is       => 'rw',
    isa      => 'Int',
    required => 1,
);
has 'b' => (
    is       => 'rw',
    isa     => 'Int',
    required => 1,
);
sub new {
   my $self = shift;
   my %arg = @_;
   print "==========CONSTRUCTOR=========n";
   Math::Test1::new_xs($self, $arg{a}, $arg{b});
}
sub addition {
    my $self = shift;
    print "==========ADDITION=========n";
    my $out = Math::Test1::add($self);
    return $out;
}
sub show {
    my $self = shift;
    print "==========DISPLAY=========n";
    Math::Test1::display($self);
}
1;

t/t.pl(测试文件)

use strict;
use warnings;
use Data::Dumper;
use Math::Test1;
# create the Test1 object
my $t_obj = Math::Test1->new( a => 5, b => 4 );
print  "t_obj is - " . Dumper($t_obj);
$t_obj->show();
my $c = $t_obj->addition();
print"addition is $cn";

输出

=======================
结构成员是5和4
t_obj is -$ var1 = bless(do {(我的$ o = 29924112)},'Math :: test1');
=====================
初始:
节数字是5和4
代码:部分
=====================
添加是9

我担心的是为什么对象得到祝福(do {(我的$ o = 29924112)},'Math :: test1');
我是否需要纠正错误地解释输出或打字输出部分。

请给出您的建议。

perl对C数据结构一无所知,因此您将数据结构(29924112)的地址存储在一个祝福的标量中。这正是您应该做的。

现在,您需要创建登录器,以使用该指针访问C数据结构(但是,您创建了一个访问您从未创建过的驼鹿对象的驼鹿访问者)。

您已经编写了它,因此对象存储为C结构,所有Perl都知道它是该结构的地址。这没什么错,但是您的属性A和B也不会为它们提供XS代码。

从其他答案中的评论中,听起来您需要两个对象;最初的一个生活在C结构中,并且在其上执行操作后,一个可访问的对象,不再绑在原始C对象上?

最新更新