I know this is a minor nit and doesn't address your actual question, but I find this style of including the C code (or whatever) to
Inline::C to be easier to read and maintain (example untested, but basically using your original code in OP),
use Inline (
C => 'DATA',
BUILD_NOISY => 1,
clean_after_build => 0,
warnings => 10,
);
# perl stuff
# more perl stuff...
__DATA__
__C__
#include <stdio.h>
// checks if array is indeed an arrayref and sets array_sz to its size
+ and
// returns 1 else returns 0 (not an array)
int is_array_ref(
SV *array,
size_t *array_sz
){
if( ! SvROK(array) ){ fprintf(stderr, "is_array_ref() : warning, i
+nput '%p' is not a reference.\n", array); return 0; }
if( SvTYPE(SvRV(array)) != SVt_PVAV ){ fprintf(stderr, "is_array_r
+ef() : warning, input ref '%p' is not an ARRAY reference.\n", array);
+ return 0; }
// it's an array, cast it to AV to get its len via av_len();
// yes, av_len needs to be bumped up, it's $#array
int asz = 1+av_len((AV *)SvRV(array));
if( asz < 0 ){ fprintf(stderr, "is_array_ref() : error, input arra
+y ref '%p' has negative size!\n", array); return 0; }
*array_sz = (size_t )asz;
return 1; // success, it is an array and size returned by ref, abo
+ve
}
int func(
SV *inp,
SV *out
){
AV *av, *av2;
size_t i, j, asz;
if( is_array_ref(out, &asz) ){
printf("Case1: @out\n");
// we have an \@R, e.g. func(\@R)
av = (AV *)SvRV(out);
// but first clear any contents
if( asz > 0 ) av_clear(av);
} else if( SvROK(out) ){
printf("Case3: \\$out\n");
// we have a scalar ref, e.g. func(\$x)
av = newAV();
sv_setsv(SvRV(out), (SV *)av);
} else {
printf("Case2: $out\n");
// we have a scalar e.g func($x);
av = newAV();
sv_setsv(out, (SV *)av);
}
// and fill it in
for(i=0;i<5;i++){
av2 = newAV();
av_extend(av2, 3);
av_push(av, (SV *)av2);
for(j=0;j<3;j++){
av_store(av2, j, newSViv(42));
}
}
return 0; // success
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.