Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^7: Executing functions from another process

by gri6507 (Deacon)
on Jan 24, 2014 at 15:09 UTC ( [id://1071953]=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^8: Executing functions from another process
by BrowserUk (Patriarch) on Jan 24, 2014 at 15:37 UTC
    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.
      So it sounds like dump+eval does not create a true copy of the data structure. What alternate methods are there to create a true clone of a structure so that even the referenced values could be writable?
        So it sounds like dump+eval does not create a true copy of the data structure.

        None that I am aware of will retain knowledge that one scalar in the structure is a reference to another scalar in that same structure.

        Ie. If you have this structure (which is different to what you showed, but may be what you intended):

        $a = 'one'; $b = 'two'; @d = ( $a, $b, \$b );;

        Then neither Storable:

        use Storable qw[ freeze thaw ];; $x = freeze \@d;; $y = thaw $x;; pp $y;; ["one", "two", \"two"] ${ $y->[2] } = 'three';; pp $y;; ["one", "two", \"three"]

        Nor Clone successfully capture that scalar reference relationship:

        use Clone qw[ clone ];; $x = clone \@d;; pp $x;; ["one", "two", \"two"] ${ $x->[2] } = 'three';; pp $x;; ["one", "two", \"three"]

        You could say that was a bug -- and I'd probably agree with you -- but it is a long standing one that it would appear that no one has till now seen as a problem. Which is another way of saying it is unlikely to be fixed -- or even accepted as a bug -- any time soon.


        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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found