develooper Front page | perl.inline | Postings from December 2016

Inline::C Cookbook addition

Thread Next
From:
Ron Grunwald via inline
Date:
December 7, 2016 14:44
Subject:
Inline::C Cookbook addition
Message ID:
53887E45-240B-405B-983E-BF81E95F6115@yahoo.com.au
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 => Config => ccflagsex => "-std=c99";
use Inline C => "DATA";

our $mesh_data = "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 = 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=0; ix < N_MP; ix++) {
      z = 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
rongrw@yahoo.com.au
http://www.dvlcorner.org




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About