Hullo,

I'm calling code in a perl module from C. The problem is that since I'm
loading the module via load_module() my Carp calls all break --
apparently because there's no caller to reference back to.

I'd appreciate any suggestions or insight into this.

= My C =============================================================

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

    static PerlInterpreter *my_perl;

    EXTERN_C void       xs_init(pTHX);

    int
    main(int argc, char **argv, char **env)
    {

        char               *my_argv[] = { "", "/dev/null" };
        int                 count;
        SV                 *ex;     /* the object */

        char                buf[] = "some arbitrary data";
        int                 bufsiz;

        PERL_SYS_INIT3(&argc, &argv, &env);
        my_perl = perl_alloc();
        perl_construct(my_perl);


        /* parse the "file" */
        perl_parse(my_perl, xs_init, 2, my_argv, (char **) NULL);

        PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
        perl_run(my_perl);


        dSP;

        /* Load the module */
        load_module(PERL_LOADMOD_NOIMPORT, newSVpvn("Ex", 2),
newSVnv(0.01));

        ENTER;
/**************************************************************/
        SAVETMPS;

        /* instantiate an object */
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpvn("Ex", 2)));
        PUTBACK;
        count = call_method("new", G_SCALAR);
        SPAGAIN;
        if (count != 1)
            croak("Something weird with Ex::new()\n");
        ex = POPs;

        /* call a method */
        bufsiz = strlen(buf);       /* just an example, after all */
        PUSHMARK(SP);
        XPUSHs(ex);
        XPUSHs(sv_2mortal(newSVpvn(buf, bufsiz)));
        XPUSHs(sv_2mortal(newSViv(bufsiz)));
        PUTBACK;
        count = call_method("do_something", G_SCALAR);
        SPAGAIN;
        if (count != 1)
            croak("Something weird with $ex->do_something()\n");

        FREETMPS;
        LEAVE;
/**************************************************************/


        perl_destruct(my_perl);
        perl_free(my_perl);
        PERL_SYS_TERM();

    }

= My Perl module =====================================================

    package Ex;
    use warnings;
    use strict;
    use Data::Dumper;
    use Carp;

    our $VERSION = '0.01';

    sub new {
        my $class = shift;
        my %args  = @_;
        my $self  = \%args;
        bless $self, $class;
        return $self;
    }

    sub do_something {
        my $self = shift;

        warn qq(\n"caller()"\n) . Dumper( [ caller() ] ) . "\n";
        warn qq("warn"ing);
        carp qq("carp"ing);
        print qq(you gave me "$_[0]"\n);

        return;
    }

    1;

= the Output =========================================================

    "caller()"
    $VAR1 = [
              'main',
              '/dev/null',
              0
            ];

    "warn"ing at lib/Ex.pm line 21.
    "carp"ing at /dev/null line 0
    you gave me "some arbitrary data"

===========================================================================

Reply via email to