Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: globally seen variable

by vit (Friar)
on Sep 28, 2010 at 18:15 UTC ( #862479=note: print w/replies, xml ) Need Help??

in reply to Re: globally seen variable
in thread globally seen vairable

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

Replies are listed 'Best First'.
Re^3: globally seen variable
by moritz (Cardinal) on Sep 28, 2010 at 18:20 UTC
    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/ line 12.
      What do I do wrong?
        Contrary to the example you gave previously, you now seem to have the two snippets in question in different files. Which means that they don't share a lexical scope. So my explanation with lexical aliases doesn't apply in the new case.

        A lesson you can learn from is that it's not efficient to ask very general questions first, if you really want to solve a specific problem; neither is asking a question that doesn't actually demonstrate the problem.

        To answer your question, you need to either declare a lexical alias in each scope, to the same variable in the same package, or you need to qualify the namespace in one of the locations:

        { our $ERRORFILE = 'foo'; } { package RecordError; print $main::ERRORFILE, "\n"; }
        Perl 6 - links to (nearly) everything that is Perl 6.

        You need to use our $ERROR_FILE; in each scope in which you need visibility.

        Rather than this comment....

        ## $ERROR_FILE is global var ## open OF, ">>$ERROR_FILE" or die "cannot open ERROR_FILE $ERROR_FILE\n" +;

        This serves the same purpose, and gives you that visibility.

        our $ERROR_FILE; open OF, ">>$ERROR_FILE" or die "cannot open ERROR_FILE $ERROR_FILE\n" +;

        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://862479]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-07-20 17:09 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (438 votes). Check out past polls.