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

Re^3: 'require/use' scoping ("scope")

by brian_d_foy (Abbot)
on Jan 29, 2008 at 17:11 UTC ( #664930=note: print w/replies, xml ) Need Help??

in reply to Re^2: 'require/use' scoping ("scope")
in thread 'require/use' scoping

Package variables are visible throughout the entire program. They have no scope. Don't confuse the default package with a scope. A default package doesn't limit the extent or effect of a variable. To use "scope" in that sense only confuses people and makes the word useless. In your sense, you could also talk about the "scope" of hash keys. It's not a useful concept when you extend it like that, and that's not it's meaning when you're talking Perl.

local has nothing to do with the scope of the variable. It deals with the value of the variable during a lexical scope (but not within a lexical scope), and only works on package variables. The variable's value is available outside of its lexical scope (as in subroutines and so on).

See the entry for package in perlfunc for details.

Update: Tye, think what you like, but Perl isn't Wikipedia. When you talk about (Update: variable) scope in Perl, the time it makes any sense is in lexical scoping. Any other use of the concept is just wanking. It's not my personal definition: it's how it's used in the Perl documentation, which doesn't have any other (Update: variable) scope to talk about. Despite you're repeated claim about a package variable being scoped, it's just not true. It's always available everywhere in the program. The small matter of syntax about referencing it doesn't matter. As for what local does, don't conflate how it does it behind the scenes with what the feature is. You might also want to read Dominus's Coping with Scoping".

brian d foy <>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^4: 'require/use' scoping ("scope")
by runrig (Abbot) on Jan 29, 2008 at 21:43 UTC
    local has nothing to do with the scope of the variable...(snip)...the time it makes any sense is in lexical's how it's used in the perl documentation, which doesn't have any other scope to talk about...(snip) Dominus's Coping with Scoping.

    See perldoc -q "dynamic and lexical"

    Also, in Dominus' addendum article Seven Useful Uses of Local there's some stuff on dynamic scoping.

    Update: also (on an unrelated note), in your node above, the comment in this line is incorrect (as pointed out by tye in the CB):

    require "lib/"; # no @INC searching!
Re^4: 'require/use' scoping ("scope")
by tye (Sage) on Jan 29, 2008 at 17:52 UTC

    I guess you need to scour the web and literature and implement your new personal definition of "scope" (which appears to only include "lexical scope of variables").

    For example, Wikipedia knows that there are non-lexical types of scope including dynamic scope, that scope can apply to values not just variables, and that "a namespace is a scope". Although Foldoc unfortunately defines scope tersely in a way that implies only lexical scopes but this poor definition is contradicted by its own entries for dynamic scope and lexical scope.

    A namespace is a scope and it does limit the effect of a variable; that's the point. You don't want your choice of variable names to affect the code that doesn't use your namespace. A package variable is limited in scope to only places that reference the owning namespace (either by declaring that they are in that package or by prepending the package name in order to access the variable). You can even access lexical variables outside of their scope if you take extra steps. The scope talks about where a simple $name will work.

    local has nothing to do with the scope of the variable. It deals with the value

    Yeah, that was what I said. But it is more accurate to say that local effects a new instance of a variable (and assigns that instance a new value) that has a dynamic scope. The value of this new instance can be changed but the instance will be destroyed when the program leaves its dynamic scope.

    Update: Brian, think what you want. I heard about dynamic scope because the transition from Perl 4 to Perl 5 was a transition from only supporting dynamic scope to supporting lexical scope. If the authors of some of the Perl documentation now choose to not use "scope" except when talking about lexical scope, then I don't have a problem with that. But the programming world is not the Perl documentation and claiming "a namespace isn't a scope" is untrue. You can claim "the Perl documentation avoids referring to a namespace as a scope, preferring to reserve that term for only lexical scopes". But you don't get to define what the word "scope" means outside of that little world. You can even say "I wouldn't call a package a scope". I don't happen to live in the Perl documentation and care about more than how such chooses to use words.

    You can choose to use a more narrow definition of "scope" and you won't even run into any problems so long as you realize you have narrowed the definition and so avoid boldly declaring what isn't included in your narrowed definition (without qualifiers to denote to others that it is a non-standard usage).

    - tye        

Re^4: 'require/use' scoping ("scope")
by shmem (Chancellor) on Jan 29, 2008 at 21:34 UTC
    When you talk about scope in Perl, the time it makes any sense is in lexical scoping. Any other use of the concept is just wanking.

    Well, not quite. I can switch, in any lexical scope, the package. Doing so, as long as the package switch lasts, I can access thingies of that package - subroutines, globs, variables (except file scoped things like my() variables) - without qualifying them. It's a scope! and I can enter and leave it whenever I want! There's more to scopes in perl than just blocks!

    A scope is de- and confined by its boundaries. Do you have a better "synthesis term" for what tye, I and you are saying? If you haven't, putting off those considerations as "wanking" is just... er, can't get an appropriate term right now; have to think about it...


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-04-17 06:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found