Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

global variable in a package

by jithoosin (Scribe)
on Jan 02, 2006 at 07:22 UTC ( #520354=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://520354]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2023-02-07 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?