in reply to Re: how do i push an array into the perl stack in C prog before calling perl_call_pv
in thread how do i push an array into the perl stack in C prog before calling perl_call_pv

I am trying to implement a provision by which any perl sub cld be called from C ,so i may want to emulate something like my_function("string1",@arr1,@arr2,"string2",@arr3,...). Right now i do XPUSHs(sv_2mortal...) for the string1 then create arr1 by newAV() & then do av_push(arr1..) of SV's within arr1 & then XPUSH(arr1) (which is a ptr as newAV returns AV*) ...but this sequence doesn't help. ( also i tried with/without PUTBACK;av_push();SPAGAIN; as i saw source code for av_push() uses dSP; and does some SVTIED push of SV's into stack. but neither of them helped) Is there something wrong in my sequence.Please do reply. thank you ragu
  • Comment on Re: Answer: how do i push an array into the perl stack in C prog before calling perl_call_pv

Replies are listed 'Best First'.
Re: Re: Answer: how do i push an array into the perl stack in C prog before calling perl_call_pv
by tye (Sage) on May 07, 2001 at 19:47 UTC

    To emulate my_function("string1",@arr1,@arr2,"string2",@arr3) you just need to XPUSH the string, then XPUSH each of the elements of @arr1 (you don't need to build an array), then XPUSH each of the elements of @arr2, then XPUSH the next string, then XPUSH each of the elemnts of @arr3.

    That is, in this code:

    my @arr1= qw(A few things); my @arr2= qw(Two vals); my @arr3= (1,2,3); my_function("string1",@arr1,@arr2,"string2",@arr3); my_function(qw(string1 A few things Two vals string2),1,2,3);
    the last two lines are the same (unless my_function has a prototype).

            - tye (but my friends call me "Tye")
      hi, in that case how wld the perl subroutine demarcate the two arrays arr1 & arr2. ie if we have arr1=(1,2) and arr2=(3,4) how wld the perl subroutine know that arr1's contents ends at 2 and arr2 ends at 4 as i would be doing only XPUSH of 1,2,3,4 before calling the sub through perl_call_pv. any help would be greatly appreciated . thank you ragu

        That is the point: It wouldn't. I suggest you read more about Perl if you think that it would. For example, if you have Perl installed, type "perldoc perlsub" for lots of information about Perl subroutines and how to call them.

                - tye (but my friends call me "Tye")