in reply to Re^6: Inline CPP
in thread Inline CPP

The following Inline::CPP script seems to do what you need on perl-5.8.8 (but not on perl-5.10.0):
use warnings; use Inline CPP => Config => BUILD_NOISY => 1; use Inline CPP => <<'EOC'; void add_nir(int x, int y, AV * perlList) { SV* perlItem=newSViv(2); av_push(perlList, perlItem); } EOC my @test = (1, 2); print "@test\n"; my $reftest = \@test; add_nir(1, 4, $reftest); print "@test\n";
On perl-5.8.8 that correctly outputs:
1 2 1 2 2
and you can see that the "2" was, in fact, pushed onto the array. But, on perl-5.10.0 I get Can't locate auto/main/add_nir.al in @INC ... which is similar to what you are getting. Apparently, one can't supply an AV* arg to an Inline::CPP function on perl-5.10.0, though there's no such problem with perl-5.8.8. I don't know whether this is an Inline::CPP bug or a perl bug - probably Inline::CPP.

The above script also works fine on perl-5.10.0 as an Inline::C script - do you really need to use Inline::CPP ? (There's a useful example of doing object-oriented inline using Inline::C in 'perldoc Inline::C-Cookbook'. Look for "Soldier".)

If you really do need Inline::CPP, you're in luck - there's a simple rewrite of the above script that has add_nir() take an SV instead of an AV, and it works fine with Inline::CPP on perl-5.10.0:
use warnings; use Inline CPP => Config => BUILD_NOISY => 1; use Inline CPP => <<'EOC'; void add_nir(int x, int y, SV * perlList) { SV* perlItem=newSViv(2); av_push((AV*)SvRV(perlList), perlItem); } EOC my @test = (1, 2); print "@test\n"; my $reftest = \@test; add_nir(1, 4, $reftest); print "@test\n";
At least that works fine for me on perl-5.10.0. Note that the perl code is exactly the same as in the first script. The only difference is in the way the add_nir() function has been constructed.

Cheers,
Rob

Replies are listed 'Best First'.
Re^8: Inline CPP
by nirf1 (Novice) on Jun 03, 2008 at 07:32 UTC
    Thanks Rob, It helped. I did the option you wrote (with one level referencing) and it worked.
    void add_nir(int x, int y, AV * perlList) { SV* perlItem=newSViv(2); av_push(perlList, perlItem); }
    I understand that I shouldn't have initialized the AV* inside the cpp code, but only outside in the perl code as you did. Thanks again!
      As I mentioned in my previous post, that rendition of add_nir() won't work with perl 5.10. If that's a concern, then just switch to the other rendition provided in that last post of mine.

      In the meantime, I've gone digging into the Inline::CPP source, found the problem, and made out a bug report about the issue (which includes a fix). That bug report and fix can be found at http://rt.cpan.org//Ticket/Display.html?id=36421 . As I've mentioned there, I really don't know whether the fix I've provided is the correct one.

      Cheers,
      Rob