RexOps / Rex

Rex, the friendly automation framework
https://www.rexify.org
716 stars 223 forks source link

issue that prevents storing hash in shared hash #1009

Open krimdomu opened 8 years ago

krimdomu commented 8 years ago

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

see: #921

ferki commented 8 years ago

@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.

shattered commented 8 years ago

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

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

$hash{'a'}->{b} = -1;
say $hash{'a'}->{b};
ferki commented 8 years ago

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

krimdomu commented 8 years ago

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.

ferki commented 8 years ago

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 commented 4 years ago

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).