Hi All,

I'm not sure if this is a bit off topic. But I can't seem to get any help 
from any of the Perl NGs. Since Inline folks are probably the most well 
versed with Perl guts, I though - "there MUST be someone who can help me out 
here ..".

Anyway here goes.

Basically I've written a C app and I want to be able to run perl scripts 
from it - so that I could use inline to run C programs from within my app 
(using my own C API). Unfortunately (read frustratingly !), I've come 
unstuck at the first step - embedding Perl in my application. I have 
followed the info in the perlembed doc. Basically, so long as I pass my app 
a "valid" i.e. syntactically correct Perl string, everything works fine. If 
the perl expression has a syntax error, or I mis-spelled a module name, it 
goes belly up in a very unglamourous way - It crashes my entire application 
!

I have included the following text and sample test program, to help 
demonstrate. C'mon please somebody - point out what I'm doing wrong. It will 
be very much appreciated.



I'm building on Win2k and my Perl is version 5.005_03 built for MSWin32-x86 
(NOT ActiveState).

These are the problems I'm facing :

1. Formatting of input string appears to be significant (WHY ?!!!)
If I use the commented out sprintf statement instead, the application
runs OK (prints text to console) and then it bombs out on it's way out
- when main returns to the OS. I get an "Unhandled exception - Memory acess
violation" message.

In the Debug View in VC++ Iget the following message:

First-chance exception in PERL_PARSER.exe: 0xC0000005: Access Violation.



** NOTES To Issue #2 **

  The crashes described in (2) below, occur when perl_eval_pv() is called
  in main() - this leads me to believe that I should be doing some checking
  or something (but WHAT ?), before I invoke perl_eval_pv()

2. No graceful error handling:
a) syntax errors in the string to be evaluated (sTest), crash the App
  To test this, simply modify the sprintf statement to print a string
  with incorrect Perl syntax to sTest.

  When the same test is carried out (passing a string with incorrect
  perl syntax to the perl interpreter i.e. "Perl"), this is the output I
get:

     C:\>perl -e "just do it"
  syntax error at -e line 1, near "just do it"
  Execution of -e aborted due to compilation errors.

  I want to be able to able to gracefully recover from the above
  error, and return the error description string above, back to main().



b) mis-spelled or missing perl modules crash the App:
    To test this, simply replace one of the modules being used
    (eg test), with the module FakeModule - the App crashes.
    However, when the same syntax is passed to the perl interpreter
    (i.e. "Perl") at the command prompt, you get something like this:


  C:\>perl -e "use Strict; use FakeModule; print 'Hello Big Boy !'"
   Can't locate FakeModule.pm in @INC (@INC contains:
   c:\site_perl c:\perl\lib/MSWin32-x86 c:\perl\lib
   c:\perl\site\5.00503\lib/MSWin32-x86 c:\perl\site\5.00503\lib .)
   at -e line 1.

   BEGIN failed--compilation aborted at -e line 1.

  I want to be able to able to gracefully recover from the above
  error, and return the error description string above, back to main().



Heres the code for the test project (as well as building instructions) - Any
help will be much appreciated :


// PERL_PARSER.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <EXTERN.h>               /* from the Perl distribution     */
#include <perl.h>                 /* from the Perl distribution     */
static PerlInterpreter *my_perl;  /***    The Perl interpreter    ***/




/**************    AUTO-GENERATED CODE    ***************/
/* This section of the code I autogenerated by typing : */
/* perl -MExtUtils::Embed -e xsinit at the console  */
/********************************************************/

#if defined(__cplusplus) && !defined(PERL_OBJECT)
#define is_cplusplus
#endif

#ifdef is_cplusplus
extern "C" {
#endif

#include <EXTERN.h>
#include <perl.h>
#ifdef PERL_OBJECT
#define NO_XSLOCKS
#include <XSUB.h>
#include "win32iop.h"
#include <fcntl.h>
#include <perlhost.h>
#endif
#ifdef is_cplusplus
}
#  ifndef EXTERN_C
#    define EXTERN_C extern "C"
#  endif
#else
#  ifndef EXTERN_C
#    define EXTERN_C extern
#  endif
#endif

EXTERN_C void xs_init _((void));

EXTERN_C void boot_DynaLoader _((CV* cv));

EXTERN_C void
xs_init(void)
{
char *file = __FILE__;
dXSUB_SYS;

/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}





/********************************************************/
/* Hand crafted code begins here ..      */
/********************************************************/



int my_perl_parser( char *ptext, char *run )
{
   char *embedding[] = { "", run, "0" };
   int retval = 0;

   PerlInterpreter *my_perl;
   my_perl = perl_alloc();
   perl_construct( my_perl );
   perl_parse(my_perl, xs_init, 3, embedding, NULL);
   perl_run(my_perl);
   perl_eval_pv( ptext, TRUE );
   perl_destruct(my_perl);
   perl_free(my_perl);
   return retval;
}



/***************************************************************************
*/
/*                   */
/*   BUILDING AND TESTING THE TEST APPLICATION IN VISUAL C++   */
/*                   */
/***************************************************************************
*

A. Create a windows console App in Visual Studio 6 (may work in v5)

B. Insert this file into the project created in the previous step

C. You may replace "stdafx.h" "stdio.h" if you want (that's all the
stdafx.h header contains at the moment)

D. Add the path to core perl headers (c:\perl\lib\MSWin32-x86\CORE in my
case)
   to your include directories in your project settings (Alt-F7)

E. Add the additional library path (c:\perl\lib\MSWin32-x86\CORE in my case)

F. Add the following library modules:
   oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
   comdlg32.lib advapi32.lib shell32.lib ole32.lib perl.lib

****************************************************************************
**/





/******************* PROBLEMS WITH THE CODE BELOW **********************


1. Formatting of input string appears to be significant (WHY ?!!!)
If I use the commented out sprintf statement instead, the application
runs OK (prints text to console) and then it bombs out on it's way out
- when main returns to the OS. I get an "Unhandled exception - Memory acess
violation" message.

In the Debug View in VC++ Iget the following message:

First-chance exception in PERL_PARSER.exe: 0xC0000005: Access Violation.



** NOTES To Issue #2 **

  The crashes described in (2) below, occur when perl_eval_pv() is called
  in main() - this leads me to believe that I should be doing some checking
  or something (but WHAT ?), before I invoke perl_eval_pv()

2. No graceful error handling:
a) syntax errors in the string to be evaluated (sTest), crash the App
  To test this, simply modify the sprintf statement to print a string
  with incorrect Perl syntax to sTest.

  When the same test is carried out (passing a string with incorrect
  perl syntax to the perl interpreter i.e. "Perl"), this is the output I
get:

     C:\>perl -e "just do it"
  syntax error at -e line 1, near "just do it"
  Execution of -e aborted due to compilation errors.

  I want to be able to able to gracefully recover from the above
  error, and return the error description string above, back to main().



b) mis-spelled or missing perl modules crash the App:
    To test this, simply replace one of the modules being used
    (eg test), with the module FakeModule - the App crashes.
    However, when the same syntax is passed to the perl interpreter
    (i.e. "Perl") at the command prompt, you get something like this:


  C:\>perl -e "use Strict; use FakeModule; print 'Hello Big Boy !'"
   Can't locate FakeModule.pm in @INC (@INC contains:
   c:\site_perl c:\perl\lib/MSWin32-x86 c:\perl\lib
   c:\perl\site\5.00503\lib/MSWin32-x86 c:\perl\site\5.00503\lib .)
   at -e line 1.

   BEGIN failed--compilation aborted at -e line 1.

  I want to be able to able to gracefully recover from the above
  error, and return the error description string above, back to main().

*************************************************************************/




int main(int argc, char **argv, char **env)
{
char sTest[64] =  "" ;


/* Why does this text formatting cause the App to crash ?

sprintf(sTest,"use Strict;\n      \
             use test;\n   \
       print 'Woohoo !! -  I can use extensions!'") ;
*/


sprintf(sTest,"use Strict;\nuse test;\nprint 'Woohoo !! - I can use
extensions!'") ;


return my_perl_parser( sTest, "-e" );
}






Many TIA

The Hacker








_________________________________________________________________
Join the world�s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

Reply via email to