4 module Puppet::Parser::Functions
5 newfunction(:getvar, :type => :rvalue, :doc => <<-'DOC') do |args|
6 Lookup a variable in a given namespace.
7 Returns undef if variable does not exist.
11 $foo = getvar('site::data::foo')
12 # Equivalent to $foo = $site::data::foo
14 This is useful if the namespace itself is stored in a string:
16 $datalocation = 'site::data'
17 $bar = getvar("${datalocation}::bar")
18 # Equivalent to $bar = $site::data::bar
20 Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
21 will be used instead of this function. The new function also has support for
22 digging into a structured value. See the built-in
23 [`getvar`](https://puppet.com/docs/puppet/latest/function.html#getvar) function
26 unless args.length == 1
27 raise Puppet::ParseError, "getvar(): wrong number of arguments (#{args.length}; must be 1)"
32 catch(:undefined_variable) do
33 result = lookupvar((args[0]).to_s)
36 # avoid relying on inconsistent behaviour around ruby return values from catch
38 rescue Puppet::ParseError # rubocop:disable Lint/HandleExceptions : Eat the exception if strict_variables = true is set