[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1223
  • Last Modified:

Threads - Shared - Reference

Hi Friends, I have the following code:

use threads;
use threads::shared;
my $a='abc';
my $b : shared = \$a;

For this, I'm getting the error: Invalid value for shared scalar at del.pl line 4.

Basically, I would like to have a variable(here $b) which is shared and holds a reference. Is this possible? If so please help me.

Thanx
0
vi_srikanth
Asked:
vi_srikanth
  • 2
2 Solutions
 
MercantilumCommented:
You can't put a reference to an unshared value in a shared scalar:

use threads;
use threads::shared;
my $bar;
my $foo : shared;
$foo = \$bar;

gives:

Invalid value for shared scalar at x line 5.

See http://www.nntp.perl.org/group/perl.ithreads/758
0
 
joespCommented:
why not:

#!/usr/bin/perl
use threads;
use threads::shared;
my $a= "abc";
my $b = &share(\$a);
print $a . "\n";
print $$b . "\n";

0
 
MercantilumCommented:
So, you could simply

use threads;
use threads::shared;
my $bar : shared;
my $foo : shared;
$foo = \$bar;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now