Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^5: Executing functions from another process

by Anonymous Monk
on Jan 23, 2014 at 23:09 UTC ( #1071848=note: print w/replies, xml ) Need Help??

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

inter process communication works by bytes -- values -- you cannot pass references between processes -- you have to serialize them to bytes
  • Comment on Re^5: Executing functions from another process

Replies are listed 'Best First'.
Re^6: Executing functions from another process
by davido (Cardinal) on Jan 23, 2014 at 23:30 UTC

    That's exactly the point I was glossing over when I recommended a dispatch table at the receiving end, and a JSON producer at the sending end..

    The OP doesn't have to use JSON, but it's convenient, and well understood. The point is that the OP cannot simply pass references around, he has to pass plain old data. So a JSON packet that looks something like this:


    ...would be easy to interpret at the receiving end. And the dispatch table avoids the mess of going directly to symbolic subrefs.

    Things get ugly if he's got to send huge amounts of data to the subroutines. But even there, an additional layer of indirection can help; instead of sending data as a parameter, send a link to the data; a filename, a row ID from a database, or whatever. But sending a Perl reference is an indirection that will lose its magic as it gets passed around. ...just like you wouldn't pass a C pointer to local data from one process to another.


      The subroutines that I am trying to call from one process to the other do indeed pass a bunch of data, some of which is by reference. I understand that this can't be done due to the memory space separation of the two processes. I also understand that I would need to
      1. serialize the parameters on the caller side, flattening out any references
      2. deserialize the parameters on the receiving side into the same prototype
      3. actual function executes with the deserialized parameters, potentially modifying the by reference parameters
      4. then serialize the modified parameters again to capture the modified values
      5. deserialize the parameters on the original caller side to update the parameter values
      I've put together a very simple example of the first 3 steps where I use Data::Dump for my (de)serialization. However, I am running into an issue.
      use warnings; use strict; use Data::Dump qw(dump); my $a = "one"; my $b = "two"; my $c = \$b; my @d = ($a, $b, $c); my $str = dump(@d); # ("one", "two", \"two") my @newD = eval $str; $newD[0] = "A"; $newD[1] = "B"; ${$newD[2]} = "C";
      The problem happens on the last line which errors out with Modification of a read-only value. Why is this the case?
        The problem happens on the last line which errors out with Modification of a read-only value. Why is this the case?

        Because the combination of the dump + eval equates to:

        my @newD = ("one", "two", \"two");

        Which means that $newD[ 2 ] is assigned a reference to the string literal "two"; and when you indirect through that reference, you are trying to modify that string literal.

        With the other two elements of the array, new variables are created which are initialised to read-write copies of the string literals.

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-12-02 06:09 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (33 votes). Check out past polls.