future parser, defined types, and variable scope

126 views
Skip to first unread message

John Sellens

unread,
Dec 15, 2014, 12:07:45 PM12/15/14
to puppet...@googlegroups.com
I couldn't find any reference to this different behaviour, so I'm
wondering if anyone knows if this is intentional.

I use a bunch of defined types, and with the future parser, when I refer
to a variable in a template, it seems that the default scope is not the
scope of the class.

e.g. in a template, <%= @a %> no longer refers to $a from the current
class.

This is different behaviour than with the current parser, and doesn't
seem to match the documentation I've found, or the behaviour with the
standard types.


For example, I have a module called "parsetest" and the init.pp is:

define fileerb ( ) {
file { "/tmp/$name":
content => template( "parsetest/${name}.erb" ),
}
}

class parsetest {

$a = "hello"

fileerb { "bloop" : }

}

The templates/bloop.erb contains:

the value of a is: <%= @a %>

In current puppet, the value of $a ends up in /tmp/bloop.
With the future parser, no value is used, and I don't see
any errors.

I see this in puppet 3.7.3 (from the puppetlabs yum repo) and
also in 3.6.2 (from freebsd ports).


Is it expected that a defined type evaluates templates in a
(seemingly) different scope?

Thanks for any insight!

John

jcbollinger

unread,
Dec 16, 2014, 9:20:43 AM12/16/14
to puppet...@googlegroups.com


On Monday, December 15, 2014 11:07:45 AM UTC-6, John Sellens wrote:
I couldn't find any reference to this different behaviour, so I'm
wondering if anyone knows if this is intentional.

I use a bunch of defined types, and with the future parser, when I refer
to a variable in a template, it seems that the default scope is not the
scope of the class.

e.g. in a template, <%= @a %> no longer refers to $a from the current
class.

This is different behaviour than with the current parser, and doesn't
seem to match the documentation I've found, or the behaviour with the
standard types.



The behavior you describe under the future parser perfectly matches the scope documentation and the templating documentation.  The old behavior you describe is buggy, written up as PUP-1220 (among others).  I don't quite see how it is comparable to anything done by a native type, whether provided with Puppet or not.

 
Is it expected that a defined type evaluates templates in a
(seemingly) different scope?



Yes, it is.  As documented in the scope specifications linked above, defined type bodies establish anonymous local scopes that are not related to the scope associated with the class that declares them.  That should apply to templates' view of variables exactly the same way it applies in the DSL, as the templating docs specifically limit the variables directly available within a template to those that are in (DSL) scope at the point where the template is evaluated.


John

Erik Dalén

unread,
Dec 16, 2014, 9:21:44 AM12/16/14
to puppet...@googlegroups.com
Yes, this is expected. Defined types have their own scope which doesn't inherit variables from the class they are instantiated from.

So if you need the variable in the define, forward it to it using a parameter.


In Puppet 3.x that behaviour worked in templates, but not outside templates. With future parser it is more consistent.
Reply all
Reply to author
Forward
0 new messages