Inline::C Cookbook addition

[email protected] (Ron Grunwald via inline) Wed, 7 Dec 2016 22:44:26 +0800
Newsgroups perl.inline
Message-ID <[email protected]>
--Apple-Mail=_37E82C1F-A0F5-44F9-BCF1-790C48FD3F22
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hello all,

Following the recent discussion about accessing Perl variables directly =
from a C function, I've put together some sample code that I think would =
be appropriate to add into the Inline::C Cookbook. I thought to present =
the example here first to allow users to comment or provide suggestions.

The code was tested in the following environments,
- Linux Fedora 20.0, ActivePerl 5.14.4, Inline::C 0.76
- OS X Yosemite 10.10.5, ActivePerl 5.24.0, Inline::C 0.76

The code example:

use strict;
use warnings;
use Inline C =3D> Config =3D> ccflagsex =3D> "-std=3Dc99";
use Inline C =3D> "DATA";

our $mesh_data =3D "MESH-POINTS 0.0 0.0 0.5 0.25 1.0 0.5 1.5 0.75";
CalcSurfaceHeights();

__DATA__
__C__
#define N_MP 4

void CalcSurfaceHeights() {
   double x[N_MP], y[N_MP], z;
   char   *mesh_data =3D SvPV_nolen(get_sv("main::mesh_data", 0));

   sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
                     x, y, x+1, y+1, x+2, y+2, x+3, y+3);

   for (int ix=3D0; ix < N_MP; ix++) {
      z =3D 0.5*( sin(x[ix]) + sin(y[ix]) );

      printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
             z, x[ix], y[ix]);
   }
}

________________________________________
Ron Grunwald
[email protected]
http://www.dvlcorner.org




--Apple-Mail=_37E82C1F-A0F5-44F9-BCF1-790C48FD3F22
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D"">Hello all,<div class=3D""><br class=3D""></div><div =
class=3D"">Following the recent discussion about accessing Perl =
variables directly from a C function, I've put together some sample code =
that I think would be appropriate to add into the Inline::C Cookbook. I =
thought to present the example here first to allow users to comment or =
provide suggestions.</div><div class=3D""><br class=3D""></div><div =
class=3D"">The code was tested in the following environments,</div><div =
class=3D"">- Linux Fedora 20.0, ActivePerl 5.14.4, Inline::C =
0.76</div><div class=3D"">- OS X Yosemite 10.10.5, ActivePerl 5.24.0, =
Inline::C 0.76</div><div class=3D""><br class=3D""></div><div =
class=3D"">The code example:</div><div class=3D""><br =
class=3D""></div><div class=3D""><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">use strict;</font></div><div =
class=3D""><font face=3D"Courier" style=3D"font-size: 12px;" =
class=3D"">use warnings;</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D"">use Inline C =
=3D&gt; Config =3D&gt; ccflagsex =3D&gt; "-std=3Dc99";</font></div><div =
class=3D""><font face=3D"Courier" style=3D"font-size: 12px;" =
class=3D"">use Inline C =3D&gt; "DATA";</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D""><br =
class=3D""></font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">our $mesh_data =3D "MESH-POINTS =
0.0 0.0 0.5 0.25 1.0 0.5 1.5 0.75";</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" =
class=3D"">CalcSurfaceHeights();</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D""><br =
class=3D""></font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">__DATA__</font></div><div =
class=3D""><font face=3D"Courier" style=3D"font-size: 12px;" =
class=3D"">__C__</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">#define N_MP 4</font></div><div =
class=3D""><font face=3D"Courier" style=3D"font-size: 12px;" =
class=3D""><br class=3D""></font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D"">void =
CalcSurfaceHeights() {</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp;double x[N_MP], =
y[N_MP], z;</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp;char &nbsp; =
*mesh_data =3D SvPV_nolen(get_sv("main::mesh_data", =
0));</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D""><br class=3D""></font></div><div =
class=3D""><font face=3D"Courier" style=3D"font-size: 12px;" =
class=3D"">&nbsp; &nbsp;sscanf(mesh_data, "MESH-POINTS =
%lf%lf%lf%lf%lf%lf%lf%lf",</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x, y, x+1, =
y+1, x+2, y+2, x+3, y+3);</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D""><br =
class=3D""></font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp;for (int ix=3D0; ix =
&lt; N_MP; ix++) {</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp; &nbsp; z =3D 0.5*( =
sin(x[ix]) + sin(y[ix]) );</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D""><br =
class=3D""></font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp; &nbsp; =
printf("Surface-Height: %6.3f Mesh-Point: %6.2f, =
%6.2f\n",</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;z, x[ix], y[ix]);</font></div><div class=3D""><font =
face=3D"Courier" style=3D"font-size: 12px;" class=3D"">&nbsp; =
&nbsp;}</font></div><div class=3D""><font face=3D"Courier" =
style=3D"font-size: 12px;" class=3D"">}</font></div><div class=3D""><br =
class=3D""></div><div apple-content-edited=3D"true" class=3D"">
<div style=3D"color: rgb(0, 0, 0); letter-spacing: normal; orphans: =
auto; text-align: start; text-indent: 0px; text-transform: none; =
white-space: normal; widows: auto; word-spacing: 0px; =
-webkit-text-stroke-width: 0px; word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D""><div =
class=3D"">________________________________________</div><div =
class=3D""><div style=3D"margin: 0px;" class=3D"">Ron Grunwald</div><div =
style=3D"margin: 0px;" class=3D""><a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a></div><div style=3D"margin: 0px;" =
class=3D""><a href=3D"http://www.dvlcorner.org" =
class=3D"">http://www.dvlcorner.org</a></div></div><div class=3D""><br =
class=3D""></div></div><br class=3D"Apple-interchange-newline">

</div>
<br class=3D""></div></body></html>=

--Apple-Mail=_37E82C1F-A0F5-44F9-BCF1-790C48FD3F22--