Why are variables declared with “our” visible across files?

后端 未结 2 1722
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 01:59

From the "our" perldoc:

our has the same scoping rules as my, but does not necessarily create a variable.

This means that vari

2条回答
  •  忘了有多久
    2021-02-06 02:28

    You have a good answer already, but perhaps this will be helpful as well.

    The our declaration combines aspects of my and use vars. It functions similarly to use vars in that it declares package variables; however, variables declared in this way are lexically scoped and cannot be accessed outside the scope in which they were declared (unless you use the fully qualified name of the variable). In addition, a variable declared with our is visible across its entire lexical scope, even across package boundaries.

    Here's a table that I added to my Perl notes a while back. For an example, see this SO answer.

                  Scope/     Package
                  Namespace  Variable    Private    New
    ---------------------------------------------------
    my            Lexical    No          Yes        Yes
    our           Lexical    Yes         No         No
    use vars      Package    Yes         No         No
    

提交回复
热议问题