C++ calling perl script - Not able to get the stack arguments pushedrom XPUSH in perl script

Discussion in 'Perl Misc' started by Yogi, Sep 13, 2012.

  1. Yogi

    Yogi Guest

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

    Output of the program

    Hello World

    Test

    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 $z;";

    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);

    dSP;
    ENTER ;
    SAVETMPS ;
    PUSHMARK(SP) ;


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

    PERL_SET_CONTEXT(my_perl);
    XPUSHs(sv_2mortal(newSViv(a)));
    PERL_SET_CONTEXT(my_perl);
    XPUSHs(sv_2mortal(newSViv(b)));


    /* Done with pushing pointers to Perl stack */

    PUTBACK;

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

    SPAGAIN;
    sv1 = POPs;
    PUTBACK;

    FREETMPS;
    LEAVE;

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

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

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

    if(perlBeginScript.length()==0)
    {
    perlBeginScript="EmbeddedPerl";
    }

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

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

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

    //perlScript += perlEndScript;

    /* Done with perl script to be executed */
    my_eval_sv(TRUE);
    PL_perl_destruct_level = 1;
    perl_destruct(my_perl);
    perl_free(my_perl);
    PERL_SYS_TERM();
    }
    Yogi, Sep 13, 2012
    #1
    1. Advertising

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

    Yogi <> writes:

    > I have the below sample program which pushes the arguments to Perl
    > stack and then calls "eval_sv".


    Just tried to make some guesses at your identical StackOverflow
    question.

    http://stackoverflow.com/questions/...l-sv-not-passing-arguments-to-script/12405016

    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
    subroutine.

    Your code is roughly comparable to a script doing:

    #!/usr/bin/perl

    @_ = (10, 20);

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

    __END__

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

    //Makholm
    Peter Makholm, Sep 13, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Navaneeth
    Replies:
    4
    Views:
    539
    Kenny McCormack
    Nov 20, 2010
  2. Debajit Adhikary
    Replies:
    36
    Views:
    2,239
    Andre Kaufmann
    Feb 10, 2011
  3. sed_y
    Replies:
    0
    Views:
    994
    sed_y
    Feb 15, 2012
  4. pj
    Replies:
    3
    Views:
    271
  5. Replies:
    5
    Views:
    99
    Tintin
    Feb 21, 2007
Loading...

Share This Page