develooper Front page | perl.perl5.porters | Postings from October 2003

[perlembed] Maintaining multiple interpreter instances

From:
Stas Bekman
Date:
October 23, 2003 14:30
Subject:
[perlembed] Maintaining multiple interpreter instances
Message ID:
3F984738.6080909@stason.org
The second example in perlembed.pod

=head2 Maintaining multiple interpreter instances

has two problems. With 5.8.0 and higher it doesn't produce what's promised (it 
does with 5.6.x). The doc says it should print:

  % multiplicity
  Hi, I'm one_perl
  Hi, I'm two_perl

but it does:

  % multiplicity
  Hi, I'm /tmp/multiplicity
  Hi, I'm /tmp/multiplicity

for the code:

SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)"

i.e. $^X is not getting set to the fake first arg in perl_parse, but to the 
real full path of the executable. Is it a bug in perl or the doc?

Second, why the example calls PERL_SET_CONTEXT so many times? I could be wrong 
but you really need it for perl_parse to get this example to work. If it's 
needed for other perl_ calls, then this example is not representative and a 
better example should be given instead or in addition. I'm hunting an abscure 
bug in mod_perl 2.0 connected to the switching of context with multiple perl 
instances in the same process and I'd love to have a clear understanding when 
PERL_SET_CONTEXT is needed really.

The following changed version prints what the doc promises under any perl and 
also uses the minimal amount of PERL_SET_CONTEXT calls.

#include <EXTERN.h>
#include <perl.h>

/* we're going to embed two interpreters */
/* we're going to embed two interpreters */

#define SAY_HELLO "-e", "$who = shift; print qq(Hi, I'm $who\n)"

int main(int argc, char **argv, char **env)
{
     PerlInterpreter *one_perl, *two_perl;
     char *one_args[] = { argv[0], SAY_HELLO, "one_perl"};
     char *two_args[] = { argv[0], SAY_HELLO, "two_perl" };

     PERL_SYS_INIT3(&argc,&argv,&env);
     one_perl = perl_alloc();
     two_perl = perl_alloc();

     perl_construct(one_perl);
     perl_construct(two_perl);

     PERL_SET_CONTEXT(one_perl);
     perl_parse(one_perl, NULL, 4, one_args, (char **)NULL);
     PERL_SET_CONTEXT(two_perl);
     perl_parse(two_perl, NULL, 4, two_args, (char **)NULL);

     perl_run(one_perl);
     perl_run(two_perl);

     perl_destruct(one_perl);
     perl_destruct(two_perl);

     perl_free(one_perl);
     perl_free(two_perl);
     PERL_SYS_TERM();
}

The original example was:

  #include <EXTERN.h>
  #include <perl.h>

  /* we're going to embed two interpreters */
  /* we're going to embed two interpreters */

  #define SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)"

  int main(int argc, char **argv, char **env)
  {
      PerlInterpreter *one_perl, *two_perl;
      char *one_args[] = { "one_perl", SAY_HELLO };
      char *two_args[] = { "two_perl", SAY_HELLO };

      PERL_SYS_INIT3(&argc,&argv,&env);
      one_perl = perl_alloc();
      two_perl = perl_alloc();

      PERL_SET_CONTEXT(one_perl);
      perl_construct(one_perl);
      PERL_SET_CONTEXT(two_perl);
      perl_construct(two_perl);

      PERL_SET_CONTEXT(one_perl);
      perl_parse(one_perl, NULL, 3, one_args, (char **)NULL);
      PERL_SET_CONTEXT(two_perl);
      perl_parse(two_perl, NULL, 3, two_args, (char **)NULL);

      PERL_SET_CONTEXT(one_perl);
      perl_run(one_perl);
      PERL_SET_CONTEXT(two_perl);
      perl_run(two_perl);

      PERL_SET_CONTEXT(one_perl);
      perl_destruct(one_perl);
      PERL_SET_CONTEXT(two_perl);
      perl_destruct(two_perl);

      PERL_SET_CONTEXT(one_perl);
      perl_free(one_perl);
      PERL_SET_CONTEXT(two_perl);
      perl_free(two_perl);
      PERL_SYS_TERM();
  }

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About