Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: globally seen variable

by moritz (Cardinal)
on Sep 28, 2010 at 15:39 UTC ( #862451=note: print w/replies, xml ) Need Help??


in reply to globally seen vairable

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.

Replies are listed 'Best First'.
Re^2: globally seen variable
by vit (Friar) on Sep 28, 2010 at 18:15 UTC
    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.
        I declared
        our $ERROR_FILE = $cgi_dir."files/logs/errors.txt";
        in main program and tried to use it in package:
        use strict; package RecordError; sub RecordError { my $error = shift; ## $ERROR_FILE is global var ## open OF, ">>$ERROR_FILE" or die "cannot open ERROR_FILE $ERROR_FILE\n" +; print OF "$error\n"; close OF; } 1;
        and it returns
        Global symbol "$ERROR_FILE" requires explicit package name at E:/..... +./lib/RecordError.pm line 12.
        What do I do wrong?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-10-18 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (102 votes). Check out past polls.

    Notices?