On Thursday, January 17, 2013 7:40:11 PM UTC-6, iamauser wrote:
>
> My custom function looks like this :
>
> module Puppet::Parser::Functions
> newfunction(:pattern_in_file, :type => :rvalue) do |args|
> filename = args[0]
> pattern = args[1]
> value = false
> File.open(filename) do |f|
> f.each_line do |line|
> return true if line.match(pattern)
> end
> end
> false
> end
> end
>
> ruby -rpuppet and irb tests to see if the function is seen by puppet end
> successfully without any error.
>
> Inside the manifests callings
> pattern_in_file('/etc/fstab', '/usr')
> don't complain or print anything,
>
> If I use :
>
> $getval = pattern_in_file('/etc/fstab', '/usr')
>
> It complains the following :
>
> Error: Could not retrieve catalog from remote server: Error 400 on SERVER:
> Function 'pattern_in_file' does not return a value at
> /etc/puppet/modules/mname/manifests/datavers.pp:3 on node node_name
>
> Any suggestion why the return doesn't work ?
>
Basically, it doesn't work because the Ruby block defining your function's
behavior (do |args| ... end) is exactly that -- a block. The 'return'
statement does not set the value of the block; instead, it attempts to
return from the method that evaluates the block. Without knowing any
details about that method's implementation, it is very dangerous to make
your block attempt to return from it. Indeed, it appears that doing so is
the wrong thing in this case.
You must instead ensure that the block evaluates to the desired value. For
example:
module Puppet::Parser::Functions
newfunction(:pattern_in_file, :type => :rvalue) do |args|
filename = args[0]
pattern = args[1]
File.open(filename) do |f|
f.lines.any? { |line| line.match(pattern) }
end or false
# The "or false" converts nil to false for safety, in
# the event that the specified file does not exist.
end
end
That's cleaner and clearer anyway, at least for this example.
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/puppet-users/-/4jvwgp0O9TQJ.
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-users?hl=en.