Dick Davies
2009-Oct-24 10:29 UTC
[Puppet Users] BUG? $name inside a definition ''method signature'' different to within a body ?
If I have something like this: ---------------------------------------- define bar($thing="/tmp/$name") { file { $thing: ensure => present } } class foo { somedef{ "bar": } } ---------------------------------------- puppet will try to create a file called ''/tmp/foo'' , not /tmp/bar. It seems like if I try to access $name inside the ''default arguments'' bit of a definition, it''s set to the enclosing class. I want to get at the name of the definition (''bar'' in the above example). Once i''m inside the body of the definition, $name seems to be set correctly (inside templates called from the definition, etc.). [I''m writing an apache module, and want to infer a default docroot of ''/docroot/www-vhostname'', but allow an option to override it. how can I do that ? (and is this a bug?) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Lathrop
2009-Oct-24 17:37 UTC
[Puppet Users] Re: BUG? $name inside a definition ''method signature'' different to within a body ?
On Sat, Oct 24, 2009 at 3:29 AM, Dick Davies <rasputnik@hellooperator.net> wrote:> > If I have something like this: > > ---------------------------------------- > > define bar($thing="/tmp/$name") { > file { $thing: ensure => present } > } > > > class foo { somedef{ "bar": } } > > ---------------------------------------- > > puppet will try to create a file called ''/tmp/foo'' , not /tmp/bar. > It seems like if I try to access $name inside the ''default arguments'' > bit of a definition, > it''s set to the enclosing class. > > I want to get at the name of the definition (''bar'' in the above example). > > Once i''m inside the body of the definition, $name seems to be set correctly > (inside templates called from the definition, etc.). > > [I''m writing an apache module, and want to infer a default docroot of > ''/docroot/www-vhostname'', > but allow an option to override it. > > how can I do that ? (and is this a bug?)This isn''t a bug. It is a natural consequence of scoping. define bar() { $thing = "/tmp/$name" file { $thing: ensure => present } } should do what you want. --Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Dick Davies
2009-Oct-25 08:02 UTC
[Puppet Users] Re: BUG? $name inside a definition ''method signature'' different to within a body ?
Thanks, I''ve seen that work but the trouble is I want to be able to pass in a $docroot option, or default to something if one is not there; I suppose I can probably get around it by checking if $docroot is defined and if not setting it or something? Thanks again. On Sat, Oct 24, 2009 at 5:37 PM, Paul Lathrop <paul.lathrop@gmail.com> wrote:> > On Sat, Oct 24, 2009 at 3:29 AM, Dick Davies > <rasputnik@hellooperator.net> wrote: >> >> If I have something like this: >> >> ---------------------------------------- >> >> define bar($thing="/tmp/$name") { >> file { $thing: ensure => present } >> } >> >> >> class foo { somedef{ "bar": } } >> >> ---------------------------------------- >> >> puppet will try to create a file called ''/tmp/foo'' , not /tmp/bar. >> It seems like if I try to access $name inside the ''default arguments'' >> bit of a definition, >> it''s set to the enclosing class. >> >> I want to get at the name of the definition (''bar'' in the above example). >> >> Once i''m inside the body of the definition, $name seems to be set correctly >> (inside templates called from the definition, etc.). >> >> [I''m writing an apache module, and want to infer a default docroot of >> ''/docroot/www-vhostname'', >> but allow an option to override it. >> >> how can I do that ? (and is this a bug?) > > This isn''t a bug. It is a natural consequence of scoping. > > define bar() { > $thing = "/tmp/$name" > file { $thing: ensure => present } > } > > should do what you want. > > --Paul > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Meier
2009-Oct-25 12:30 UTC
[Puppet Users] Re: BUG? $name inside a definition ''method signature'' different to within a body ?
Hi> Thanks, I''ve seen that work but the trouble is I want to be able to > pass in a $docroot option, > or default to something if one is not there; I suppose I can probably > get around it by checking > if $docroot is defined and if not setting it or something?how about: define bar($docroot = undef) { if $docroot == $undef { $real_docroot = "/tmp/$name" } else { $real_docroot = $docroot } file { $real_docroot: ensure => present } } cheers pete --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Dick Davies
2009-Oct-27 09:45 UTC
[Puppet Users] Re: BUG? $name inside a definition ''method signature'' different to within a body ?
Nice one Pete - that''s what I was aiming for, but you''ve probably saved me a day and half a bottle of rum getting there :) On Sun, Oct 25, 2009 at 12:30 PM, Peter Meier <peter.meier@immerda.ch> wrote:> > Hi > >> Thanks, I''ve seen that work but the trouble is I want to be able to >> pass in a $docroot option, >> or default to something if one is not there; I suppose I can probably >> get around it by checking >> if $docroot is defined and if not setting it or something? > > how about: > > define bar($docroot = undef) { > if $docroot == $undef { $real_docroot = "/tmp/$name" } > else { $real_docroot = $docroot } > > file { $real_docroot: ensure => present } > } > > cheers pete > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Meier
2009-Oct-27 10:13 UTC
[Puppet Users] Re: BUG? $name inside a definition ''method signature'' different to within a body ?
> Nice one Pete -except the minor typo: if $docroot == $undef { $real_docroot = "/tmp/$name" } vs. if $docroot == undef { $real_docroot = "/tmp/$name" } but I hope you spotted that and now have anyway time to get your bottle of rum down. ;) cheers pete --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---