SV *buildA(A& a) { SV *tmpSv, a_sv; I32 count; int i; dSP; ENTER; SAVETMPS; PUSHMARK; tmpSv = sv_2mortal(newSVpv("A", 0)); XPUSHs(tmpSv); PUTBACK; count = call_method("new", G_SCALAR); // A::new { // my $proto = shift; // my $class = ref($proto) || $proto; // my $this = { }; // bless($this, $class); // $this->{B} = [ ]; // return $this; // } SPAGAIN; assert(1 == count); tmpSv = POPs; a_sv = newSVsv(tmpSv); // or use NEWSV/SvSetMagicSV PUTBACK; FREETMPS; LEAVE; std::vector b_svs; for (i = 0; i < a.b_count; ++i) { tmpSv = buildB(a.b[i]); b_svs.push_back(tmpSv); } addBs(a_sv, b_svs); return a_sv; }