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

Hi, I have written .pm file, that takes the key value pair as input and populates a hash. This code is called repeatedly from a C program that embeds this perl code. e.g.
for (int i = 0; i < 10; i++) { ... perl_call_pv("PerlCode::addKeyValue", G_EVAL|G_SCALAR); ... }
and the perl code is :
sub addKeyValue { ($key, $value} = @_; push @{${$self->{$hash}}{$key}}, $value; }
Now, I want to write another function in Perl, that would modify this hash, (say add another key - value) and I want to return this hash back to the C code. How do I do this? Thanks in advance, Raj

Replies are listed 'Best First'.
Re: Return a hash from Perl to C
by PodMaster (Abbot) on Apr 15, 2003 at 11:44 UTC
    That should be
    perl_call_pv(aTHX_ i, j);
    See perl_call_pv -> call_pv to learn why.

    You should look into get_hv (see perldoc `perlguts', perldoc `perlapi').

    And while you're at it, you might wanna stop by the Extending and Embedding Perl Forum, or even buy the book

    update: Just return a reference to your hash from the function so that it's on the stack, and then just grab it from the stack (POPs).


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Return a hash from Perl to C
by tall_man (Parson) on Apr 15, 2003 at 04:18 UTC
    I think it would be quite ugly for the C code to get the hash back directly. Better would be to write another wrapper function to look up values in the hash and keep it on the perl side.