hmonroe,

I have a 20,000+ line package with about 6 modules, and I use 'our' in parent that forks copies of itself once all global data has been initialized. I'm sure this is just another way of looking at the problem, but it does work for forked copies, so I'm sure it will work for all modules sharing one address space. The sample code is just a skeleton.

parent code:

{ ## Parent our ( %hash, @array, $scalar, $LOCK ); ## Global data my $lockfile = "./GobalLock"; open ( $LOCK, ">>", $lockfile ) or die "Can't open $lockfile; $!\n"; ... # code to fork and maintain number of children }

If your package is running as a single user, you don't have to lock the parent globals, but you must lock them if more than one module needs access to the global data. When the children want to get or update global data:

{ my $need; ... if ( flock( $LOCK, LOCK_SH ) ) ## Get a copy of Global data { $need = $hash{"stuff"}; flock( $LOCK, LOCK_UN ); } else { die "$!\n"; } ... if ( flock( $LOCK, LOCK_EX ) ) ## Update the Global data { $hash{"stuff"} = $need; flock( $LOCK, LOCK_UN ); } else { die "$!\n"; } ... }

If you scope each module with a separate pair of brackets {}, and you are of course running with 'strict' and 'warnings', sometimes I get an error that I need to define a global. I just add the 'our ...' after the top '{' and it covers the entire module. It doesn't seem to happen as much as in the past, but if you see it just add the our in scope.

Good Luck!

"Well done is better than well said." - Benjamin Franklin


In reply to Re: Accessing hash from within module by flexvault
in thread Accessing hash from within module by hmonroe

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.