Dear Monks of Perl,
A C (or C++) program needs to have a script language for running simple and complex scripts. I considered Perl, because it can run independent from the programs regular memory space (there is no risk that a script does something bad with the program's memory) and the few interaction commands (like output to user, input from user, get or set some data) could be easily build as a Perl module. The interpreter needs to run within the master program - which could be done by loading the perl lib.
Here is a sample script:
use MasterProgram; MasterProgram::output("Hello World!"); MasterProgram::waitOK(); MasterProgram::clear(); MasterProgram::output("foo bar");
This doesn't really require perl but it is sufficent for demonstation of the problem:
The master starts this script, it shows a message, waits for an OK click and shows another message. While waiting for the OK click, there are two problems:
  • The master must be able to contiue doing other jobs. This could be done at the waitOK() or using threads in the Master, no real problem, but
  • Another instance of the script may be spawned while waiting for the OK-click. Our script run must block until the OK is received (waitOK() will do the blocking), but the other instance must run indepently from this
  • With an expected amount of 1000 - 5000 instances which must run at the same time, one perl instance for every instance isn't an option. There will be about 200 - 500 different fixed scripts. Changing a script may require restarting the master

    My idea was: Split the Perl code in blocks at every blocking command (like waitOK) either manually or using PPI, store each script in an array. Every array element is a anonymus sub-ref to a block including the blocking command. For our example:

    @Script = ( sub { MasterProgram::output("Hello World!"); MasterProgram +::waitOK(); }, sub {MasterProgram::clear(); MasterProgram::output("foo ba +r"); });
    Every script gets one Hash ref for storing all run-time variables which need to survive the whole script. my-variables will end at the next blocking command.
    The master starts an instance by creating a hash, pre-filling some neccessary values and running the first block. Whenever the master wishes to end a blocking command, it runs the next item of the array giving. Every item gets the instance's hash ref as the first argument.

    Question 1: Even if the concept is still in a very early stage and many things need to be considered... would it be possible?
    Question 2: Is there an easier way?

    Thank you.


    In reply to Perl as embedded script language by Sewi

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.