Ask Your Question
0

undef variable

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

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

Comments

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 -0600 )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 -0600 )edit

2 Answers

Sort by » oldest newest most voted
0

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

puser gravatar image

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

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
0

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

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 (
  $mandatory_param,
  $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 (
  $mandatory_param,
  # 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

Stats

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

Seen: 809 times

Last updated: Mar 24