Typecasting

Sections

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"
You can also typecast between number formats, such as turning a hex number string into an integer:
$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

When you use strings in arithmetic, Puppet assumes that you did so intentionally and coerces them to the proper types. For example:
$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

You can cast case-insensitive strings of 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

Converting a hash to an array creates a multi-dimensional array by flattening each key-value pair into a two-element array as an element of another array. Converting an array to a hash interleaves alternating elements as keys and values.
$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]]
Puppet sites use proprietary and third-party cookies. By using our sites, you agree to our cookie policy.