Perl XS : What is the meaning of this dumped value for object - t_obj is - $VAR1 = bless( do{\(my $o = 41032464)}, 'Math::Test1' );

StackOverflow https://stackoverflow.com/questions/21519681

  •  06-10-2022
  •  | 
  •  

Question

I tried to write the perl code which use C APIs using Perl XS. I was getting the Dump output of object as -

t_obj is - $VAR1 = bless( do{(my $o = 41032464)}, 'Math::Test1' );

I wrote the simple library called Math::Test1 for practice though my modules are different and more complex than this.

Looks like the conversion from perl to C is fine but when i am getting the data back fom C to perl, the data is in this puzzled form while i expect the attributes of the object with some added information.

Pasting my code -

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 %d\n", RETVAL->x, RETVAL->y);

    OUTPUT:
    RETVAL


SV *
display(in)
    Math::Test1  in

    INIT:
        printf("INIT: Section\n");
        printf("number is %d and %d\n", in->x, in->y);

    CODE:
        printf("CODE:Section\n");


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);

Math/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 (Test file)

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 $c\n";

OUTPUT

==========CONSTRUCTOR=========
struct members are 5 and 4
t_obj is - $VAR1 = bless( do{(my $o = 29924112)}, 'Math::Test1' );
==========DISPLAY=========
INIT: Section
number is 5 and 4
CODE:Section
==========ADDITION=========
addition is 9

My concern is Why the object has bless( do{(my $o = 29924112)}, 'Math::Test1' );
Do i interpreting the output wrongly or typemap OUTPUT section needs to be corrected.

Kindly give your suggestions on this.

Was it helpful?

Solution

Perl doesn't know anything about C data structures, so you stored the address of the data structure (29924112) into a blessed scalar. That's exactly what you should have done.

Now you need to create accessors to access the C data structure using that pointer (but instead you created a Moose accessors that access a Moose object you never created).

OTHER TIPS

You have written it so the object is stored as a C structure and all perl knows about it is the address of that structure. There's nothing wrong with that, but your attributes a and b are not going to work without providing XS code for them too.

From your comments in the other answer, it sounds like you want two objects; an initial one living in a C structure, and after an operation is performed on it, a Perl-accessible object, no longer tied to the original C object?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top