<?xml version="1.0" encoding="windows-1252"?>
<node id="994372" title="Re: Calling C++ functions from Perl without using xsubpp" created="2012-09-18 17:40:20" updated="2012-09-18 17:40:20">
<type id="11">
note</type>
<author id="281137">
davido</author>
<data>
<field name="doctext">
&lt;p&gt;[mod://Inline::CPP] does pretty much what you're asking.  It doesn't completely absolve you from studying [doc://perlguts], but it's a big step in that direction.&lt;/p&gt;
&lt;c&gt;
use Inline CPP =&gt; 'DATA';

my $sum = add( 2, 4 );
print "$sum\n";

__DATA__
__CPP__

#include &lt;iostream&gt;

int add( int x, int y ) {
    return x + y;
}
&lt;/c&gt;
&lt;p&gt;There are C++ constructs that don't map well to Perl.  For one thing, your C++ compiler has no idea how Perl intends to call a function, so template-based functions and classes need to be instantiated in a concrete C++ function or class wrapper before being exposed to Perl.  But once you get familiar with the large subset of C++ that does map well to Perl, Inline::CPP is pretty fun to use.  I've even spent some time verifying its compatibility with the new C++11 standard (much of it "just works" as it should, with a few exceptions in predictable places).
&lt;/p&gt;
&lt;p&gt;The Inline::CPP documentation (always a work in progress) is improving, and should get you going in the right direction.  I also recommend reading [mod://Inline], [mod://Inline::C], and [mod://Inline::C-Cookbook].  Lots of reading, I know.  But each of those resources gets you closer to being productive in this funky art.  The &lt;b&gt;Inline::C-Cookbook&lt;/b&gt;, in particular, demonstrates how to manipulate the Perl stack within your C or C++ functions.&lt;/p&gt;
&lt;p&gt;Also, the tests found in the Inline::CPP distribution under t/ and grammar/t demonstrate many C++ constructs at work.  And [mod://Math::Prime::FastSieve] is a sort of "proof of concept" and example of using Inline::CPP.&lt;/p&gt;
&lt;p&gt;Advanced Perl Programming (2nd Edition) has a chapter that discusses the Inline modules (Inline::C in particular, but much is applicable to Inline::CPP).  And Sam Treagar's book, "Writing Perl Modules for CPAN" has a chapter on Inline::C too.&lt;/p&gt;
&lt;p&gt;If you find yourself working much with it, feel free to join the inline@perl.org mailing list.  See [http://lists.perl.org] for details.&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-281137"&gt;
&lt;br /&gt;&lt;p&gt;Dave&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
994363</field>
<field name="parent_node">
994363</field>
</data>
</node>
