develooper Front page | perl.perl5.porters | Postings from December 1999

Re: Returning An Array from an Extension

Thread Previous
Nick Ing-Simmons
December 12, 1999 10:33
Re: Returning An Array from an Extension
Message ID:
Aby Paul <> writes:
>I have the following code snippet which should return an array. 

It stuffs a reference to an array into place 3rd arg was i.e. as though 
you had :

sub foo
 my @args = @_;
 $args[2] = [0..4];

>This doesn't seem to work. 
>Could someone tell me what is wrong in this code?
>SV *retsv=NULL;
>AV *av;
>int i = 5;
>av = newAV();
>retsv = newRV(sv_2mortal((SV*)av));

Why not use newRV_noinc() instead.

>while(--i >= 0) 
>  av_store(av, i, newSViv(i));

What is XSRETURN'ed ?

>When called from the script, 

How is it called from the script?

>the length is always returned as 1. 
>But I am unable to see the contents. When @x and $z = $x[0] are
>printed, it shows nothing.
>If I replace 
>ST(2)=sv_2mortal(retsv); with sv_setsv(ST(returnIndex),sv_2mortal(retsv));

That is like 
  $_[2] = [0..4];

which makes a little more sense. But you did not whant that made 'mortal'.
Mortal-ness is (mainly) to free up stuff on the _stack_, not stored in 

Can you supply perl code that does what you are trying to do
and I/we will attempt to translate ;-)

Are you trying to do : 

my $ref = foo();   # ST(0) = sv_2mortal(newRV_noinc(av)); XSRETURN(1);

my @data = foo();  # ST(0) = sv_2mortal(newSViv(0)); 
                   # ST(1) = sv_2mortal(newSViv(1));
                   # ... 
                   # XSRETURN(5);  

foo(\@data);       # av = SvRV(ST(n));
                   # av_store(av,i,newSViv()); 
                   # ... 
                   # XSRETURN(0);

or ...

Nick Ing-Simmons

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About