On Wed, Aug 17, 2005 at 02:15:56PM -0400, Stevan Little wrote:
: So, onto my question, I am wondering what are the valid scopes for 
: $?SELF and $?CLASS.
: 
: Are these (magical) globals who only have bound values in certain 
: contexts? If that is so, what value do they have outside of a valid 
: context? undef? or is attempting to accessing the value a runtime 
: exception?
: 
: Or ...
: 
: Is it a syntax error to access them outside of a valid scope, and 
: therefore caught at compile time? In this case, then it seems to me 
: that they are not behaving as global variables so much as they are 
: behaving like language keywords.

They are variables known to the compiler but whose bindings change
during the course of the compile, so they tend to behave more like
lexically scoped entities, at least for any of them that the compiler
to save and restore.  In a sense, they're lexical to the program
but dynamic to the compiler.  So something like $?LINE does not
necessarily scope lexically, but the compiler does know where $?SELF
is valid and where it isn't, and which invocant to map it to when
it is valid.

: Now as for the valid contexts.
: 
: The obvious one is that they are both valid within a method. I asumme 
: that $?SELF is bound to the invocant, and $?CLASS is bound to the class 
: the method was defined within. It seems to me that this also mean that 
: in a class method, that $?SELF == $?CLASS?
: 
: Also (IIRC) we discussed $?CLASS being valid inside a class Foo { ... } 
: block at the hackathon. Would mean that something like this should be 
: possible.
: 
:   class FooLoggerProxy is Foo {
:       has Logger $.logger;
:       for ($?CLASS.meta.superclasses()) -> $super {
:           for ($super.meta.getmethods()) -> $method {
:              $?CLASS.meta.add_method($method.label => method {
:                   $?SELF.logger.log($method.label ~ " has been called");
:                   return $method.do([EMAIL PROTECTED])
:              });
:               }
:       }
:   }
: 
: I am not sure if there are any other valid contexts other than inside a 
: method or a class composition block. At least none that I can think of.

If there are more they will become obvious as we go along.  $? variables
that scope lexically are probably just "temp" or "let" variables in
the Perl grammar, so they won't be hard to monkey with if we need to.

Larry

Reply via email to