ERB variable scope

44 views
Skip to first unread message

Daniel Urist

unread,
Oct 6, 2017, 2:28:26 PM10/6/17
to puppet-users
I've noticed in classes with multiple templates that ERB variables seem to share a single scope. That caught me by surprise-- is that intended behavior?

Here's a simple test case:

class erb_scope_test (
)
{
  $myarr1 = [ 'one' ]
  $myarr2 = [ 'two' ]

  $str1 = inline_template("<%= @myarr1.concat(@myarr2) %>")
  notify { $str1: }

  $str2 = inline_template("<%= @myarr1.concat(@myarr2) %>")
  notify { $str2: }
}

Notice: ["one", "two"]
Notice: ["one", "two", "two"]

Henrik Lindberg

unread,
Oct 6, 2017, 6:17:23 PM10/6/17
to puppet...@googlegroups.com
On 06/10/17 11:28, Daniel Urist wrote:
> I've noticed in classes with multiple templates that ERB variables seem
> to share a single scope. That caught me by surprise-- is that intended
> behavior?
>

Intended or not - that is the behavior, and this is Ruby so it is
possible to mutate things that should be immutable.

There is great responsibility when writing code in ERB because of the
generality and power available in Ruby. Suggest using EPP instead where
problems like this one does not occur.

Best,
- henrik

> Here's a simple test case:
>
> class erb_scope_test (
> )
> {
>   $myarr1 = [ 'one' ]
>   $myarr2 = [ 'two' ]
>
>   $str1 = inline_template("<%= @myarr1.concat(@myarr2) %>")
>   notify { $str1: }
>
>   $str2 = inline_template("<%= @myarr1.concat(@myarr2) %>")
>   notify { $str2: }
> }
>
> Notice: ["one", "two"]
> Notice: ["one", "two", "two"]
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CAEo6%3DKbd8wMO9rvzHMe4W-bo%2BizqLxyAyEf4ThWScOAgpL%2BkdA%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-users/CAEo6%3DKbd8wMO9rvzHMe4W-bo%2BizqLxyAyEf4ThWScOAgpL%2BkdA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Daniel Urist

unread,
Oct 9, 2017, 10:42:35 AM10/9/17
to puppet-users
Thanks very much. 

It would be great if the template doc page (https://docs.puppet.com/puppet/5.3/lang_template.html) gave more guidance on epp vs erp, use cases for each and possible gotchas like this.

On Fri, Oct 6, 2017 at 4:17 PM, Henrik Lindberg <henrik....@puppet.com> wrote:
On 06/10/17 11:28, Daniel Urist wrote:
I've noticed in classes with multiple templates that ERB variables seem to share a single scope. That caught me by surprise-- is that intended behavior?


Intended or not - that is the behavior, and this is Ruby so it is possible to mutate things that should be immutable.

There is great responsibility when writing code in ERB because of the generality and power available in Ruby. Suggest using EPP instead where problems like this one does not occur.

Best,
- henrik

Here's a simple test case:

class erb_scope_test (
)
{
   $myarr1 = [ 'one' ]
   $myarr2 = [ 'two' ]

   $str1 = inline_template("<%= @myarr1.concat(@myarr2) %>")
   notify { $str1: }

   $str2 = inline_template("<%= @myarr1.concat(@myarr2) %>")
   notify { $str2: }
}

Notice: ["one", "two"]
Notice: ["one", "two", "two"]

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com <mailto:puppet-users+unsubscrib...@googlegroups.com>.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/or8vca%242te%241%40blaine.gmane.org.

warron.french

unread,
Oct 9, 2017, 5:39:48 PM10/9/17
to puppet...@googlegroups.com
At least you are on PE 5.3, Red Hat Satellite's implementation of PE-3.8 doesn't even support EPP templates.

--------------------------
Warron French


Reply all
Reply to author
Forward
0 new messages