Grokbase Groups Perl xs May 2006
FAQ

Steve writes:
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.
If you are geting errors in the call_method() I think adding G_EVAL
will provide _a_ level for caller to find.

If it is the load_module() itself that is croaking then it is more tricky.

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',

];

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

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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 8 | next ›
Discussion Overview
groupxs @
categoriesperl
postedMay 25, '06 at 7:10p
activeMay 31, '06 at 3:45p
posts8
users2
websiteperl.org

2 users in discussion

Steve: 6 posts Nick Ing-Simmons: 2 posts

People

Translate

site design / logo © 2019 Grokbase