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;

In reply to Re: Threads, dynamic loading programs, and using globals? by pg
in thread Threads, dynamic loading programs, and using globals? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.