<?xml version="1.0" encoding="utf-8"?>
<!-- generator="Blooki/0.4808" -->
<rss version="2.0">
  <channel>
    <title>Zero Brane</title>
    <link>http://notebook.kulchenko.com/</link>
    <description>By seeking, you will discover...</description>
    <language>en</language>
    <copyright>Copyright 2009</copyright>
    <lastBuildDate>Mon, 09 Jan 2006 05:52:44 GMT</lastBuildDate>
    <pubDate>Mon, 09 Jan 2006 05:52:44 GMT</pubDate>
    <generator>http://www.blooki.org/</generator>
    <ttl>60</ttl>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs> 


  <item>
    <title>Embedding Perl in C++</title>
    <description>&lt;p&gt;In addition to all &lt;a href="modeling/modeling-getting-visual"&gt;visual features&lt;/a&gt; I want, I also &lt;a href="modeling/neural-modeling"&gt;want the framework/simulator to be easily configurable and scriptable&lt;/a&gt;, and what language provides better parsing and scripting capabilities than Perl? (or Python, or Ruby, but my experience with those is very limited...)&lt;/p&gt;

&lt;p&gt;It's (non)surprisingly easy to call perl from C as the following code shows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#ifdef __cplusplus
  extern &amp;quot;C&amp;quot; {
#   include &amp;lt;EXTERN.h&amp;gt;
#   include &amp;lt;perl.h&amp;gt;
    static PerlInterpreter *my_perl;
  }
#endif

int main(int argc, char **argv, char **env) {

  my_perl = perl_alloc();
  perl_construct(my_perl);

  perl_parse(my_perl, NULL, argc, argv, env);
  perl_run(my_perl);

  perl_destruct(my_perl);
  perl_free(my_perl);

  return 0;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code can be compiled with something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;g++ -c interp.c -o interp.o `perl -MExtUtils::Embed -e ccopts`
g++ interp.o -o interp `perl -MExtUtils::Embed -e ldopts`&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then &lt;code&gt;interp -v&lt;/code&gt; should give you the same output as &lt;code&gt;perl -v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While it may be easy to compile and run this simple program, it's still a challenge to pass complex data structures between C and Perl as you need to know all those &lt;a href="http://perldoc.perl.org/perlembed.html"&gt;*_sv, *_pv, *_av and stack manipulation calls&lt;/a&gt;. Fortunately for me I own a copy of &lt;a href="http://www.amazon.com/exec/obidos/ASIN/1930110820/ref=nosim/soaplite-20"&gt;Extending and Embedding Perl&lt;/a&gt;. There are even libraries like &lt;a href="http://www.willnolan.com/sdev/cpp/useful/PerlStream/PerlStream.html"&gt;PerlStream&lt;/a&gt; that will hide all that complexity from you.&lt;/p&gt;

&lt;p&gt;Another problem is that it means having additional libraries and module files (if you happen to use Perl modules) in your distribution. On Windows this can be solved by using &lt;a href="http://thinstall.com/help/index.html?version.htm"&gt;Thinstall&lt;/a&gt;, &lt;a href="http://www.bitsum.com/pebundle.asp"&gt;&lt;span class="caps"&gt;PEB&lt;/span&gt;undle&lt;/a&gt;, or one of &lt;a href="http://www.programmerstools.org/taxonomy/term/15/"&gt;many other exe packers&lt;/a&gt; that compress executable and dll files into a single file.&lt;/p&gt;</description>
    <guid>http://notebook.kulchenko.com/modeling/practice/embedding-perl-in-cpp</guid>
    <category>modeling/practice/</category>
    <pubDate>Mon, 09 Jan 2006 05:52:44 GMT</pubDate>
  </item>


  </channel>
</rss>

