Is a shared hash available?

I am trying to use a hash hash to keep a constant field and timestamp when this field was changed.

It should be shared, though, since I have 2 threads that are needed to access it, although only one sets the values.

here is my current code with a few comments:

my $status = {}; share($status); sub get_status { my($raid) = @_; return $status->{$raid}->{status} if exists $status->{$raid}; return 1; } sub set_status { my($raid,$newstatus) = @_; my %t; $t{status} = $newstatus; $t{timestamp} = Time::HiRes::time; $status->{$raid} = \%t; #$status->{$raid} = { #status => $newstatus, #timestamp => Time::HiRes::time() #}; #$status->{$raid}->{status}=$newstatus; #$status->{$raid}->{timestamp} = Time::HiRes::time; return 1; } set_status('680','1'); get_status('680'); 

I constantly get Invalid value for shared scalar at ./hashtest line 19.

Can anyone help: D?

+4
source share
1 answer

This perlmonks page should explain the problem. One post says:

You can share a nested hash if internal hashes are also marked as shared.

You can try something like this:

 share( %t ); $status->{ $raid } = \%t; 
+4
source

All Articles