I am just looking for a URL or an example of how to return a reference to a C
structure to perl.  I have read the Cookbook, and I can get my code to work if
I actually create a blessed reference.  I would prefer to bless it - I just
want a reference that I can later pass to other C functions.

I have looked at the docs for Inline::Struct, and I do not think that is what
I want.  Again, I do not want a blessed reference, just a reference.  I have
read perlguts, and believe the call I need to use is newRV_noinc.  

What follows are two files.  The first one is a package that is mostly
stolen^wmodeled on the Cookbook OO example.  The main difference is that I do
not bless the structure, I just try to return a ref to it.  The other piece of
code exercises my problem -- namely a segfault.

What have I missed?

Thanks,
Mik

---- The Package ------
package Break;

use Inline C => Config => INC  => '-I/opt/PolicyDirector/include',
                          LIBS => ' -lpthread  -lpdadminapi -llibstdc++';
use Inline C;

sub new {
    my $class = shift;
    my $self = {};
    my %opts = @_;

    $self->{unit} = $opts{unit};
    $self->{obj} = induct( $opts{name}, $opts{rank}, $opts{serial} );

    bless $self, $class;
}

sub get_name {
    my $self = shift;

    getname( $self->{obj} );
}

1;
__DATA__
__C__

typedef struct {
    char *name;
    char *rank;
    long serial;
} Soldier;

SV* induct( char *name, char *rank, long serial ) {
    Soldier* soldier = malloc(sizeof(Soldier));
    
    soldier->name = strdup(name);
    soldier->rank = strdup(rank);
    soldier->serial = serial;

    return newRV_noinc((SV*)soldier);
}

char* getname( SV* obj ) {
    return ((Soldier*)SvIV(SvRV(obj)))->name;
}

------- The program ---------
#!/usr/bin/perl -w

use strict;
use Break;
use Data::Dumper;

my $foo = Break->new( name   => 'mikfire',
                      rank   => 'corporal',
                      serial => 1234567,
                      unit   => '3rd army' );

print $foo->get_name( $foo->{obj} ), "\n";

Reply via email to