On Sep 16, 2010, at 3:36 PM, Luke Kanies wrote:

> On Sep 16, 2010, at 12:45 PM, Paul Berry wrote:
> 
>> I'm trying to get a better understanding of how variable scoping is
>> intended to work in Puppet.  Based on my reading of the code (and some
>> experiments), here's the behaviors that I think are intended:
>> 
>> 1. Variables defined at toplevel can be seen inside classes.  For example:
>> 
>> $var = "value"
>> class foo { notify { $var: } }
>> include foo
> 
> This is a good idea and should stay, short and long term.
> 
>> 2. Variables defined in a class can be seen inside the classes that it
>> includes (this is really a generalization of (1)).  Some might call
>> this "dynamic scoping".  For example:
>> 
>> class foo {
>>     $var = "value"
>>     include bar
>> }
>> class bar { notify { $var: } }
>> include foo
> 
> From this on down, we're basically dealing with dynamic scoping, and in 
> general, I think it should be fixed, and by fixed, I essentially mean removed.

I should have been a bit more clear and less, I guess, flippant here.  More 
comments below.

Further, as Nigel is hammering home, we shouldn't necessarily treat resource 
defaults differently, but we should very much make sure we consider them 
separately, since they have different use cases.

>> 3. Conflicts in (2) are resolved in favor of the class that is closer
>> in the include chain.  For example:
>> 
>> class foo {
>>     $var = "value 1"
>>     include bar
>> }
>> class bar {
>>     $var = "value 2"
>>     include baz
>> }
>> class baz { notify { $var: } } # resolves as "value 2"
>> include foo
>> 
>> 4. Variables defined in a base class can be seen in derived classes.
>> This takes precedence over (1) and (2).
>> 
>> $var = "toplevel value"
>> class foo {
>>     $var = "foo value"
>>     include derived
>> }
>> class base { $var = "base value" }
>> class derived inherits base { notify { $var: } } # resolves as "base value"
>> include foo
>> 
>> 5. Variables defined in a class can be seen inside base classes of the
>> classes that it includes.  For example:
>> 
>> class foo {
>>     $var = "value"
>>     include derived
>> }
>> class base { notify { $var: } }
>> class derived inherits base { }
>> include foo

Everything up to here is essentially intentional, although as my first comment 
implied, not necessarily something I want to have long-term.

>> 6. In general, variables defined in a class cannot be seen inside
>> nested classes.  That is, there is no lexical scoping.  For example:
>> 
>> class foo {
>>     $var = "value"
>>     class bar { notify { $var: } } # doesn't work
>> }
>> include foo::bar

This is more of a natural consequence of the dynamic scoping than a feature I 
specifically intended.

>> 7. If a class (or one of its derived classes) is included from
>> multiple places, Puppet respects only the first "include" for that
>> class that it evaluated.  For example, this works because "bar"
>> is included from "foo" before it's included from toplevel:
>> 
>> class foo {
>>    $var = "value"
>>    include bar
>> }
>> class bar { notify { $var: } } # works
>> include foo
>> include bar
>> 
>> But this doesn't work because "bar" is included from toplevel first:
>> 
>> class foo {
>>    $var = "value"
>>    include bar
>> }
>> class bar { notify { $var: } } # doesn't work
>> include bar
>> include foo
>> 
>> Note: a more complex example of this can be found here:
>> http://projects.puppetlabs.com/issues/4748

I've always considered this to be a bug, and an unsolvable one at that.

>> Can someone help me understand which of these behaviors is intentional
>> and/or desirable?  Behavior 7 in particular seems problematic, since
>> it introduces an order dependency.  I want to make sure we fix bug 4748 in a
>> way that doesn't compromise the other behaviors we want to keep.


-- 
Always behave like a duck - keep calm and unruffled on the surface but
paddle like the devil underneath.   -- Jacob Braude
---------------------------------------------------------------------
Luke Kanies  -|-   http://puppetlabs.com   -|-   +1(615)594-8199



-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to