http://qs1969.pair.com?node_id=520354

jithoosin has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Please forgive me if my doubt is too simple.I tried my best ,but not getting the answer.
I have a global hash ("our %hash") in package and the hash is used only in a function in that package. The function is accessed from different instances of an Module(Class). But each instance is creating it's own version of hash. Is there any way by which all instances use the same global hash rather than creating its own hash.
Let me make clear that the Module which is accessing the hash is different from the module which contains the hash

Thanks
Kiran

Replies are listed 'Best First'.
Re: global variable in a package
by ysth (Canon) on Jan 02, 2006 at 07:49 UTC
    Can you show some code? You seem to be contradicting yourself: "the hash is used only in a function in that package" vs. "the Module which is accessing the hash is different from the module which contains the hash".

    You can access a global variable in a different package by prefixing with the package name, e.g. keys %packagename::hash

Re: global variable in a package
by mikeock (Hermit) on Jan 02, 2006 at 20:17 UTC
    Maybe you should look how the variable is being declared.. From what I can gather, you should declare it in the package as a my %hash.

    From there you then declare an our within the sub or whatever is using the hash.

    Camel -> pg 134

    "Our never creates values; it just exposes a limited form of acces to the global, which lives forever.