Why variables standing by themselves should be quoted in resource titles and only there?

94 views
Skip to first unread message

David Racodon

unread,
Aug 15, 2015, 10:23:44 AM8/15/15
to Puppet Users
Hi,

The Puppet Language Style Guide states at https://docs.puppetlabs.com/guides/style_guide.html#quoting that "Variables standing by themselves should not be quoted, unless they are a resource title". So, it means that you should write:

file { "${foo}":
...
}

instead of:

file { $foo:
...
}

correct?

If so, could you please provide some explanations on why it is recommended to quote variables standing by themselves in resource titles and only there?

Thank you

Regards,

David RACODON
Freelance QA Consultant

Rich Burroughs

unread,
Aug 15, 2015, 10:12:53 PM8/15/15
to Puppet Users
Hi David,

My guess is that this is because of the guideline in section 9.1 that all resource titles be quoted. So that is the area where it should be done for consistency. Otherwise if you have a standalone variable elsewhere in your manifest, it's not necessary to quote it.

Generally the impetus behind the suggestions in the style guide is to make the code as readable as possible. That partly means being consistent (like quoting all titles), and keeping things as simple as possible (not quoting standalone variables, and using single quotes for strings unless there is a variable being interpolated, etc.).

I'm pretty sure I've written code where there were unquoted variables in titles and it worked fine (in Puppet 3.x anyway). You aren't required to use all of the formatting in the style guide, and if you're using puppet-lint you can disable specific checks you don't want to use.


Rich
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c4f51d65-1fb4-4973-a47f-60de2adf24a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Racodon

unread,
Aug 17, 2015, 2:07:01 AM8/17/15
to puppet...@googlegroups.com
Thanks for your feedback Rich.

David RACODON
Freelance QA Consultant

jcbollinger

unread,
Aug 17, 2015, 9:12:05 AM8/17/15
to Puppet Users


On Saturday, August 15, 2015 at 9:12:53 PM UTC-5, Rich Burroughs wrote:
Hi David,

My guess is that this is because of the guideline in section 9.1 that all resource titles be quoted. So that is the area where it should be done for consistency. Otherwise if you have a standalone variable elsewhere in your manifest, it's not necessary to quote it.

Generally the impetus behind the suggestions in the style guide is to make the code as readable as possible. That partly means being consistent (like quoting all titles), and keeping things as simple as possible (not quoting standalone variables, and using single quotes for strings unless there is a variable being interpolated, etc.).

I'm pretty sure I've written code where there were unquoted variables in titles and it worked fine (in Puppet 3.x anyway). You aren't required to use all of the formatting in the style guide, and if you're using puppet-lint you can disable specific checks you don't want to use.


Yes, quoting resource titles is a convention, not a requirement.  In fact, when you use an array title, whether a literal array or a variable whose value is an array, you must not quote it.

As a convention and style matter I absolutely favor quoting scalar resource titles that consist at least partly of literal text, but titles that consist entirely of a variable reference are a gray area.  The quotes provide little clarity benefit, and no consistency benefit (since there are cases where quotes must not be used).  I think a safer interpretation of the conventions is that titles consisting solely of a variable reference may be quoted, not that they should be quoted.  If puppet-lint disagrees then I think I'd account that a bug.


John

David Racodon

unread,
Aug 19, 2015, 2:39:30 AM8/19/15
to puppet...@googlegroups.com
Hi John,

Thanks for your feedback. Actually, puppet-lint goes against this recommendation because it flags quoted resource titles containing only a variable as an issue: "string containing only a variable on line 8".

Regards,

David RACODON
Freelance QA Consultant

--
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.

Henrik Lindberg

unread,
Sep 7, 2015, 11:22:59 PM9/7/15
to puppet...@googlegroups.com
On 2015-19-08 8:39, David Racodon wrote:
> Hi John,
>
> Thanks for your feedback. Actually, puppet-lint goes against this
> recommendation because it flags quoted resource titles containing only a
> variable as an issue: "string containing only a variable on line 8".
>

Which is a different problem, because it is reasonable to interpolate a
value of a non string data type and thereby converting it to a string...

(That is for Puppet 4.x and 3.x with future parser).
- henrik

--

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

Reply all
Reply to author
Forward
0 new messages