Domanda

Sto cercando di creare un hash Perl all'interno di una libreria C. Ecco cosa ho ottenuto finora:

static void add_string_to_perl_hash ( HV *hv, char * key, char *value ) {

SV *obj = sv_2mortal(newSVpv(value, 0));

hv_store(hv, (const char *)key, strlen (key), obj, 0);

SvREFCNT_inc(obj);

}

SV * do_get_test_hash () {

    static char *foo ="foo";
    static char *bar ="bar";

    HV *hv;

    hv = newHV();
    add_string_to_perl_hash ( hv, "foo",   foo);
    add_string_to_perl_hash ( hv, "bar",   bar);

    return sv_2mortal(newRV_noinc((SV*)hv));
}

Provandolo: non ho niente che abbia alcun senso per me:

use testlib;
use Data::Dumper;

print Dumper (testlib::do_get_test_hash());

$VAR1 = bless( do{\(my $o = 5359872)}, '_p_SV' );

idee?

È stato utile?

Soluzione

dai un'occhiata all'esempio 6 di perlxstut . Crea un mucchio di hash e li aggiunge a un array. Alla fine, restituisce un riferimento all'array. Funzionerebbe praticamente allo stesso modo se restituissi un hash.

Altri suggerimenti

Credo che tu debba spingere il valore che vuoi tornare nello stack, non restituirlo dalla funzione, ma sono abituato a XS piuttosto che a SWIG.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top