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

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

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.

Reply via email to