Ask Your Question

undef variable

asked 2017-03-23 14:28:21 -0500

WiktorVip gravatar image

Hello All I have puppet master in 3.8 version. I need part of code for undefined variable. Example: if var is undefined setup some value something like this but is not working.

if $my_parameter  == 'undef' {
  $my_parameter = 'none'

if variable is defined don't change it

edit retag flag offensive close merge delete


PP is not a procedural language. All “variables” have a final value with their first assignment. Also you're comparing against the string 'undef'. You want to check against the literal undef (not surrounded by single quotes).

Kai Burghardt gravatar imageKai Burghardt ( 2017-03-23 17:15:24 -0500 )edit

I made only example what I need.. there is no any way to make condition like this?

WiktorVip gravatar imageWiktorVip ( 2017-03-24 06:35:51 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-03-24 06:36:57 -0500

puser gravatar image

updated 2017-03-24 06:40:45 -0500

if $myparameter == undef { $myparameter = 'none' }

p.s. If I was the manager of whoever wrote this code they would get a talking to.

edit flag offensive delete link more

answered 2017-03-24 07:13:22 -0500

DarylW gravatar image

The pattern that I most commonly see for this type of 'workflow' internal to a module (not across module boundries) is to use _ to prefix a private variable.

class foo (
  $optional_param = undef,
  # either using a selector to set a 'default' value, usually used if I am munging multiple params, or if one potential default value depends on a different param (like paths relative to a base path)
  $_optional_param = $optional_param ? {
    undef          => 'none',
    default            => $optional_param,

Or more generally, just use a default. value, and conditionally assign a different parameter to use in a different location that 'requires' undef.

class bar (
  # Or just use a default value explicitly
  $optional_param = 'none',
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-23 14:28:21 -0500

Seen: 57 times

Last updated: Mar 24