I was debugging a little problem just today and figured out that defined(Class[‘something’]) would return true if in the current scope, there is a class something.
Example:
class foo { notify{'I am class foo': } } class bar::foo { notify {'I am class bar::foo': } if ! defined(Class['foo']) { notify {'foo was not declared yet. do it!': } include foo } } include bar::foo |
This results in
Notice: I am class bar::foo Notice: /Stage[main]/Bar::Foo/Notify[I am class bar::foo]/message: defined 'message' as 'I am class bar::foo'
Not quite what I expected. I added some debug statements in the defined function and figured out that he resolved Class[‘foo’] to Class[‘bar::foo’].
After this, It was pretty easy to fix. Also note that you need to add the ‘::’ when including foo too!
class foo { notify{'I am class foo': } } class bar::foo { if ! defined(Class['::foo']) { notify {'foo was not declared yet. do it!': } include ::foo } } |
Notice: foo was not declared yet. do it! Notice: I am class foo Notice: I am class bar::foo
HURRAY! So, as a general rule, always ::scope everything where you can ;)