home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / perlwin32faq10.html < prev    next >
Encoding:
Extensible Markup Language  |  1999-10-15  |  19.2 KB  |  547 lines

  1. <?xml version="1.0"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3.     "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
  4. <html xmlns="http://www.w3.org/TR/xhtml1">
  5.   <head>
  6.     <title>perlwin32faq10 - Embedding and Extending</title>
  7.     <link rev="made" href="mailto:bjepson@debian.ids.net" />
  8.     <meta name="GENERATOR" charset="iso-8859-1" />
  9.     <link rel="STYLESHEET" href="../win32prk.css" type="text/css"
  10.     media="screen" />
  11.   </head>
  12.  
  13.   <body bgcolor="#ffffff">
  14.     <!-- beginning of leaf header-->
  15.  
  16.     <table width="100%">
  17.       <tr>
  18.         <td bgcolor="000000" width="70" height="31"><a href= 
  19.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  20.         "ActiveState Home Page" border="0" width="68" height=
  21.         "30" /></a></td>
  22.  
  23.         <td width="10" bgcolor="#ffffff"> </td>
  24.  
  25.         <td valign="middle" bgcolor="#cc0066"><font face=
  26.         "sans-serif" size="+1" color="#ff99cc">   
  27.         Win32 FAQ</font></td>
  28.       </tr>
  29.     </table>
  30.     <!-- end of leaf content--><!-- INDEX BEGIN -->
  31.  
  32.     <ul>
  33.       <li><a href="#NAME">NAME</a></li>
  34.  
  35.       <li>
  36.         <a href="#DESCRIPTION">DESCRIPTION</a> 
  37.  
  38.         <ul>
  39.           <li><a href="#How_do_I_write_an_extension_for_">How do I
  40.           write an extension for Perl for Win32?</a></li>
  41.  
  42.           <li><a href="#How_do_I_embed_the_Perl_interpre">How do I
  43.           embed the Perl interpreter into my C/C++
  44.           program?</a></li>
  45.  
  46.           <li><a href="#I_have_a_program_with_perl_embed">I have a
  47.           program with perl embedded from the standard
  48.           distribution.</a></li>
  49.         </ul>
  50.       </li>
  51.  
  52.       <li><a href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND
  53.       COPYRIGHT</a></li>
  54.     </ul>
  55.     <!-- INDEX END -->
  56.     <hr />
  57.     <br />
  58.  
  59.     <h1><a name="NAME">NAME</a></h1>
  60.  
  61.     <p>perlwin32faq10 - Embedding and Extending</p>
  62.     <br />
  63.     <hr />
  64.  
  65.     <h1><a name="DESCRIPTION">DESCRIPTION</a></h1>
  66.  
  67.     <p>Techniques for Embedding and Extending Perl for Win32</p>
  68.     <br />
  69.     <hr />
  70.  
  71.     <h2><a name="How_do_I_write_an_extension_for_">How do I write
  72.     an extension for Perl for Win32?</a></h2>
  73.  
  74.     <p>Writing extensions in <font size="-1">C</font> or <font
  75.     size="-1">C++</font> for Perl for Win32 is identical to writing
  76.     them for standard Perl. Consult the perlxstut document for
  77.     complete information and pointers to other sources of
  78.     information.</p>
  79.  
  80.     <p>As a result of the oneperl effort, you now longer need to
  81.     build Perl for Win32 from the source distribution to write or
  82.     compile extensions. The Perl installation will detect your
  83.     <font size="-1">C</font> compiler (currently, just Visual <font
  84.     size="-1">C++),</font> and configure Perl to use it for
  85.     compiling extensions.</p>
  86.  
  87.     <p>Note that writing extensions in <font size="-1">C</font> or
  88.     <font size="-1">C++</font> is not easy. You need to have
  89.     experience writing <font size="-1">C</font> programs for
  90.     Windows platforms before you should even think about writing an
  91.     extension.</p>
  92.     <br />
  93.     <hr />
  94.  
  95.     <h2><a name="How_do_I_embed_the_Perl_interpre">How do I embed
  96.     the Perl interpreter into my C/C++ program?</a></h2>
  97.  
  98.     <p>As of build 500, this is quite easy. Perl for Win32 exposes
  99.     Perl's internals through a <font size="-1">C++</font> object
  100.     called <font size="-1">PERL_OBJECT.</font> Among other things,
  101.     <font size="-1">PERL_OBJECT</font> simplifies the creation of
  102.     multiple Perl interpreters within a single process.</p>
  103.  
  104.     <p>To embed Perl for Win32 in your own application, you must
  105.     have the following:</p>
  106.  
  107.     <ul>
  108.       <li>
  109.         <p>Microsoft Visual <font size="-1">C++</font></p>
  110.       </li>
  111.  
  112.       <li>
  113.         <p> <font size="-1">A</font> binary release of Perl for
  114.         Win32 (build 500 or later)</p>
  115.       </li>
  116.  
  117.       <li>
  118.         <p>The Perl for Win32 source code (the version of the
  119.         source code must match the build of your binary version of
  120.         Perl)</p>
  121.       </li>
  122.     </ul>
  123.  
  124.     <p>Before you can compile an application that embeds Perl for
  125.     Win32, you must compile Perl from source. This does not mean
  126.     that you must install Perl from source - the reason that you
  127.     must compile Perl from source is that the compilation process
  128.     creates the <em>DynaLoader.c</em> source code file, which is
  129.     required if you want to use any extensions in your
  130.     application.</p>
  131.  
  132.     <p>To compile Perl from source:</p>
  133.  
  134.     <ol>
  135.       <li>
  136.         <strong><a name="item_"></a></strong> 
  137.  
  138.         <p>Change directory to the <em>win32</em> directory of your
  139.         Perl source distribution and edit <em>Makefile</em> as
  140.         necessary.</p>
  141.       </li>
  142.  
  143.       <li>
  144.          
  145.  
  146.         <p>Type <code>nmake</code>. This will compile all of
  147.         Perl.</p>
  148.       </li>
  149.  
  150.       <li>
  151.          
  152.  
  153.         <p>Type <code>nmake test</code>. This ensures that Perl
  154.         built correctly and passes all its tests.</p>
  155.       </li>
  156.  
  157.       <li>
  158.          
  159.  
  160.         <p>Perl is now compiled, and the <em>DynaLoader.c</em> file
  161.         is created. If you did not install a binary release of
  162.         Perl, you can issue the command <code>make install</code>,
  163.         which will install Perl for you. This step is not necessary
  164.         if you installed a binary release of Perl.</p>
  165.       </li>
  166.     </ol>
  167.  
  168.     <p>Once you have compiled Perl from source, you have a source
  169.     tree that can be used to build an embedded version of Perl.
  170.     Here is a simple program that includes an embedded Perl
  171.     interpreter. It is actually quite a bit like <em>perl.exe</em>.
  172.     You can use all the command-line switches that are supported by
  173.     perl, and you can also include the name of a script to execute.
  174.     If you supply no switches, it will read in a Perl program from
  175.     standard input, and executes the program when end-of-file is
  176.     encountered. Save this file as <em>MyPerl.cpp</em> on your
  177.     computer (remember to outdent the source code):</p>
  178.     <br />
  179. <pre>
  180.     /*
  181.         
  182.         1) include perlhost.h. This header defines all the
  183.            OS Interfaces that are used by the PERL_OBJECT.
  184.         2) define the host data structure 'CPerlHost'
  185.         3) call the CPerlHost member function PerlCreate,
  186.            which calls perl_alloc and perl_construct
  187.         4) call the CPerlHost member function PerlParse
  188.         5) call the CPerlHost member function PerlRun
  189.         6) when done, call the CPerlHost member function PerlDestroy,
  190.            which calls perl_destruct and perl_free
  191.         
  192.         Including XSUB.h brings in the files that have macros to redefine
  193.         the perl_* functions, to work with PERL_OBJECT. Note that these 
  194.         macros may cause problems when there are name collisions. A common 
  195.         problem is having a local variable that has the same name as a Perl 
  196.         function. To track down these name collisions compile using '/P' 
  197.         option with VC++ so that you can see what the preprocessor has done.
  198.         
  199.     */ 
  200.     #include <EXTERN.h>
  201.     #include <perl.h>
  202.     #define NO_XSLOCKS
  203.     
  204.     // Bring in the macros that redefine perl_* functions to work with
  205.     // PERL_OBJECT.
  206.     //
  207.     #include <XSUB.h>
  208.     
  209.     /* for Win32 */
  210.     #include "win32iop.h"
  211.     #include <fcntl.h>
  212.     #include <perlhost.h>
  213.     #include <stdio.h>
  214.     
  215.     // DynaLoader stuff.
  216.     //
  217.     char *staticlinkmodules[] = {
  218.         "DynaLoader",
  219.         NULL,
  220.     };
  221.     
  222.     // More DynaLoader stuff.
  223.     //
  224.     EXTERN_C void boot_DynaLoader _((CV* cv _CPERLarg));
  225.     
  226.     static void
  227.     xs_init(CPERLarg)
  228.     {
  229.         char *file = __FILE__;
  230.         dXSUB_SYS;
  231.         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  232.     }
  233.     
  234.     CPerlObj *pPerl;  // The Perl object used by the host.
  235.     
  236.     int main (int argc, char **argv, char **env)
  237.     {
  238.         CPerlHost host;     // The Perl host
  239.         host.PerlCreate();  // Create an instance of the Perl Interpreter
  240.     
  241.         printf("Welcome to my very own Perl!\n\n");
  242.     
  243.         // Read arguments from the command line.  If no script is named on the
  244.         // command-line, and nothing is specified for -e, the interpreter will
  245.         // read commands from standard input (just like Perl!).
  246.         //
  247.         host.PerlParse(xs_init, argc, argv, NULL);
  248.     
  249.         host.PerlRun();      // Run the interpreter.
  250.         host.PerlDestroy();  // Destroy the interpreter.
  251.         return 0;
  252.     }
  253. </pre>
  254.  
  255.     <p>To compile this program, you must use the following <em>
  256.     Makefile</em>. Be sure to update <code>LIBDIR</code> to point
  257.     to the <em>CORE</em> subdirectory of your Perl binary
  258.     installation, and update <code>SRCDIR</code> to point to the
  259.     <em>src</em> directory of the Perl source code that you
  260.     compiled earlier. Save this file as <em>Makefile</em> (remember
  261.     to outdent the contents):</p>
  262.     <br />
  263. <pre>
  264.     LIBDIR = C:\Perl\5.00500\lib\MSWin32-x86\CORE
  265.     SRCDIR = C:\5.00500\src
  266.     
  267.     LIBS = oldnames.lib kernel32.lib user32.lib winspool.lib advapi32.lib \
  268.            shell32.lib ole32.lib  oleaut32.lib netapi32.lib uuid.lib \
  269.            wsock32.lib version.lib PerlCRT.lib $(LIBDIR)\PerlCore.lib
  270. </pre>
  271.     <br />
  272. <pre>
  273.     CCFLAGS = -c -I$(LIBDIR) -Gf -W3 -DWIN32 -D_CONSOLE -DPERL_CORE \
  274.               -O2 -MD -DNDEBUG -TP -GX -DPERL_OBJECT -UPERLDLL
  275.     CCDYNA = $(CCFLAGS) -I$(SRCDIR)\ext\DynaLoader
  276.     
  277.     all:
  278.         cl.exe $(CCFLAGS) -Fo.\MyPerl.obj MyPerl.cpp
  279.         cl.exe $(CCFLAGS) -Fo.\win32.obj $(SRCDIR)\win32\win32.c
  280.         cl.exe $(CCFLAGS) -Fo.\win32sck.obj $(SRCDIR)\win32\win32sck.c
  281.         cl.exe $(CCFLAGS) -Fo.\DynaLoader.obj \
  282.                           $(SRCDIR)\ext\DynaLoader\DynaLoader.c
  283.         link.exe -nologo -nodefaultlib -release -machine:x86 $(LIBS) \
  284.             -subsystem:console MyPerl.obj win32.obj win32sck.obj \
  285.             DynaLoader.obj setargv.obj
  286. </pre>
  287.  
  288.     <p>Make sure that <em>PerlCRT.lib</em> has been installed
  289.     properly. For more information, see <a href= 
  290.     "#How_do_I_write_an_extension_for_">How do I write an extension
  291.     for Perl for Win32?</a>.</p>
  292.  
  293.     <p>You can then compile <em>MyPerl.cpp</em> by typing <code>
  294.     nmake</code> at the command line (make sure that both <em>
  295.     MyPerl.cpp</em> and <em>Makefile</em> are in your current
  296.     working directory). This will produce the file <em>
  297.     MyPerl.exe</em>, which you can run at the command line as
  298.     though it were <em>perl.exe</em>:</p>
  299.     <br />
  300. <pre>
  301.     C:\MyPerl>myperl -e "print qq[Hello, World\n];"
  302.     Welcome to my very own Perl!
  303.     
  304.     Hello, World
  305. </pre>
  306.  
  307.     <p>or:</p>
  308.     <br />
  309. <pre>
  310.     C:\MyPerl>myperl
  311.     Welcome to my very own Perl!
  312.     
  313.     print "Hello, world\n";
  314.     ^Z
  315.     Hello, world
  316. </pre>
  317.  
  318.     <p>If you want to use modules and extensions from your local
  319.     Perl installation, you'll need to do one of two things. The
  320.     simplest thing you can do is copy your executable to the same
  321.     location as perl.exe.</p>
  322.  
  323.     <p>If this is not an option, you need to tell your program
  324.     where to find things. This can be accomplished by setting the
  325.     <code>PERL5LIB</code> environment variable with something
  326.     like:</p>
  327.     <br />
  328. <pre>
  329.     set PERL5LIB=C:\Perl\5.00500\lib;C:\Perl\site\5.00500\lib;C:\Perl\site\lib 
  330. </pre>
  331.  
  332.     <p>Then you can use modules and extensions from your local Perl
  333.     installation:</p>
  334.     <br />
  335. <pre>
  336.     C:\MyPerl>MyPerl -MWin32::OLE -e "print $Win32::OLE::VERSION;"
  337.     Welcome to my very own Perl!
  338.     
  339.     0.0807
  340. </pre>
  341.  
  342.     <p>The following example is based on the PerlPower example from
  343.     Doug MacEachern and Jon Orwant's perlembed document (this is
  344.     part of Perl, and you should have a copy included with Perl for
  345.     Win32). You should save the following program as <em>
  346.     PerlPower.cpp</em>:</p>
  347.     <br />
  348. <pre>
  349.     /*
  350.         To show how to embed Perl with PERL_OBJECT defined, we will show the
  351.         changes needed for the PerlPower sample that is in perlembed.pod.
  352.         
  353.         1) include perlhost.h. This header defines all the
  354.            OS Interfaces that are used by the PERL_OBJECT.
  355.         2) define the host data structure 'CPerlHost'
  356.         3) call the CPerlHost member function PerlCreate,
  357.            which calls perl_alloc and perl_construct
  358.         4) call the CPerlHost member function PerlParse
  359.         5) call the CPerlHost member function PerlRun
  360.         6) call any of the perl_call_* functions
  361.         7) when done, call the CPerlHost member function PerlDestroy,
  362.            which calls perl_destruct and perl_free
  363.         
  364.         Including XSUB.h brings in the files that have macros to redefine the
  365.         perl_* functions, to work with PERL_OBJECT. Note that these macros may
  366.         cause problems when there are name collisions. A common problem is
  367.         having a local variable that has the same name as a Perl function. To
  368.         track down these name collisions compile using '/P' option with VC++ so
  369.         that you can see what the preprocessor has done.
  370.         
  371.     */
  372.     
  373.     #include <EXTERN.h>
  374.     #include <perl.h>
  375.     
  376.     #define NO_XSLOCKS
  377.     
  378.     // Bring in the macros that redefine perl_* functions to work with
  379.     // PERL_OBJECT.
  380.     //
  381.     #include <XSUB.h>
  382.     
  383.     /* for Win32 */
  384.     #include "win32iop.h"
  385.     #include <fcntl.h>
  386.     #include <perlhost.h>
  387.     
  388.     // DynaLoader stuff.
  389.     //
  390.     char *staticlinkmodules[] = {
  391.         "DynaLoader",
  392.         NULL,
  393.     };
  394.     
  395.     // More DynaLoader stuff.
  396.     //
  397.     EXTERN_C void boot_DynaLoader _((CV* cv _CPERLarg));
  398.     static void
  399.     xs_init(CPERLarg)
  400.     {
  401.         char *file = __FILE__;
  402.         dXSUB_SYS;
  403.         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  404.     }
  405.     
  406.     CPerlObj *pPerl;
  407.     
  408.     static void
  409.     PerlPower(int a, int b)
  410.     {
  411.         dSP;                            /* initialize stack pointer      */
  412.         ENTER;                          /* everything created after here */
  413.         SAVETMPS;                       /* ...is a temporary variable.   */
  414.         PUSHMARK(SP);                   /* remember the stack pointer    */
  415.         XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack  */
  416.         XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack  */
  417.         PUTBACK;                        /* make local stack pointer global */
  418.         perl_call_pv("expo", G_SCALAR); /* call the function             */
  419.         SPAGAIN;                        /* refresh stack pointer         */
  420.                                         /* pop the return value from stack */
  421.     
  422.         printf ("%d to the %dth power is %d.\n", a, b, POPi);
  423.         PUTBACK;
  424.         FREETMPS;                       /* free that return value        */
  425.         LEAVE;                          /* ...and the XPUSHed "mortal" args.*/
  426.     }
  427.     
  428.     int main (int argc, char **argv, char **env)
  429.     {
  430.         CPerlHost host;
  431.     
  432.         host.PerlCreate();
  433.     
  434.         // Don't use argv or argc for embedding.  Instead, we'll pretend that
  435.         // we gave it the arguments "-e 0".
  436.         //
  437.         char *embedding[] = { "", "-e", "0" };
  438.         host.PerlParse(xs_init, 3, embedding, NULL);
  439.     
  440.         // Define the expo() function.
  441.         //
  442.         perl_eval_pv("sub expo { my($a, $b) = @_; return $a ** $b; }", TRUE);
  443.     
  444.         host.PerlRun();
  445.     
  446.         PerlPower(3, 4);                      /*** Compute 3 ** 4 ***/
  447.     
  448.         host.PerlDestroy();
  449.         return 0;
  450.     }
  451. </pre>
  452.  
  453.     <p>The following <em>Makefile</em> can be used to compile <em>
  454.     PerlPower.cpp</em>:</p>
  455.     <br />
  456. <pre>
  457.     LIBDIR = C:\Perl\5.00468\lib\MSWin32-x86\CORE
  458.     SRCDIR = C:\betab9\src
  459.     
  460.     LIBS = oldnames.lib kernel32.lib user32.lib winspool.lib advapi32.lib \
  461.            shell32.lib ole32.lib  oleaut32.lib netapi32.lib uuid.lib \
  462.            wsock32.lib version.lib PerlCRT.lib $(LIBDIR)\PerlCore.lib
  463.     
  464.     CCFLAGS = -c -I$(LIBDIR) -Gf -W3 -DWIN32 -D_CONSOLE -DPERL_CORE \
  465.               -O2 -MD -DNDEBUG -TP -GX -DPERL_OBJECT -UPERLDLL
  466.     CCDYNA = $(CCFLAGS) -I$(SRCDIR)\ext\DynaLoader
  467.     
  468.     all:
  469.         cl.exe $(CCFLAGS) -Fo.\PerlPower.obj PerlPower.cpp
  470.         cl.exe $(CCFLAGS) -Fo.\win32.obj $(SRCDIR)\win32\win32.c
  471.         cl.exe $(CCFLAGS) -Fo.\win32sck.obj $(SRCDIR)\win32\win32sck.c
  472.         cl.exe $(CCFLAGS) -Fo.\DynaLoader.obj \
  473.                           $(SRCDIR)\ext\DynaLoader\DynaLoader.c
  474.         link.exe -nologo -nodefaultlib -release -machine:x86 $(LIBS) \
  475.             -subsystem:console PerlPower.obj win32.obj win32sck.obj \
  476.             DynaLoader.obj setargv.obj
  477. </pre>
  478.  
  479.     <p>As with the <em>MyPerl.cpp</em> example <em>Makefile</em>,
  480.     be sure that <code>SRCDIR</code> and <code>LIBDIR</code> point
  481.     to the correct directories. You can compile <em>
  482.     PerlPower.cpp</em> by typing <code>nmake</code> at the command
  483.     line (make sure that both <em>PerlPower.cpp</em> and <em>
  484.     Makefile</em> are in your current working directory). This will
  485.     produce the file <em>PerlPower.exe</em>, which you can run at
  486.     the command line:</p>
  487.     <br />
  488. <pre>
  489.     C:\PerlPower>PerlPower
  490.     3 to the 4th power is 81.
  491. </pre>
  492.  
  493.     <p>You should definitely read the perlembed document in its
  494.     entirety to get an overview of the issues involved with
  495.     embedding Perl. Then, you can use the <em>MyPerl.cpp</em> and
  496.     <em>PerlPower.cpp</em> as guides for embedding Perl for
  497.     Win32.</p>
  498.     <br />
  499.     <hr />
  500.  
  501.     <h2><a name="I_have_a_program_with_perl_embed">I have a program
  502.     with perl embedded from the standard distribution. Why won't it
  503.     compile with Perl for Win32?</a></h2>
  504.  
  505.     <p>Perl for Win32 exposes the Perl interpreter in a slightly
  506.     different fashion than standard Perl. For information on how to
  507.     embed using Perl for Win32, see <a href= 
  508.     "#How_do_I_embed_the_Perl_interpre">How do I embed the Perl
  509.     interpreter into my C/C++ program?</a>.</p>
  510.     <br />
  511.     <hr />
  512.  
  513.     <h1><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND
  514.     COPYRIGHT</a></h1>
  515.  
  516.     <p>This <font size="-1">FAQ</font> was originally assembled and
  517.     maintained by Evangelo Prodromou. <a href= 
  518.     "mailto:evangelo@endcontsw.com">evangelo@endcontsw.com.</a> It
  519.     has been revised and updated by Brian Jepson of O'Reilly and
  520.     Associates, and David Grove and David Dmytryshyn of
  521.     ActiveState.</p>
  522.  
  523.     <p>This <font size="-1">FAQ</font> is in the public domain. If
  524.     you use it, however, please ensure that you give credit to the
  525.     original authors.</p>
  526.  
  527.     <p><!-- beginning of leaf footer--></p>
  528.  
  529.     <table width="100%">
  530.       <tr>
  531.         <td bgcolor="000000" width="70" height="31"><a href= 
  532.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  533.         "ActiveState Home Page" border="0" width="68" height=
  534.         "30" /></a></td>
  535.  
  536.         <td width="10"> </td>
  537.  
  538.         <td valign="middle" bgcolor="#cc0066"><font face=
  539.         "sans-serif" size="+1" color="#ff99cc">   
  540.         Win32 FAQ</font></td>
  541.       </tr>
  542.     </table>
  543.     <!-- end  of leaf footer-->
  544.   </body>
  545. </html>
  546.  
  547.