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