Barley streams :: common to a nested data structure

# thread::shared only allws a single level of shared structure
# needs a explicit hash creation for nested data structures
my %counts     : shared = ();

foreach my $state (%known_states) {
    # COUNTS
    unless (exists $counts{build}) {
        my %cb : shared;
        $counts{build} = \%cb;
    }
    $counts{build}{$state} = 0;

}

Right now, I have to do something like the above, where I must explicitly create a hash link for each hash at the unit level.

Is there a better way to do this?

PS if I do not create a hash code, I get the error "Invalid value for a common scalar", as I try to use it as a hash.

+4
source share
1 answer

Autovivitation makes

$counts{build}{$state} = 0;

behave like

( $counts{build} //= {} )->{$state} = 0;

For readability, make two lines

$counts{build} //= {};
$counts{build}{$state} = 0;

But, as you said, we need a common hash.

$counts{build} //= &share({});
$counts{build}{$state} = 0;

You can add the following to make sure that you do not accidentally vibrate the lone variable:

no autovivification qw( fetch store exists delete );
+5
source

All Articles