in reply to Arrays and Inline C

I'm not an Inline expert, but here are some examples I have from previous experimenting. The thing you need to consider is the type of data in the arrays, Perl takes care of it for you automatically, but in C, you need to be exact.
#!/usr/bin/perl use warnings; use strict; use Inline C=>'DATA'; my @array = ( 1 .. 100 ); print get_an_element(\@array)."\n"; __DATA__ __C__ SV* get_an_element(SV* aref) { AV* array; if (! SvROK(aref)) croak("aref is not a reference"); array = (AV*)SvRV(aref); SV** v = av_fetch(array,8, 0); if( v == NULL ) croak("not enough elements in array"); SV* ret = *v; SvREFCNT_inc(ret); return ret; } __END__
#!/usr/bin/perl use warnings; use Tk; use Inline C; use strict; $| = 1; my $mw = tkinit; $mw->Button( -text => 'Get Array from C', -command => sub { &get_data(); })->pack(); $mw->Button( -text => 'Quit', -command => sub { exit; })->pack(); MainLoop; ######################################## sub get_data { my $freq_adj = .1; my $vol = .5; my @raw = gen($freq_adj, $vol); #print "@raw\n\n"; foreach(@raw){ print $_,"\n"; } } __END__ __C__ #include <stdio.h> #include <math.h> /* function must return void for Inline to use the stack See the Inline_C cookbook */ void gen(float freq_inc , float vol) { /* two numbers to work with */ float result, rad; rad = .1; Inline_Stack_Vars; Inline_Stack_Reset; while ( rad < 6.3 ) { rad = rad + freq_inc; result = vol * 32768 * sin(rad); Inline_Stack_Push(sv_2mortal(newSVnv(result))); /* newSVnv for floats, newSViv for ints, see perldoc perlguts * +/ } Inline_Stack_Done; }
#!/usr/bin/perl use warnings; use Tk; use Inline C; use strict; $| = 1; #this ones returns ints, not floats my $mw = tkinit; $mw->Button( -text => 'Get Array from C', -command => sub { &get_data(); })->pack(); $mw->Button( -text => 'Quit', -command => sub { exit; })->pack(); MainLoop; ###################################### sub get_data { my $freq_adj = .1; my $vol = .5; my @raw = gen($freq_adj, $vol); print "@raw\n\n"; } __END__ __C__ #include <stdio.h> #include <math.h> /* function must return void for Inline to use the stack See the Inline_C cookbook */ void gen(float freq_inc , float vol) { /* two numbers to work with */ float result, rad; rad = .1; Inline_Stack_Vars; Inline_Stack_Reset; while ( rad < 6.3 ) { rad = rad + freq_inc; result = vol * 32768 * sin(rad); /* printf("%6.6f\n",result); */ Inline_Stack_Push(sv_2mortal(newSViv(result))); } Inline_Stack_Done; }
#!/usr/bin/perl use warnings; # by Tachyon of perlmonks # The easiest way to access arrays and hashes when you # are using Inline C or XS is to pass by reference and # use the access macros to get at the values. Here is a # trivial example to get you started. use Inline 'C'; print multiply( [ 1,2,3,4 ] ); __END__ __C__ double multiply( SV * terms ) { I32 numterms = 0; int i; double result; /* Make sure we have an array ref with values */ if ((!SvROK(terms)) || (SvTYPE(SvRV(terms)) != SVt_PVAV) || ((numterms = av_len((AV *)SvRV(terms))) < 0)) { return 0; } /* Set result to first value in array */ result = SvNV(* av_fetch((AV *)SvRV(terms), 0, 0)); for (i = 1; i <= numterms; i++) { result *= SvNV(* av_fetch((AV *)SvRV(terms), i, 0)); } return result; }

I'm not really a human, but I play one on earth Remember How Lucky You Are