Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i do a newAV() and using av_push() push data into the array and then i want to do a push ( like XPUSHs for sv's). but i donot find any equivalent macro. any help will be greatly appreciated. thank you ragu ( ragup@hp.india.com)
  • Comment on how do i push an array into the perl stack in C prog before calling perl_call_pv

Replies are listed 'Best First'.
Re: how do i push an array into the perl stack in C prog before calling perl_call_pv
by tye (Sage) on May 04, 2001 at 18:40 UTC
    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.
      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")