For a third approach to Inline, there is Python's weave.inline:

  http://www.scipy.org/site_content/weave/tutorial.html#Inline

Here is what it might look like in perl:

   use Inline;

   sub foo {
     my($qux) = @_;
     $qux = int(0+ $qux);  # make sure it's an int.
     my $c_code = <<"  END";
     printf("%d\n",qux);
     END
     Inline::inline($c_code,['$qux']);
   }

   sub cplusplus_sort {
     my($ahash) = @_;
     croak if ref($ahash) !~ /HASH/;
     my $code = <<"  END";
     #line 21 "hash_sort.pm"  
     Perl::Array keys = ahash.keys();
     Perl::Array items(keys.length());
     keys.sort();     
     SV* item = NULL; 
     for(int i = 0;  i < keys.length();i++)
     {
       item = Array_GET(keys.ptr(),i);
       item = Hash_GET(ahash.ptr(),item);
       SvREFCNT_inc(item);
       Array_SET(items.ptr(),i,item);              
     }           
     return_val = Perl::new_reference_to(items);
     END
     return Inline::inline(code,['$ahash'],verbose=>1)
   }


Nice things about weave?  It has a much nicer underlying (Python vs
Perl) C api to work with (along with even simpler C++ wrappers).  It
integrates nicely with the python equivalents of PDL and wxPerl.  And,
believe it or not, it currently is more tmtowtdi ("there is more than
one way to do it").  Gasp.

Because it's more accessibly architected than Inline currently is.
Eg, one can write the equivalent of
  my $module = Inline::C::Module->new(NAME => "Foo");
  my $mfunc  = Inline::C::Function->new(
    NAME => 'increment',
    CODE => "return_val = Perl::new_reference_to(Perl::Int(a+1));",
    PASS => ['$a']);
  $module->add_function($mfunc);
  $module->compile();

But weave is rather less SWIG-ish (parse existing code and make glue).
    
Inline and Weave have many of the same needs, but were developed
independently, so it's quite interesting to see the different design
decisions.  APIs, caching details, etc.

You can get weave from http://www.scipy.org/site_content/weave .
"python setup.py build" and "python setup.py install" is the
equivalent of "perl Makefile.PL" etc.

Then you can do
   use Inline Python => <<"END";
   import weave    
   def foo(a):
     return weave.inline('return_val = a + 1;',['a'])
   END
   print &foo(41),"\n";
:)

Mitchell

Reply via email to