Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Perl array with SWIG

by 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);
}
#endif
The 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.

Replies are listed 'Best First'.
Re: Perl array with SWIG
by choroba (Cardinal) on Jun 05, 2016 at 12:41 UTC
    Crossposted to StackOverflow. It's considered polite to inform about crossposting so that people not attending both sites don't waste their efforts hacking a solution to a problem already solved at the other end of the internet.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Perl array with SWIG
by airween (Initiate) on Jun 07, 2016 at 14:03 UTC
    Okay, at my crossposted post, there is a solution - I just screwed up the assignment... :( http://stackoverflow.com/questions/37634442/swig-typedef-perl-array/37667575#37667575 All of other components were right. Sorry guys, chers, a.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1164929]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found