开发者

Shared Hashref of hashes possible?

开发者 https://www.devze.com 2023-04-03 20:17 出处:网络
Im trying to use a hashref of hashes to store a persistant field and a timestamp of when that field was changed.

Im trying to use a hashref of hashes to store a persistant field and a timestamp of when that field was changed.

It needs to be shared though as I have 2 threads that are required to access it although only one sets the values.

here is my current code with several commented out variations:

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_s开发者_StackOverflow社区tatus('680');

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

Could anyone help please :D?


This perlmonks page should explain the problem. One of the posts claims:

You can share nested hash, as long as the internal hashes are also marked as shared.

You can try something like this:

share( %t );
$status->{ $raid } = \%t;
0

精彩评论

暂无评论...
验证码 换一张
取 消