Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to return list from Inline::C code?

Reply
Thread Tools

How to return list from Inline::C code?

 
 
kj
Guest
Posts: n/a
 
      07-29-2005



I'm attempting to hack the Perl internals, something I know very
little of. So I apologize ahead of time for the cluelessness of
this question.

I want to implement a function via Inline::C such that, depending
on context, it returns either a scalar or a list of two values.
(This function will access the internals of other variables, and I
want to make it as fast as possible, hence the decision to code it
in C).

Since C is not capable of optionally returning a scalar or a list,
I don't even know where to begin with this.

Any advice would be much appreciated. In particular, I'd love to
look at source code for a simple function (in C) that returns a
scalar or a list according to calling context.

Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      07-30-2005

"kj" <(E-Mail Removed)> wrote in message
news:dce677$blb$(E-Mail Removed)...
>
>
>
> I'm attempting to hack the Perl internals, something I know very
> little of. So I apologize ahead of time for the cluelessness of
> this question.
>
> I want to implement a function via Inline::C such that, depending
> on context, it returns either a scalar or a list of two values.
> (This function will access the internals of other variables, and I
> want to make it as fast as possible, hence the decision to code it
> in C).
>
> Since C is not capable of optionally returning a scalar or a list,
> I don't even know where to begin with this.
>
> Any advice would be much appreciated. In particular, I'd love to
> look at source code for a simple function (in C) that returns a
> scalar or a list according to calling context.
>


As William Ahern pointed out, GIMME_V seems to be your friend here:

D:\pscrpt\inline\special>type gimme_v.pl

use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;

use Inline C => <<'EOC';

void foo() {
Inline_Stack_Vars;

if(GIMME_V == G_SCALAR) {
printf("%s\n", "return scalar");
Inline_Stack_Reset;
Inline_Stack_Push(sv_2mortal(newSViv(42)));
Inline_Stack_Done;
Inline_Stack_Return(1);
}

if(GIMME_V == G_ARRAY) {
printf("%s\n", "return array");
Inline_Stack_Reset;
Inline_Stack_Push(sv_2mortal(newSViv(65537)));
Inline_Stack_Push(sv_2mortal(newSViv(65539)));
Inline_Stack_Done;
Inline_Stack_Return(2);
}

if(GIMME_V == G_VOID) {
printf("Nothing to do ... \n");
Inline_Stack_Void;
}

}



EOC

$x = foo();
print $x, "\n\n";

@y = foo();
print "@y\n\n";

foo();

__END__
D:\pscrpt\inline\special>perl gimme_v.pl
return scalar
42

return array
65537 65539

Nothing to do ...

Cheers,
Rob


 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      07-30-2005

Many thanks for all the replies! I'm in business now.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why doesn't python's list append() method return the list itself? dhruvbird Python 16 07-16-2010 05:40 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Why list.sort() don't return the list reference instead of None? ankyhe@gmail.com Python 11 05-09-2006 12:43 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments