We have embedded perl with c++ on a win32 environment to be able to use scripting power in one of our applications. We are able to read-write from perl to C++ and vice versa. We are trying to pass a structure from C++ to perl but are in failure. We can pass variables without a problem. We are still trying to figure some stuff out. We will have nearly 1 million members in our tree, so passing 1 by 1 is out of the question. Are we suppose to map them all 1 by 1 as well in our C++ code ? Isnt there a simpler or faster way of doing this ?? Please remember we are Win32 programmers and not Perl programmers, though we have had some perl experience prior version 5.

We are not expecting full codes. Please just point us in the right direction. Thank you :) !!


The structure we want is something like this
struct somestruct <----------- { int count; <--- This number could be from 25 to 25000 someotherstruct * pdata; <--- Obviously this number will match as +well } struct someotherstruct { <--- This structure can contain alot ... vectorstruct vSomething; vectorstruct vSomethingElse; float, int, char, and what not... } ...
Below is our testing code, mostly based on Perldoc, PerlEmbed and PerlCall
Link

************ // all the rest is temp : figuring this perl thing out... char * cpArguments[] = { "", "test1.pl" }; my_perl = perl_alloc(); ISNOTNULLRETURN(my_perl, "WinMain : NULL my_perl", "Error", -1); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, 2, cpArguments, (char **)NULL); // run script // perl_run(my_perl); // run sub from script // char * cpArgumentsToSub[] = { NULL }; // call_argv("OnUpdate", G_DISCARD | G_NOARGS, cpArgumentsToSub); // run sub from script passing 2 vars into @_ (simple test). // ref http://www.apl.jhu.edu/Misc/Unix-info/perl/perlcall.html { SAFEMALLOCI(char, cpBuf, "WinMain : memory allocation failed", -1, + 1000); int a = 243; int b = 527; dSP; int count; SV * sva; //= NULL ? SV * svb; //= NULL ? ENTER; SAVETMPS; sva = sv_2mortal(newSViv(a)); svb = sv_2mortal(newSViv(b)); PUSHMARK(sp); XPUSHs(sva); XPUSHs(svb); PUTBACK; count = perl_call_pv("OnUpdate", G_DISCARD); if (count != 0) { croak("wtf\n"); BOX("wtf\n", "Error"); } int newa = SvIV(sva); int newb = SvIV(svb); sprintf (cpBuf, "A [ original = %d modified = %d ]\nB [ original = + %d modified = %d ]", a, newa, b, newb); BOX(cpBuf, "Debug"); FREETMPS; LEAVE; SAFEFREE(cpBuf); } perl_destruct(my_perl); perl_free(my_perl);
Our perl script :

#!perl use strict; sub OnUpdate { ######################################## # get data ######################################## my ($a, $b) = @_; ######################################## # code using the names ######################################## $a = 5; $b = 3; ######################################## # send data ######################################## $_[0] = $a; $_[1] = $b; }


So having $_[0] to $_[possibly up to 1 million] sounds like a very bad idea to us. We think perl supports structure like variables. We have seen things like, $bleh->sub.
This would be something we would want :
my $data = shift; for (my $i=0; $i<$data->count; $i++) { $data->pdata[$i]->vsomething->x = $whatever; }
Obviously again :), this creates a problem with the "send data" part of the perl script.

Would there be a way to directly modify the memory wihtout reassigning $_[n].
We are not expecting full codes. Please just point us in the right direction. Thank you :) !!




Any help is very appreciated. Thank you for reading

In reply to Embedding Perl / C++ structure question by TheGeniuS

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.