in reply to Threads, dynamic loading programs, and using globals?

First I have to agree that, your way of using thread and package is quite creative, and to be frank, I like the thinking. But perl does have a little problem with this.

The main problem is that. The shared variable can only be shared within one invocation of the package, or we can say that, that shared variable has a per package scope.

If you have two invocations of a package. Each of them will have its own instance of that shared variable, not a SINGLE one as you expected. (I have to say that Perl does this perfectly, and this is how it should be.)

You may have to redesign the overall structure of your application.

I attached some pieces of code to test this.
package ex3.pm: use threads::shared; use strict; my $p; share($p);#a shared variable, and we will see that it has a per invoca +tion scope. sub process1 { while (1) { print "process1 thinks \$p = $p\n";#if it is shared, you will +see the bumps sleep(1); } } sub process2 { while (1) { $p ++; print "process2 bumped \$p to $p\n";#the bump should affect pr +ints from process1, if $p is shared. sleep(1); } } ex3.pl: demo the failure of share between invocations. use threads; use strict; my $th1 = threads->create (sub {require ex3;#invocation 1 process1(); }); my $th2 = threads->create (sub {require ex3;#invocation 2 process2(); }); $th1->join; ex4.pl: demo the succ of share within the same invocation. use threads; use ex3;#once for all use strict; my $th1 = threads->create (sub { process1(); }); my $th2 = threads->create (sub { process2(); }); $th1->join;