Re: calling function written for perl consumption from c

[email protected] (David Mertens) Mon, 12 Jun 2017 23:24:06 -0400
Newsgroups perl.xs
Message-ID <CA+4ieYWAnRf4Z90U-ENAh8A2OKLe1Tzz4j97-T-AuNyf6Kb+_w@mail.gmail.com>
--94eb2c03596edc71720551cef700
Content-Type: text/plain; charset="UTF-8"

Hello Bill,

In my experience, it is easier to to split the function into two pieces, a
pure C function and a thin XS wrapper. This makes it easy for other C code
to call the pure C version, while providing a Perl-accessible hook.

If you need to get that C function in XS code for an unrelated module, you
could try many approaches. One is to stash the pure C function's pointer in
an obfuscated package variable. Or you could try using ExtUtils::Depends.
Let me know if you need a bit more help as this is a little trickier.

If you want to call the xsub itself from another xsub, you'll need to
prepare the argument stack. I am pretty sure that this is just like calling
any other Perl function. For that, see details in perlcall:
http://perldoc.perl.org/perlcall.html

Finally, I wrote C::Blocks, which provides an entirely different approach
to this problem.

Hope that helps!
David

On Mon, Jun 12, 2017 at 9:05 AM, Bill Schoolfield <[email protected]> wrote:

> Hi,
>
> Completely new to perlxs. So far I've managed to write a few c functions
> that work fine when referenced in perl. Now I need to call one of those
> functions from another c function. I assume that is possible and I assume
> the perl stack has to be used to make the call instead of the normal c
> conventions. Is this right?
>
> Can someone point me in the right direction on how to do this?
>
> Bill
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan

--94eb2c03596edc71720551cef700
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><div><div>Hello Bill,<br><br></div>In my ex=
perience, it is easier to to split the function into two pieces, a pure C f=
unction and a thin XS wrapper. This makes it easy for other C code to call =
the pure C version, while providing a Perl-accessible hook.<br><br>If you n=
eed to get that C function in XS code for an unrelated module, you could tr=
y many approaches. One is to stash the pure C function&#39;s pointer in an =
obfuscated package variable. Or you could try using ExtUtils::Depends. Let =
me know if you need a bit more help as this is a little trickier.<br><br></=
div>If you want to call the xsub itself from another xsub, you&#39;ll need =
to prepare the argument stack. I am pretty sure that this is just like call=
ing any other Perl function. For that, see details in perlcall: <a href=3D"=
http://perldoc.perl.org/perlcall.html">http://perldoc.perl.org/perlcall.htm=
l</a><br><br></div>Finally, I wrote C::Blocks, which provides an entirely d=
ifferent approach to this problem.<br><br></div>Hope that helps!<br></div>D=
avid<br></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On =
Mon, Jun 12, 2017 at 9:05 AM, Bill Schoolfield <span dir=3D"ltr">&lt;<a hre=
f=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt;</s=
pan> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
Completely new to perlxs. So far I&#39;ve managed to write a few c function=
s that work fine when referenced in perl. Now I need to call one of those f=
unctions from another c function. I assume that is possible and I assume th=
e perl stack has to be used to make the call instead of the normal c conven=
tions. Is this right?<br>
<br>
Can someone point me in the right direction on how to do this?<span class=
=3D"HOEnZb"><font color=3D"#888888"><br>
<br>
Bill<br>
</font></span></blockquote></div><br><br clear=3D"all"><br>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature">=C2=A0&quot;Debugg=
ing is twice as hard as writing the code in the first place.<br>
 =C2=A0 Therefore, if you write the code as cleverly as possible, you are,<=
br>
 =C2=A0 by definition, not smart enough to debug it.&quot; -- Brian Kernigh=
an<br></div>
</div>

--94eb2c03596edc71720551cef700--