Typecasting
Typecasting is a method for converting data from one data type to another. In many cases, you can explicitly typecast into the type you need, but the Puppet language also provides shortcuts for common data conversions, as well as support for more complex conversions.
Creating a new typed variable
When there's an unambiguous translation between one data type and another, you can typecast into the type you need:
$temp = Float("98.6") # Cast into a float variable; 98.6
$count = Integer("42") # Cast into an integer variable; 42
$bool = Boolean("false") # Cast into a boolean false
$string = String(100) # Cast into the string "100"
$intval = Integer("0xFF") # Cast into an integer variable; 255
Invoking the data type directly is a shortcut for calling the .new()
function. For more information, including the conversion rules and options
available, see the function reference for new()
.
Automatic coercions
strict
is set to error
, which is the default setting in Puppet 8. In Puppet 8,
automatic coercion causes catalog to fail. Example:
puppet apply -e 'notice("2" + "2")'
Error: Evaluation Error: The string '2' was automatically coerced to the numerical value 2 (line: 1, column: 8) on node localhost
String
values to
Integer
values in Puppet code.
Example:puppet apply -e 'notice(Integer("2") + Integer("2"))'
Notice: Scope(Class[main]): 4
To achieve the old behavior,
set strict=warning
in puppet.conf
on the
puppetserver node.
$result = "2" + "2" # Cast into an integer variable; 4
$multiple = 4 * "25" # Cast into an integer variable; 100
$float = "2.75" * 2 # Cast into a float variable; 5.5
$identity = "1024" + 0 # Cast into integer variable; 1024 (direct conversion)
When
you interpolate variables into a string, Puppet converts them to their string
representation using the most obvious method. If you need an explicit conversion,
you can manually typecast. For example:$var = 1
notice("The truth value of ${var} is ${Boolean($var)}") # Outputs "The truth value of 1 is true"
Extracting a Number from a String fragment
To convert a string that contains non-numeric characters into a number, for example
32º
or 9,000
,
you must parse the string. You can parse a string using the scanf
function.
For example:
$input = "It is 32º outside!"
$format = "It is %iº outside!"
scanf($input, $format) # returns array of matches; [32]
$input = "The melting point of iron is 2,800°F".delete(",")
$format = "The melting point of iron is %i°F"
scanf($input, $format) # returns array of matches; [2800]
Here are more
example format conversions:Conversion | Description of match |
---|---|
%d , %u
|
Optionally signed decimal integer |
%i |
Optionally signed integer with auto-detected base |
%o |
Optionally signed octal integer |
%x , %X
|
Optionally signed hexadecimal integer |
%s |
String, or a sequence of non-white-space characters |
%c |
A single character |
%% |
A % character |
Converting to a Boolean
true
/false
, yes
/no
, non-zero/0
,
and integers of non-zero/0
:$value = Boolean('true') # true
$value = Boolean('Yes') # true
$value = Boolean('FALSE') # false
$value = Boolean('1') # true
$value = Boolean(0) # false
$value = Boolean(127) # true
Converting data structures
$an_array = Array({a => 10, b => 20}) # results in [[a, 10],[b, 20]]
$a_hash = Hash([1,2,3,4]) # results in {1=>2, 3=>4}
$a_hash = Hash([[1,2],[3,4]]) # results in {1=>2, 3=>4}
The Array
conversion also takes a second argument to "force" an array conversion. This
slightly changes the semantics of the operation by creating a single-element array
out of the first argument if it's not
already.$an_array = Array(1, true) # results in [1]
$an_array = Array([1], true) # results in [1]
$an_array = Array(1) # Raises a type error, cannot convert directly
$an_array = Array({1 => 2}, true) # results in [{1 => 2}]
$an_array = Array({1 => 2}} # results in [[1, 2]]