Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Calling a C++ subroutine from Perl

by ibm1620 (Hermit)
on Mar 19, 2019 at 00:36 UTC ( [id://1231411]=perlquestion: print w/replies, xml ) Need Help??

ibm1620 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Our company obtained a C++ source code license for Metaphone3 and I want to test it using Perl as a driver. I'm wondering what my options are. In the past, when I needed to call a complex C subroutine from Perl, I've written a standalone C++ program that accepted command-line parameters, called the subroutine, and wrote the output to STDOUT, and then "called" it using qx//. That's certainly still a possibility, but on the offchance that the forking and IPC would pose a performance hit (I'm going to test 100M's of strings), I wondered if XS was a practical possibility.

I have never written an XS module, and it looks daunting, but I thought there might be some XS stub code out there that I could easily tweak so that a Perl program could call one subroutine, passing in one string and getting two strings back; end of story.

Any thoughts on the subject? Other approaches?

(Metaphone3 is also available in Perl, but unfortunately that's a separate license.)

Replies are listed 'Best First'.
Re: Calling a C++ subroutine from Perl
by tobyink (Canon) on Mar 19, 2019 at 01:02 UTC

    Inline::CPP should be pretty easy to get started with.

    Internally, I believe it translates stuff to XS (Inline::C does anyway), so once you have a working solution, you can grab that code and use that as the basis for an XS version.

      Internally, I believe it translates stuff to XS

      Yep - it writes the XS file, compiles it and then executes the script.

      Cheers,
      Rob
        And caches the compilation so you don't have to recompile on every invocation of the script, as I read the doc. Pretty cool!
Re: Calling a C++ subroutine from Perl
by swl (Parson) on Mar 19, 2019 at 02:29 UTC

    Have a look at FFI::Platypus. No XS is required, and the documentation is pretty comprehensive.

      Thank you. I will look into both of these suggestions. My admiration and gratitude for both Perl and the Perlmonks website are unbounded.
Re: Calling a C++ subroutine from Perl
by markong (Pilgrim) on Mar 19, 2019 at 16:59 UTC

    And to complete the list, there is also the always mighty SWIG, with excellent C++ support and documentation!

Re: Calling a C++ subroutine from Perl
by bliako (Monsignor) on Mar 19, 2019 at 18:26 UTC

    I will mention the possibility to embed a perl interpreter inside a C program. See perlembed. It has benefits if your main logic is in C/C++ and you just want a quick regex engine or accessing a database). For example: in perl embeded engine access database, retrieve data, filter data, use regex and other easy-peasy perl string mamipulations, then pass result back to C for metaphone. That gives you additional freedom depending where your main program logic is.

    There is a bigger prize though: parallelism. Theoretically you can have many perl interpreters in same C program. And you may find the C++ threads model, option of shared memory etc., is better suited for your needs than Perl's.

    I have not tried compiling a C++ program with perl embeded, only with C. The reason I am mentioning it is that it gives you one more way to experiment. I can give more details if you want to proceed. I must say that you may encounter a few hiccups in compiling and linking with perl's headers and library. But the major difficulty will be that the same compiler used in compiling your perl must also be used in compiling the perl-embedded C/C++ program. That means C++. Which theoretically should be OK but in practice it may complain about c++XYZ standard of the month forbidding this or that.

    bw, bliako

Re: Calling a C++ subroutine from Perl
by misc (Friar) on Mar 20, 2019 at 08:54 UTC
    There's also this tutorial: perlxstut

    It's been quite a while ago,
    so I'm not so sure about my solution with XS.

    But I still remember, first this XS stuff really confused me.
    But after getting an idea about the mechanisms,
    with the help of (I believe) the linked tutorial,
    it showed up quite straightforward and useful.

    Just don't give up too quick, it pays out.
    Best wishes, Michael
      Thanks Michael - it would be a great tool to be able to use. I'll give it a look.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1231411]
Front-paged by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found