karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, here is what i did:

package Karl::Fractals::Mandelbrot; use Moo; use strictures 1; use namespace::clean; use Inline C => 'DATA'; sub reckon { my ( $self, $x, $y ) = @_; mandelbrot( $x, $y ); } has iterations => ( is => 'ro', default => 20 ); has width => ( is => 'ro', default => 1280 ); has height => ( is => 'ro', default => 1024 ); 1; __DATA__ __C__ /* fake */ int mandelbrot(int x, int y) { return x*y; }

 

#!/usr/bin/env perl use strict; use warnings; use lib q(.); use Karl::Fractals::Mandelbrot; use feature qw (say); use Data::Dump; my $Benoit = Karl::Fractals::Mandelbrot->new(); dd $Benoit; say $Benoit->reckon( 2, 7 ); __END__ karls-mac-mini:monks karl$ ./inline_moo.pl bless({ height => 1024, iterations => 20, width => 1280 }, "Karl::Frac +tals::Mandelbrot") 1280 1024 20 14

Somehow related to some of my previous posts.

This very simplified example works as expected.

But i wonder if it is good practice. Perhaps some unexpected traps?

BTW, I didn't use Inline::C except for "Hello World!" until now.

Thank you very much for any hint and best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re: About Moo and Inline::C
by marioroy (Prior) on Jun 10, 2015 at 01:10 UTC

    There are many ways to have fun with Perl + Inline::C. I chose to separate the Perl and C source files. In fact, created Sandboxing with Perl + MCE + Inline::C some time back. The algorithm3.pl script works out of the box. The other two scripts require dependencies mentioned in the README file.

    ./bin/ algorithm3.pl primesieve.pl primeutil.pl ./src algorithm3.c bits.h output.h sandbox.h sprintull.h typemap

    The following is the Inline::C bits inside algorithm3.pl. I chose to hide the .Inline directory under the base dir.

    BEGIN { $ENV{PERL_INLINE_DIRECTORY} = "${base_dir}/.Inline"; mkdir "${base_dir}/.Inline" unless -d "${base_dir}/.Inline"; } use Inline 'C' => Config => CCFLAGSEX => "-I${base_dir}/src -O2 -fsigned-char -fomit-frame-poin +ter", TYPEMAPS => "${base_dir}/src/typemap"; use Inline 'C' => "${base_dir}/src/algorithm3.c";

    Well, this may not be a perfect solution. I am still learning Inline::C.

      Thank you very much for advice marioroy and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: About Moo and Inline::C
by davido (Cardinal) on Jun 10, 2015 at 06:03 UTC

    One problem is where and when the _Inline build directory gets installed. If you don't plan ahead, and you're basing a module on Inline::C, the default behavior isn't usually optimal.

    There is a proof of concept one good way to do this if you look at Math::Prime::FastSieve. (It used to be the easy way until recently, but the method demonstrated in that module has probably been made obsolete.)

    The new easy way uses Inline::Module to assist in creating a distribution that, once installed on the end user's system, looks and feels light-weight, like a plain old XS module. The Inline::Module::Tutorial has pretty good examples that should help you get through the next step.


    Dave

      Hi David. Thank you for the tip on Inline::Module. I played with Math::Prime::FastSieve some time back and made a local version by replacing the primes function with Algorithm3, a practical sieve algorithm by Xuedong Luo. It was a learning experience at the time and got to see primes compute faster.

      Update: If interested, Algorithm3 which I applied to my local copy of Math::Prime::FastSieve.

      Thank you very much for advice davido. I didn't know Inline::Module .

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»