in reply to Calling PERL from C code - How to "build" a hash on the PERL stack before calling the function?

In perlguts,Working With HVs, the code to create a new HV ("Hash Variable"?) is:

HV* my_hash; my_hash= newHV();

Then, you use hv_store() and hv_fetch to read/write the keys and values of the hash.

  • Comment on Re: Calling PERL from C code - How to "build" a hash on the PERL stack before calling the function?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Calling PERL from C code - How to "build" a hash on the PERL stack before calling the function?
by itamarat (Acolyte) on Jul 07, 2014 at 09:12 UTC

    Hi,

    You have helped a lot! Thank you!

    How do I push the HV to the PERL stack?
    Currently I use:
    XPUSHs(sv_2mortal(newSViv((unsigned int)cbCtx)));

    To push integer to the stack. I've searched for 'newSVhv' and it is not present. How should it be done?

    Thanks again!

    Itamar

      perlcall has a section on Returning a List of Values. If you don't want to flatten the hash into a list of values, consider returning a reference to the hash, by returning newSVrv(HV) (or whatever the magic C incantation would be, according to perlguts.

      You can't push an HV onto the stack. Perl functions are only ever passed a list of scalars!

      You can however pass a reference to a hash, which the Perl function can dereference to access the original hash.

      Or you can pass alternating pairs of keys and values onto the stack. In which case, the Perl function can assign @_ to a lexical hash (my %args = @_).