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

I know it's been asked before, and yes i have searched through the darkest corners of the internet and done my reading in every O'reilly "insert animal here" book i could find, but i could still use some extra wisdom to get me started. (please!)

My goal was to learn perl so that i could use it alongside c++ in a database management program im writing for myself. I managed to create a script that can organize XML data. I want to embed perl into a c++/Qt interface that can run my script (plus hopefully more soon) from the c++ code.

Is there an easy way to accomplish this? by easy i mean not having to delve into the internals of perl learn the API then create a C interface to exchange data with perl then get that C code to work with my c++? I started learning perl because i thought embedding into compiled languages was what interpreted languages did, and perl had CPAN (not to shabby :)) but i am suddenly feeling way over my head. any advice/tutorials or better, working sample code i could learn from?

- Thank you in advance

Replies are listed 'Best First'.
Re: embedding perl in c++
by kcott (Archbishop) on Mar 14, 2014 at 01:53 UTC

    G'day divitto,

    Welcome to the monastery.

    "... and done my reading in every O'reilly "insert animal here" book i could find, ..."

    How about the one with the Black Leopard? That's "Advanced Perl Programming": there's two editions although, in my opinion, that really should be two volumes as there seems be be very little overlap in the content.

    • Advanced Perl Programming is the original. It's quite old (1997) but has an entire chapter devoted to this: "19. Embedding Perl: The Easy Way".
    • Advanced Perl Programming, 2nd Edition is a little more recent (2005). I don't own a copy of this one but, from looking at the online "Table of Contents", it doesn't appear to broach the subject of embedding Perl.

    There's also the "Perl ways to embed perl in your C or C++ application" documentation which is part of the standard Perl distribution. That's in the "Internals and C Language Interface" section so possibly doesn't align with your "by easy i mean not having to delve into the internals of perl ..." statement.

    -- Ken

      Thanks for the info! i have read the chapters that involve embedding perl from the panther book, i plan on reading the rest as soon as i can. it seems like a great book, chapter 19 talks about a simple way to do it but it doesn't go into much detail and chapter 20 (the one that i think goes in depth) is lost on me. I haven't read your reference to the documentation though so i will definitely check those out.

      EDIT: On second thought i have read your first link, although this being my 5th or so time through it i think i might understand it a little better. Where i get lost is where it starts talking about xsubbs, and calling from the stack is a bit fuzzy.

      From what i make of it, I'm supposed to include the perl core files, adjust the C environment to handle the interpreter, and then i can run my scripts by putting the script file name in the argv array as if it was from the terminal. is that about right? Now im starting to feel like i was over thinking it...

Re: embedding perl in c++
by Marshall (Canon) on Mar 14, 2014 at 04:07 UTC
    I'm not sure what your are trying to do?
    C or C++ is complex and not a good idea for UI. I would not recommend that.
    The Perl DBI for SQL is relatively simple.

      I've been using c++ for a long time and ive come to really enjoy Qt as a gui library. I want to use perl inside a c++/Qt program for tasks like data sorting, searching etc.. im using c++ because i love it and i want to embed perl because i think having the skill set to do so would be invaluable in future projects, Where c++ would be ideal/mandatory and not just personal preference such as a game engine or something. does that answer your question?

        It's not exactly what i wanted but i found out Qt has a way to do this called QProcess, it doesn't embed perl but it can call the interpreter and run the scripts which is close enough for now. I plan to keep educating myself until the perlembed documentation doesnt quite look so daunting. I'm sure you will all be hearing from me again i've found a deep interest in the perl ways :) thank you for your wisdoms, happy coding.

Re: embedding perl in c++
by clueless newbie (Curate) on Mar 13, 2014 at 20:56 UTC

      Thank you for responding, I've seen inline::cpp but i was under the impression that it's functionality was the opposite of what i wanted. The way i read it it seemed like it's purpose was to use c++ from perl, but i want to use perl from c++. I hope that makes since... Did i just misunderstand the module?

        Sorry for double posting but libperl++ is closer to what i am looking for i think. The problem is from everything i've read suggests that libperl++ doesn't work on windows. Although i develop on a Linux box i need my app on my Windows machine so i don't think i can use it.

        My bad. I was think of Inline::Java which has an Inline::Java::Callback.