C++ calling perl script - Not able to get the stack arguments pushed rom XPUSH in perl scr...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have the below sample program which pushes the arguments to Perl stack an=
d then calls "eval_sv". The sample perl statements get executed but i'm not=
 able to retrieve the variables passed from C++ as Perl arguments. Please l=
et me know what i am missing in the below program

Output of the program

    Hello World


    100Testing complete

This line doesn't print the value of $a and $b

string three = "print 'Test\n'; my $z = 100; print $a; print $b; print =

Here is my code:

#include <EXTERN.h>
#include <perl.h>
#include <string>
using namespace std;

string perlScript;

static PerlInterpreter *my_perl;

SV* my_eval_sv(I32 croak_on_error)
    STRLEN n_a;
    char *p1 = new char [perlScript.size()+1];
    strcpy(p1, perlScript.c_str());
    const char *p = p1;
    int len = strlen(p);

    ENTER ;

    int a, b;
    a = 10;
    b = 20;


    /* Done with pushing pointers to Perl stack */


    SV* sv1 = newSVpv(p, 0);    
    eval_sv(sv1, G_EVAL | G_KEEPERR);

    sv1 = POPs;


    if (croak_on_error && SvTRUE(ERRSV))
        croak(SvPVx(ERRSV, n_a));  

main (int argc, char **argv, char **env)
    char *embedding[] = { "", "-e", "0" };
    my_perl = perl_alloc();
    perl_parse(my_perl, NULL, 3, embedding, NULL);
    PL_exit_flags |=3D PERL_EXIT_DESTRUCT_END;

    /*string perlBeginScript;
    static const char * perlEndScript = "\


    perlScript = "sub ";
    perlScript +=3D perlBeginScript;
    perlScript +=3D "{\n"; */

    string one = "print 'Hello World\n'; ";
    string two = "my $a = shift; my $b = shift; ";
    string three=3D "print 'Test\n'; my $z = 100; print $a; print $b; pri=
nt $z;";
    string four = "print 'Testing complete\n';";

    perlScript +=3D one ;
    perlScript +=3D two;
    perlScript +=3D three;
    perlScript +=3D four;

    //perlScript +=3D perlEndScript;

    /* Done with perl script to be executed */
    PL_perl_destruct_level = 1;

Re: C++ calling perl script - Not able to get the stack arguments pushed rom XPUSH in perl script

Quoted text here. Click to load it

Just tried to make some guesses at your identical StackOverflow


The gist is that that you try to pass values by passing them on the Perl
stack, but this is only relevant if you are asking Perl the call a perl

Your code is roughly comparable to a script doing:


@_ = (10, 20);

eval 'my $a = shift; my $b = shift; my $z = 100; print $a; print $b; print $z;'


A bit simplified. @_ is really not the stack, but calling a subroutine
moves stuff from the stack into @_.


Site Timeline