Issue #2826 has been updated by Kaspar  Schiess.

The title rename strikes me as odd. The problem is that variable access through 
the method missing mechanism is broken, not the instance variable mechanism. So 
the title should probably really be
*undef variables should not return :undef but nil when accessed through 
method_missing*!


----------------------------------------
Bug #2826: undef variables should not be set as instance variables in templates
http://projects.reductivelabs.com/issues/2826

Author: Kaspar  Schiess
Status: Accepted
Priority: Normal
Assigned to: 
Category: language
Target version: 0.25.3
Affected version: 0.25.1
Keywords: undef, undefined, template, inline_template
Branch: 


The legacy way of accessing variables in templates (through method_missing and 
has_variable?) doesn't behave properly when in the presence of 'undef'-values. 
Look at this:  

  define test($foo = undef) {
    file {
      $title:
        content => inline_template(" 
        Instance variable: <%= @foo.inspect %>
        Method accessor (legacy): <%= foo.inspect %>
        Has Variable :foo <%= has_variable?('foo') %>
        ")
    }
  }
  test { 
    '/tmp/undef3':
  }
  
Output in /tmp/undef3 will be: 

  Instance variable: nil
  Method accessor (legacy): :undef
  Has Variable :foo true

Compare to the output of this: (This time, the variable really isn't defined, 
not just set to undef)

  define test() {
    file {
      $title:
        content => inline_template(" 
        Instance variable: <%= @foo.inspect %>
        Has Variable :foo <%= has_variable?('foo') %>
        ")
    }
  }
  test { 
    '/tmp/undef5':;
  }

Output will be: 

  Instance variable: nil
  Has Variable :foo false
  
Note that accessing the variable through the method accessor will raise an 
Exception - this is why I didn't include it in the sample. 

To sum up: Setting a variable to undef will make it look like it is defined 
(has_variable? == true). My test for this condition will have to be 'foo == 
:undef', exposing the internal value :undef to the template. If the variable 
really isn't set (when reusing the template for example), I get an exception 
raised, so the test really must be (legacy style): 

  <% if has_variable?('foo') && foo != :undef %>
    Do something with foo here
  <% end %>
  
New style code looks much nicer, since both cases return nil: 

  <% if @foo %>
    Do something with foo
  <% end %>
  
IMHO, the legacy way should be fixed to work as well, as long as that is 
supported at least. Otherwise we really have more than one value for 'undef'. 
And that's bad. 


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://reductivelabs.com/redmine/my/account

--

You received this message because you are subscribed to the Google Groups 
"Puppet Bugs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-bugs?hl=en.


Reply via email to