Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Inline::CPP does pretty much what you're asking. It doesn't completely absolve you from studying perlguts, but it's a big step in that direction.

use Inline CPP => 'DATA'; my $sum = add( 2, 4 ); print "$sum\n"; __DATA__ __CPP__ #include <iostream> int add( int x, int y ) { return x + y; }

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).

The Inline::CPP documentation (always a work in progress) is improving, and should get you going in the right direction. I also recommend reading Inline, Inline::C, and Inline::C-Cookbook. Lots of reading, I know. But each of those resources gets you closer to being productive in this funky art. The Inline::C-Cookbook, in particular, demonstrates how to manipulate the Perl stack within your C or C++ functions.

Also, the tests found in the Inline::CPP distribution under t/ and grammar/t demonstrate many C++ constructs at work. And Math::Prime::FastSieve is a sort of "proof of concept" and example of using Inline::CPP.

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.

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.


Dave


In reply to Re: Calling C++ functions from Perl without using xsubpp by davido
in thread Calling C++ functions from Perl without using xsubpp by dafrito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (6)
    As of 2014-07-26 12:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (176 votes), past polls