Re: Question about accessing global data from a line function
[email protected] Wed, 30 Nov 2016 09:25:42 +1100
| Newsgroups | perl.inline |
|---|---|
| Message-ID | <9E1692D2188B4D3DAB0C3EBA982D0F41@OwnerPC311012> |
Hi, You can also access the string in $data from C with (untested): SvPV_nolen(get_sv(main::data,0)); See 'perldoc perlapi' (or maybe it's in 'perldoc perlcall'). Cheers, Rob 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, I don’t think you can access perl variables this way because your C function needs some sort of reference to them. Change your code to, $data = "this is a test"; test($data); use Inline C => <<'END_OF_C_CODE'; void test(SV* data) { printf("here: %s\n", SvPV(data, PL_na)); } END_OF_C_CODE Cheers, Ron. On 23 Nov 2016, at 1:29 pm, Perf Tech <[email protected]> wrote: 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