Yeah, this works! Thanks Vadim!

On Wed, Nov 30, 2016 at 3:18 AM, Konovalov, Vadim <vadim.konova...@dell.com>
wrote:

> You can access package variables directly:
>
>
>
> $::data = "this is a test";
>
> test();
>
>
>
> use Inline C => <<'END_OF_C_CODE';
>
>
>
> void test() {
>
>             printf("here: %s\n", SvPV_nolen(get_sv("::data",0)));
>
> }
>
> END_OF_C_CODE
>
>
>
>
>
> …see “perldoc perlguts”:
>
>     If you know the name of a scalar variable, you can get a pointer to
> its SV
>
>     by using the following:
>
>
>
>         SV*  get_sv("package::varname", 0);
>
>
>
>     This returns NULL if the variable does not exist.
>
>
>
> You can’t access lexical-scoped scalars (“strict” ones) this way, because
> they often do not have a name at all.
>
>
>
> You can place reference of your my-var to package variable though and
> dereference it in your C code.
>
>
>
> Regards,
>
> Vadim.
>
>
>
>
>
>
>
> *From:* Perf Tech [mailto:perfte...@gmail.com]
> *Sent:* Wednesday, November 23, 2016 8:30 AM
> *To:* inline@perl.org
> *Subject:* Question about accessing global data from a line function
>
>
>
> Dear expert,
>
>
>
>    I am trying to access perl global variable ($data in this case) from
> within a inline C function, but the "data" variable I used is not defined.
>
> Any idea how to do it?
>
>
>
> Thanks
>
> Jin
>
>
>
>
>
> $data = "this is a test";
>
> test();
>
>
>
> use Inline C => <<'END_OF_C_CODE';
>
>
>
> void test() {
>
>             printf("here: %s\n", SvPV(data, PL_na));
>
> }
>
>
>
> END_OF_C_CODE
>
>
>

Reply via email to