Re: Question about accessing global data from a line function

[email protected] (Ron Grunwald via inline) Thu, 1 Dec 2016 06:26:29 +0800
Newsgroups perl.inline
Message-ID <[email protected]>
--Apple-Mail=_3CAFA8CC-5425-46B7-96BC-085D6673AD57
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hi all,

> You can access package variables directly:
>=20
>    printf("here: %s\n", SvPV_nolen(get_sv("main::data",0)));

Wow OK - I didn't know you could do that. I'm wondering if this could be =
added into the Inline::C Cookbook? Perhaps under the heading "Accessing =
Perl variables directly from a C function".

Rob, are you still maintaining Inline?

Cheers,
Ron.

> On 30 Nov 2016, at 6:25 am, [email protected] wrote:
>=20
> Hi,
>=20
> You can also access the string in $data from C with (untested):
>=20
> SvPV_nolen(get_sv(main::data,0));
> See 'perldoc perlapi' (or maybe it's in 'perldoc perlcall').
>=20
> Cheers,
> Rob
>=20
> From: Ron Grunwald via inline
> Sent: Wednesday, November 30, 2016 1:10 AM
> To: Perf Tech ; [email protected]
> Subject: Re: Question about accessing global data from a line function
> Hi Jin,
>=20
> I don=E2=80=99t think you can access perl variables this way because =
your C function needs some sort of reference to them. Change your code =
to,
>=20
> $data =3D "this is a test";
> test($data);
>=20
>=20
> use Inline C =3D> <<'END_OF_C_CODE';
>=20
>=20
> void test(SV* data) {
> printf("here: %s\n", SvPV(data, PL_na));
> }
>=20
>=20
> END_OF_C_CODE
>=20
> Cheers,
> Ron.
>=20
>=20
> On 23 Nov 2016, at 1:29 pm, Perf Tech <[email protected]> wrote:
>=20
> Dear expert,
>=20
>  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?
>=20
> Thanks
> Jin
>=20
>=20
> $data =3D "this is a test";
> test();
>=20
> use Inline C =3D> <<'END_OF_C_CODE';
>=20
> void test() {
> printf("here: %s\n", SvPV(data, PL_na));
> }
>=20
> END_OF_C_CODE
>=20
>=20
>=20


--Apple-Mail=_3CAFA8CC-5425-46B7-96BC-085D6673AD57
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space;" class=3D"">Hi all,<br =
class=3D""><br class=3D""><blockquote type=3D"cite" class=3D"">You can =
access package variables directly:</blockquote><blockquote type=3D"cite" =
class=3D""><br class=3D""></blockquote><blockquote type=3D"cite" =
class=3D""><font face=3D"Courier" class=3D"">&nbsp; &nbsp;printf("here: =
%s\n", SvPV_nolen(get_sv("main::data",0)));</font></blockquote><div =
class=3D""><br class=3D""></div>Wow OK - I didn't know you could do =
that. I'm wondering if this could be added into the Inline::C Cookbook? =
Perhaps under the heading "Accessing Perl variables directly from a C =
function".<div class=3D""><br class=3D""></div><div class=3D"">Rob, are =
you still maintaining Inline?</div><div class=3D""><br =
class=3D""></div><div class=3D"">Cheers,</div><div class=3D"">Ron.<br =
class=3D""><br class=3D""><blockquote type=3D"cite" class=3D"">On 30 Nov =
2016, at 6:25 am, <a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a> wrote:<br class=3D""><br =
class=3D"">Hi,<br class=3D""><br class=3D"">You can also access the =
string in $data from C with (untested):<br class=3D""><br =
class=3D"">SvPV_nolen(get_sv(main::data,0));<br class=3D"">See 'perldoc =
perlapi' (or maybe it's in 'perldoc perlcall').<br class=3D""><br =
class=3D"">Cheers,<br class=3D"">Rob<br class=3D""><br class=3D"">From: =
Ron Grunwald via inline<br class=3D"">Sent: Wednesday, November 30, 2016 =
1:10 AM<br class=3D"">To: Perf Tech ; <a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a><br class=3D"">Subject: Re: Question about =
accessing global data from a line function<br class=3D"">Hi Jin,<br =
class=3D""><br class=3D"">I don=E2=80=99t think you can access perl =
variables this way because your C function needs some sort of reference =
to them. Change&nbsp;your code to,<br class=3D""><br class=3D"">$data =3D =
"this is a test";<br class=3D"">test($data);<br class=3D""><br =
class=3D""><br class=3D"">use Inline C =3D&gt; =
&lt;&lt;'END_OF_C_CODE';<br class=3D""><br class=3D""><br class=3D"">void =
test(SV* data) {<br class=3D"">printf("here: %s\n", SvPV(data, =
PL_na));<br class=3D"">}<br class=3D""><br class=3D""><br =
class=3D"">END_OF_C_CODE<br class=3D""><br class=3D"">Cheers,<br =
class=3D"">Ron.<br class=3D""><br class=3D""><br class=3D"">On 23 Nov =
2016, at 1:29 pm, Perf Tech &lt;<a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:<br class=3D""><br =
class=3D"">Dear expert,<br class=3D""><br class=3D"">&nbsp;I am trying =
to access perl global variable ($data in this case) from within a inline =
C function, but the "data" variable I used&nbsp;is not defined.<br =
class=3D"">Any idea how to do it?<br class=3D""><br class=3D"">Thanks<br =
class=3D"">Jin<br class=3D""><br class=3D""><br class=3D"">$data =3D =
"this is a test";<br class=3D"">test();<br class=3D""><br class=3D"">use =
Inline C =3D&gt; &lt;&lt;'END_OF_C_CODE';<br class=3D""><br =
class=3D"">void test() {<br class=3D"">printf("here: %s\n", SvPV(data, =
PL_na));<br class=3D"">}<br class=3D""><br class=3D"">END_OF_C_CODE<br =
class=3D""><br class=3D""><br class=3D""><br class=3D""></blockquote><br =
class=3D""></div></body></html>=

--Apple-Mail=_3CAFA8CC-5425-46B7-96BC-085D6673AD57--