Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an array from perl to inline C++?

Tags:

c++

inline

perl

I can't tell if this is a bug or what.

The following code works with Inline::C but not Inline::CPP

#!/usr/bin/perl
use warnings;
use Inline C;

my @array = (1..10);
print findAvLen(\@array), "\n";

__END__
__C__

int findAvLen(AV* arrayIn) {
    return av_len(arrayIn);
}

The above runs fine, but replace C with CPP, and I get the error Can't locate auto/main/findAvLen.al in @INC...

I can get other inline c++ code to work. It is possible, for instance, to pass a list of variables to inline code with an ellipsis as they do in this example, but I wonder why AV* isn't working! For instance, I will want to use a subroutine to converts perl arrays passed to C++ into vectors, e.g. void perl2vector(AV* ar, std::vector<T> &v) {...}, rather than inserting code to perform such conversion into every C++ function I write that takes an array argument. How could I use that example's syntax to pass the perl array to such a converter?


This does seem to be a bug, but in the meantime, uesp has found a workaround:

int findAvLen(SV* arrRef) { 
    AV * arr = MUTABLE_AV(SvRV(arrRef));
    return av_len(arr);
}

arr is now equivalent to the arrayIn desired in the example code above.

like image 432
flies Avatar asked Apr 04 '12 17:04

flies


1 Answers

This appears to be be identical to this Perl bug which exists in 5.10.0 and should have been fixed in release 5.10.29. If you are running 5.10.0-28 try updating. If you aren't running these versions and still getting the error you can try doing what is mentioned in this forum post by changing the AV to SV.

like image 60
uesp Avatar answered Oct 27 '22 18:10

uesp