Rex icon indicating copy to clipboard operation
Rex copied to clipboard

issue that prevents storing hash in shared hash

Open krimdomu opened this issue 9 years ago • 6 comments
trafficstars

Currently it is not possible to store nested hashs (and i think also arrays) inside a shared variable.

see: #921

krimdomu avatar Apr 01 '16 13:04 krimdomu

@shattered @krimdomu: we are testing for the following data structure as far as shared hashes concerned:

%hash = (
  name     => "joe",
  surename => "doe",
  multi    => {
    key1 => "foo",
    arr1 => [qw/bar baz/],
  }
);

It seems to have a nested hash, and a nested array too, and tests pass.

Can either of you provide an example that fails with shared hashes, please? I would be glad to fix it.

ferki avatar Apr 25 '16 17:04 ferki

Given an empty %hash, this prints '42' twice:

$hash{'a'} = { b => 42 };
say $hash{'a'}->{b};

$hash{'a'}->{b} = -1;
say $hash{'a'}->{b};

shattered avatar Apr 29 '16 21:04 shattered

@shattered: thanks for the example! I'll write a testcase and will look into it.

ferki avatar Apr 29 '16 21:04 ferki

The problem is, that nested references are not tied objects. This is why it stores the first value and never updates it.

I'll move this issue into the backlog and update the documentation that currently it is not possible to store multi dimensional things in shared variables.

krimdomu avatar May 21 '16 09:05 krimdomu

I'm currently working on covering more and more cases around Rex::Shared::Var. Already overhauled arrays woth more tests, hashes are up next. I'll see what I can do about this one too.

ferki avatar May 21 '16 12:05 ferki

Note for anyone looking at this later:

In the original code example, the second assignment triggers only a FETCH operation on the tied object:

$hash{'a'} = { b => 42 };
say $hash{'a'}->{b};

$hash{'a'}->{b} = -1;
say $hash{'a'}->{b};

As a workaround, it works if the nested value is also a shared variable:

BEGIN {
  use Rex::Shared::Var;
  share qw(%hash %hash2);
}

use DDP;

$hash2{b} = 42;
p %hash2;

$hash{a} = \%hash2;
p %hash;

$hash{a}->{b} = -1;
p %hash;

In this sense, the documentation may mention something like "nesting works if assignments are done on the top-level, or when all nested levels are shared variables as well" (until fixed).

ferki avatar May 29 '20 15:05 ferki