2

Why I get this error?

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;

1 Answer 1

5

Use shared_clone() when using a variable (anonymous hash in this case) in an assignment.:

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;

Output:

$VAR1 = {
          'manager' => {
                         'surname' => 'Doe',
                         'name' => 'John',
                         'age' => 27
                       } 
        };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.