I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2010-05-14 12:31:30.028264-07 by titang
Passing shared variables among subroutines for thread::shared variables

Please help. Here is my problem.

I wrote a program, uses threads::shared to share 2 variables ($x, %y). I access them in my subroutines called thread_xy. When I have the main program and the subroutine inside a single file, everything is Fine.

Now I put thread_xy into a Package called: myPackage.pm. It no longer work. I tried to declare the variable inside myPackage.pm, passing the variable within the thread_xy(...), and tried to use use "qw" etc. None worked: the shared variables I modified inside thread_xy(...) is not passed back to my main program.

How I can declare the shared variables correctly between the main program and a subroutine when I put the subroutine in a Package (separated from my main program)?

Direct Responses: 12705 | Write a response
Posted on 2010-05-14 13:00:40.687545-07 by jdhedden in response to 12704
Re: Passing shared variables among subroutines for thread::shared variables
Main code:
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use bork; our $bork : shared; MAIN: { print("Setting to 123\n"); $bork = 123; threads->create('bork::thr')->join(); } print("Done\n"); # EOF
Code for 'bork.pm':
#!/usr/bin/perl use strict; use warnings; package bork; use threads; use threads::shared; sub thr { print("I see it as '$::bork'\n"); } 1; # EOF
The key is that variables in the 'main' package are accessed as $::var, etc..
Direct Responses: Write a response