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

You can't XPUSH an array, you have to XPUSH a reference to the array or just XPUSH all of the elements onto the stack. If you want to emulate my_function(@array), then don't build an array, just XPUSH the elements onto the Perl stack.
  • Comment on Re: how do i push an array into the perl stack in C prog before calling perl_call_pv

Replies are listed 'Best First'.
Re: Answer: how do i push an array into the perl stack in C prog before calling perl_call_pv
by Anonymous Monk on May 07, 2001 at 08:25 UTC
    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

      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