For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > XS question, calling anonymous function from C









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XS question, calling anonymous function from C
elbob@mailinator.com

2005-04-25, 3:58 am

In order to avoid namespace pollution, I would like to do the following
in C:

SV* sv;
char* cmd = "FileHandle->new(q(foo.c));";
sv = eval_pv(cmd, TRUE);

Now, sv points to a class instance. Is there a way to call methods of
the class from w/in C? Preferably, with arguments?

TIA

Reinhard Pagitsch

2005-04-25, 8:58 am

Hello,

elbob@mailinator.com wrote:
> In order to avoid namespace pollution, I would like to do the following
> in C:
>
> SV* sv;
> char* cmd = "FileHandle->new(q(foo.c));";
> sv = eval_pv(cmd, TRUE);
>
> Now, sv points to a class instance. Is there a way to call methods of
> the class from w/in C? Preferably, with arguments?


Found in the book "Extending and Embedding Perl",
page 265 (http://www.manning.com/jenness):


char* tidy_string (char* input) {
SV* result;
setSVpv(DEFSV, input, 0);
result = eval_pv("use Text::Autoformat; autoformat($_)", FALSE);
if (SvTRUE(ERRSV))
return NULL;
else
return SvPV_nolen(result);
}


I hope this help.
But have you tried to ask the question in the perl.xs group?

regards,
Reinhard
Tassilo v. Parseval

2005-04-25, 8:58 am

Also sprach elbob@mailinator.com:

> In order to avoid namespace pollution, I would like to do the following
> in C:
>
> SV* sv;
> char* cmd = "FileHandle->new(q(foo.c));";
> sv = eval_pv(cmd, TRUE);
>
> Now, sv points to a class instance. Is there a way to call methods of
> the class from w/in C? Preferably, with arguments?


You can use call_method() for that. You first have to push 'sv' onto the
stack followed by all arguments the method should receive. See
perlcall.pod for details.

Tassilo
--
use bigint;
$n=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
elbob@mailinator.com

2005-04-25, 3:58 pm

I was trying comp.lang.perl.xs. I *knew* I had seen that group before.
Mucho gracias! I also got on the Safari group, so now I can access
books like that. Can't get 'em where I am.

El Bob

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com