Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] segfault setting perl variables in C code


At 07 Apr 2003 17:45:25 +0930, David Fitch wrote:
> I have a C program that starts a perl interpreter and runs
> a perl script using perl_alloc, construct, parse and finally
> perl_run.  I'm trying to set a perl variable in the C code
> before starting the perl script.  This variable is then
> accessible from the perl script.  That's the idea anyway.
> 
> I reckon it should be possible using something like either
> of these (from reading 'man perlguts'):
> 
> SV *sv = newSVpv("MYID", 4);
> 
> or:
> 
> SV *sv = get_sv("MYID", TRUE|GV_ADDMULTI);
> sv_setpv(sv, 42);
> SvPOK_on(sv);
> 
> But either way just segfaults on the first call.

what do you want this variable to be called (eg:
"$Mystuff::Myvariable"), and what value do you want it to have?

your first bit of code creates a new scalar variable with the value
"MYID", but no name (you could return this sv from a function, for
example)

the second bit of code creates a variable called $::MYID, and then
sets it to the value *(char *)42, which is probably not what you
meant..


try something like:

 SV *sv = get_sv("MyPackage::foo", TRUE|GV_ADDMULTI);
 sv_setpv(sv, "bar");

and then

 #!perl
 print "foo is $MyPackage::foo\n";

-- 
 - Gus