在 Perl 中使用共享散列
Using shared hash in Perl
为什么会出现此错误?
Thread 1 terminated abnormally: Invalid value for shared scalar at thr_hash.pl line 8.
use threads;
use threads::shared;
use Data::Dumper;
my %h:shared;
threads->create(sub{
$h{manager} = {
name => 'John',
surname => 'Doe',
age => 27
};
})->detach;
sleep 1;
print Dumper \%h;
在赋值中使用变量(在本例中为匿名散列)时使用 shared_clone()。:
use threads;
use threads::shared;
use Data::Dumper;
my %h:shared;
threads->create(sub{
$h{manager} = shared_clone({
name => 'John',
surname => 'Doe',
age => 27
});
})->detach;
sleep 1;
print Dumper \%h;
输出:
$VAR1 = {
'manager' => {
'surname' => 'Doe',
'name' => 'John',
'age' => 27
}
};
为什么会出现此错误?
Thread 1 terminated abnormally: Invalid value for shared scalar at thr_hash.pl line 8.
use threads;
use threads::shared;
use Data::Dumper;
my %h:shared;
threads->create(sub{
$h{manager} = {
name => 'John',
surname => 'Doe',
age => 27
};
})->detach;
sleep 1;
print Dumper \%h;
在赋值中使用变量(在本例中为匿名散列)时使用 shared_clone()。:
use threads;
use threads::shared;
use Data::Dumper;
my %h:shared;
threads->create(sub{
$h{manager} = shared_clone({
name => 'John',
surname => 'Doe',
age => 27
});
})->detach;
sleep 1;
print Dumper \%h;
输出:
$VAR1 = {
'manager' => {
'surname' => 'Doe',
'name' => 'John',
'age' => 27
}
};