Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Executing functions from another process

by gri6507 (Deacon)
on Jan 23, 2014 at 19:25 UTC ( #1071805=note: print w/replies, xml ) Need Help??


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 (Pope) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1071805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-11-25 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?