passing arguments by name instead of position for functions

26 views
Skip to first unread message

Matt Zagrabelny

unread,
Oct 23, 2023, 11:18:26 AM10/23/23
to Puppet Users
Hello,

I have a variety of hashes that have similar content:

$h = {
    'foo1' => 'bar',
    [...]
    'foo99' => 'baz',
    'foo100' => 'h',
}

$g = {
    'foo1' => 'bar',
    [...]
    'foo99' => 'baz',
    'foo100' => 'g',
}

I'd like to have a function or similar mechanism:

$h = helper_module::get_hash_defaults()

but be able to override various keys/parameters in the get_hash_defaults function:

$h = helper_module::get_hash_defaults(
    'foo100' => 'h',
)

$h = helper_module::get_hash_defaults(
    'foo100' => 'g',
)

but functions only take parameters by position.

Does anyone have any clever hacks I could try to mimic passing arguments by name?

Thanks for any help!

-m

Jesse Reynolds

unread,
Oct 25, 2023, 4:24:55 AM10/25/23
to Puppet Users
Hi Matt, 

Is your aim to avoid the duplication? If so, perhaps you could just use simple composition, eg: 

$defaults = {
  'foo1' => 'bar',
  #[...]
  'foo99' => 'baz',
}

$h = $defaults + {
  'foo100' => 'h',
}

$g = $defaults + {
  'foo100' => 'g',
}

If you need more flexibility though, hiera might be the nicer way to achieve it. 

Cheers
Jesse
Reply all
Reply to author
Forward
0 new messages