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.
The primary reason for the dynamic scoping was the need for an including class
to be able to configure an included class, and that can now be done far more
sanely (and easily) with parameterized classes.
Thus, the rest of what you have should, IMO, eventually be removed and replaced
with full lexical scoping.
Make sense?
> 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
>
> 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
>
> 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
>
>
> 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.
>
> Thanks.
>
> Paul
>
>
> --
> 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.
--
If it jams, force it. If it breaks, it probably needed replacing
anyway. -- Lowrey's Law
---------------------------------------------------------------------
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.