vr has asked for the wisdom of the Perl Monks concerning the following question:
I want to run a script at computer, where I'd rather not expose its (this script's) C guts (embarrassing situation... sorry). Consider SSCCE of myscript.pl:
use strict; use warnings; use feature 'say'; use Inline C => << 'END'; int add (int x, int y) { return x + y; } END say add( 5, 7 );
I know nothing about XS; and (another alternative) though I think I can compile C to a DLL and then use FFI, I suspect it would be less efficient: script is run many times -- as e.g. $^X myscript.pl -- by larger continuously running Perl application. So I'd prefer to stay with Inline::C, but 'hide' the algo realisation.
There's FAQ topic, but it's (1) not exactly about what I need, and thus not achieves what I want, (2) I found it's not exact in what it describes -- no need to split Perl and C into separate files, no need to bother with MD5.
Nevertheless, using FAQ as base and example Math::Simple bundled with Inline:
mkdir MyC, place 2 files there:
Makefile.PL:
use Inline::MakeMaker; WriteMakefile( NAME => 'myC', VERSION_FROM => 'myC.pm' );
myC.pm:
package myC; our $VERSION = 1; use Inline C => << 'END', VERSION => 1, NAME => 'myC'; int add (int x, int y) { return x + y; } END 1;
then run perl Makefile.PL, gmake test. The blib directory is created, where I can delete everything but 2 files:
blib/arch/auto/myC/myC.xs.dll
blib/lib/myC.pm
the latter I modify as ('END' can be anything, no need to write MD5):
package myC; our $VERSION = 1; use Inline C => 'END', VERSION => 1, NAME => 'myC'; 1;
Then I modify original myscript.pl as:
use strict; use warnings; use feature 'say'; use blib; use myC; say myC::add( 5, 7 );
Then move it to some other location for testing, bundled with blib folder (which itself is 4 folders, 2 files). Looks like goal achieved, but I wonder if there is 'more correct' way. In particular, I'd like to avoid creating dummy module and package (myC), or at least have it declared inside myscript.pl itself. 2nd point, I'd like the dll to be placed e.g. into "standard" _Inline directory with "standard" Inline hierarchy and naming conventions, since this folder already exists anyway (for parts which I don't bother to 'hide'. Like I said the whole situation is complicated and embarrassing), so there's no need to use blib;. Maybe there are Inline configuration options for that?
|
---|