[Note: The following is a more thorough treatment of a lamer, earlier post (and is x-posted to usenet as well)]

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"

In reply to Embedding: load_module(), stack frames and Carp by sjfloat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.