Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

globally seen vairable

by vit (Pilgrim)
on Sep 28, 2010 at 14:37 UTC ( #862438=perlquestion: print w/ replies, xml ) Need Help??
vit has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to introduce a variable which is defined in the main program and is seen from external modules. Like static in C.
What is a drawback of such a variable, say for multithreading, etc.

Comment on globally seen vairable
Re: globally seen variable
by moritz (Cardinal) on Sep 28, 2010 at 15:39 UTC
    Is there a way to introduce a variable which is defined in the main program and is seen from external modules.

    Sure. See our.

    What is a drawback of such a variable

    There is extensive literature out there, for example search for why are global variables bad. In short: global state makes programs harder to understand, to debug, can cause problems with recursion and concurrency, and is often a sign of bad encapsulation.

    Perl 6 - links to (nearly) everything that is Perl 6.
      In this example from perldoc will not $bar declared "our" in Foo conflict in package Bar with "use strict" ?
      package Foo; our $bar; # declares $Foo::bar for rest of lexical scope $bar = 20; .......................... # package Bar; use strict; print $bar; # prints 20, as it refers to $Foo::bar
        Why should it?

        our $bar creates an entry in the current package, and a lexical alias to it. The lexical alias persists to the next package too, because packages and lexical scopes are completely orthogonal.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: globally seen vairable
by BrowserUk (Pope) on Sep 28, 2010 at 16:28 UTC
    What is a drawback of such a variable, say for multithreading,

    If it is read-only. Ie. just conveying some piece of static information from the main program to the modules/threads, there is no significant problem.

    If it is intended to act as a conduit for passing dynamic information back and forth between threads, then you would need to apply locking.

    In general, there is no difference between obtaining a piece of static information from a global variable, and calling a (global) function (singleton instantiator) to obtain that information. Both involve the calling code "knowing" an well-known name.

    Eg. $My::Config::DBhandle->somemethod( ... );

    or My::Config->new()->get_DBHandle()->somemethod( ... );


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      You may use the "vars" module.

      See: use vars;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-11-25 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (158 votes), past polls