http://qs321.pair.com?node_id=1071805


in reply to Re: Executing functions from another process
in thread Executing functions from another process

Conceptually I understand how this could be done, but I have two problems with this proposed solution. In my full application, the actual functions being executed are not even defined in the same script. They get pulled in via a do() call to another script. It would be a maintenance headache to maintain a dispatch table in this script. It's much easier to simply get the function name and execute it as a function ref.

The second problem is more practical. The prototypes of functions that get called are all different. Some have no args, some have one scalar, others have multiple array refs, etc. How could I set up a generic JSON message to convey such a varying list of parameters?

Replies are listed 'Best First'.
Re^3: Executing functions from another process
by Corion (Patriarch) on Jan 23, 2014 at 19:29 UTC

    How would you set up the parameters in Perl?

    Just set up the parameters, and then print them as encoded JSON. On the other side, reverse the process.

    For example, the following structure should work fine for passing parameters.

    { "parameters": [], "function": "frobnitz" }

    Also, maybe JSON::RPC is of help.

      Ok. I've tried it and it works great, but with one key problem. It appears that when JSON encodes the parameters, it does so "by value". In other words, if my function call is supposed to take an argument by reference to a scalar, with the intent of modifying it, this method does not work.

        Now is a good time to review perlipc.

        The only way to modify values within a Perl process is to do it within that one Perl process. Either you use threads, or you set up an API to do that modification for you.

        There are other approaches, like shared memory or debuggers, but until you've understood the limitations of IPC, it doesn't make sense to bring out the heavy guns.

        inter process communication works by bytes -- values -- you cannot pass references between processes -- you have to serialize them to bytes