There's more than one way to do things | |
PerlMonks |
Perl array with SWIGby airween (Initiate) |
on Jun 05, 2016 at 10:35 UTC ( [id://1164929]=perlquestion: print w/replies, xml ) | Need Help?? |
airween has asked for the wisdom of the Perl Monks concerning the following question:
Hi there, I'm not a Perl expert (I mean the Perl as script lang, and C API), but I would like to solve a problem. There is a library, written in C, and there are several bindings (Perl, Python, Tcl). SWIG is used to generate the bindings. I found a bug in this part of code: if the type of a variable is "int [NUMBER]" (a fixed length array), then that variable isn't accessible through bindings. I found a solution for Python (I'm working in Python in many projects - both with interpreter and with C API). I'd like to solve this for Perl too, but it doesn't work. Here is the (last) code, what I try: #ifdef SWIGPERL %typemap(out) int [ANY] { AV * av = newAV(); int i = 0,len = 0; printf("len: %d\n", $1_dim0); len = $1_dim0; for (i = 0; i < len ; i++) { SV* perlval = newSV(0); sv_setiv(perlval, (IV)$1i); av_store(av, i, perlval); } $result = newRV_noinc((SV *)av); } #endifThe name of the variable is "m" in C source, and that a structure. It has a member, called "ilist" (integer list). In Python, I can access to this list like this: print m.ilist // produces [0, 3, 6, ....]In Perl, I just get this: #!/usr/bin/perl use example1; my @m = $example1::m->{ilist}; foreach(@m) { print $i, "'", $_, "'\n"; $i=$i+1; }this produces: 0''and nothing more. What em'I missing? Any helps are welcome! Thanks, a.
Back to
Seekers of Perl Wisdom
|
|