On Tue, Jul 19, 2005 at 07:25:35PM +0100, Matthew Hodgson wrote: : Hi all, : : I've spent some of the afternoon wading through A12 and S10 trying to : thoroughly understand scope in perl 6, in light of the death of use vars : and the addition of class (as well as package & module) namespaces. : : In the process I came up against some confusion concerning how the default : package namespace should work. Currently, pugs does: : : % pugs -e '$main::foo="foo"; say $foo' : foo : : which contradicts S10, which states: : : The "::*" namespace is not "main". The default namespace for the main : program is "::*Main". : : This turned out to be an oversight - but there was then confusion as to : how one actually refers to variables in the "::*Main" namespace, as if : $Foo::bar looks up the ::Foo object and fetches $bar from it, then : presumably $*Main::foo should look up the ::*Main object, and fetch $foo : from it. : : However (from #perl6): : : <autrijus> Arathorn: when you see $Foo::bar, it means looking up the ::Foo : object, then fetch $bar from it : <autrijus> and ::Foo, just like %Foo, can be lexical or package scoped or : global (%*Foo) : <autrijus> to restrict the lookup to ::*Foo you can't use the ordinary : qualifying syntax, I think. : <autrijus> but I may be completely wrong : <Arathorn> so it sounds as if to get the variable $bar from the global : packagename ::*Foo (or just *Foo if disambiguation is not necessary), : you'd use $*Foo::bar then. : <autrijus> that may be the case, yes. : <autrijus> $?Foo::bar means $?bar in Foo:: : <autrijus> but $*Foo::bar can't mean $*bar in Foo:: : <autrijus> because Foo:: will never contain a $*bar. : <autrijus> so it must mean $bar in *Foo:: : <autrijus> this is very weird. : : So the question is: what is the correct syntax for referring to package : variables in the default namespace?
The * looks like a twigil but it isn't really. It's short for "*::", where the * is a wildcard package name, so in theory we could have $=*foo, meaning the $=foo in the *:: package. (But most of the twigils imply a scope that is immiscible with package scope.) : Also, what is the correct syntax for referring to package variables in : your 'current' namespace? $::foo? $?PACKAGENAME::foo? : $::($?PACKAGENAME)::foo? %PACKAGENAME::<foo>? That's currently: $OUR::foo though presumably $::($?PACKAGENAME)::foo would also work as a symbolic reference. I'm not sure whether $::foo is usefully distinct from $foo these days. It almost seems to imply that ::foo is in type space and we have to dereference it somehow. There's a sense in which :: only implies type space after a name. We somehow seem to have the situation where :: is simultaneously trying to be a leading sigil, a trailing sigil, and a separator. Larry