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-02-12 10:41:50.735554-08 by tetley
Shared variables across require?
Hi, Are there any known issues with sharing thread variables across "require"'d files? I will do this:


my %tid : shared;

sub start_th {
$tid{hello} = 99999;
require "child.pl";
...
}

where child.pl somewhere contains:
$tid{hello} = 11111;

If I make %tid shared, the value of $tid{hello} is 99999, even though child.pl is supposed to be setting it to 11111 in the same thread. If I set $tid{hello} to 11111 in the parent.pl (without the require), it correctly sets to 11111. If I don't share %tid at all, it correctly returns 1111 1 even with the require, but now I can't access the value from outside that thread. I tried putting/not putting the require in a BEGIN, everything. thanks!
Direct Responses: 12373 | Write a response
Posted on 2010-02-12 11:21:42.320216-08 by jdhedden in response to 12371
Re: Shared variables across require?
First of all, you need to 'use strict;' and 'use warnings;'. That would have told you that there was an issue with child.pl:

Global symbol "%tid" requires explicit package name at child.pl line 4.
Compilation failed in require at ./parent.pl line 10.


When you want to influence variables across 'require' you need to declare them as 'our'; not 'my'. The following works:
#!/usr/bin/perl # parent.pl use strict; use warnings; use threads; use threads::shared; our %tid : shared; sub start_th { $tid{hello} = 99999; print($tid{hello}, "\n"); require "child.pl"; print($tid{hello}, "\n"); } start_th;
use strict; use warnings; use threads; use threads::shared; our %tid : shared; $tid{hello} = 11111; 1;
Direct Responses: 12374 | Write a response
Posted on 2010-02-12 11:24:05.031024-08 by jdhedden in response to 12373
Re: Shared variables across require?
Sorry, hit 'submit' before I was ready. Here's the correct code:

parent.pl:
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; our %tid : shared; sub start_th { $tid{hello} = 99999; print($tid{hello}, "\n"); require "child.pl"; print($tid{hello}, "\n"); } start_th;

child.pl:
use strict; use warnings; use threads; use threads::shared; our %tid : shared; $tid{hello} = 11111; 1;
Direct Responses: 12376 | Write a response
Posted on 2010-02-12 12:38:57.26109-08 by tetley in response to 12374
Re: Shared variables across require?
I really appreciate the very fast response, but same problem. Now, thread it:

use strict; use warnings; use threads; use threads::shared; our %tid : shared; sub start_th { $tid{hello} = 99999; $tid{hello} = 88888; print("1: " . $tid{hello} . "\n"); require "child.pl"; print("2: " . $tid{hello} . "\n"); $tid{hello} = 22222; } my $myth = threads->create(\&start_th); $myth->join(); print("3: " . $tid{hello} . "\n");


child.pl is a straight cut-and-paste of what you had. In fact...the 22222 didn't even register, and that's after the require--not in it. Comment out the require, and it does. I get 88888 with require, 22222 without.
Direct Responses: 12377 | Write a response
Posted on 2010-02-12 12:53:03.607326-08 by jdhedden in response to 12376
Re: Shared variables across require?
Ah. I see my mistake. Here's what works:

parent.pl
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my %tid : shared; sub start_th { $tid{hello} = 99999; print("1: " . $tid{hello} . "\n"); $tid{hello} = 88888; print("2: " . $tid{hello} . "\n"); require "child.pl"; print("3: " . $tid{hello} . "\n"); $tid{hello} = 22222; print("4: " . $tid{hello} . "\n"); } my $myth = threads->create(\&start_th); $myth->join(); print("5: " . $tid{hello} . "\n");

child.pl
use strict; use warnings; use threads; use threads::shared; $::tid{hello} = 11111; 1;

output:
> ./parent.pl 1: 99999 2: 88888 3: 11111 4: 22222 5: 22222
Direct Responses: 12379 | Write a response
Posted on 2010-02-12 15:00:09.349535-08 by tetley in response to 12377
Re: Shared variables across require?
After switching the my to an our, that appears to work.
Direct Responses: 12421 | Write a response
Posted on 2010-02-17 08:09:59.721657-08 by tetley in response to 12379
Re: Shared variables across require?
thanks for the help. over and out. I may let you know if I find any issues with locking. I'll be hammering threads::shared in that area before too long.
Direct Responses: Write a response