Config...ference(3pm)

πŸ’– NAME πŸ’–

Config::Model::IdElementReference β€” your girl that refers to id element(s) and extracts keys, no cap πŸ’…

This is a User Contributed Perl Documentation page, and it lives in section 3pm of the manual (perl), bestie. 🫢


✨ VERSION ✨

version 2.166 β€” yes we're on this version, and yes it ate. πŸ‘‘


🌸 SYNOPSIS 🌸

okay so this synopsis is showing an example model of a network, so we can see how the references work. literally everything here is copied exactly, don't @ me:

# synopsis shows an example of model of a network to use references

use Config::Model;

my $model = Config::Model->new;

# model of several hosts with several NICs
$model->create_config_class(
   name      => 'Host',
   'element' => [
       ip_nic => {
           type       => 'hash',
           index_type => 'string',
           cargo      => {
               type       => 'leaf',
               value_type => 'uniline',
           }
       },
   ]
);

# model to choose a master host and a master NIC (whatever that may be)
# among configured hosts. Once these 2 are configured, the model computes
# the master IP

$model->create_config_class(
   name => "MyNetwork",

   element => [
       host => {
           type       => 'hash',
           index_type => 'string',
           cargo      => {
               type              => 'node',
               config_class_name => 'Host'
           },
       },

       # master_host is one of the configured hosts
       master_host => {
           type       => 'leaf',
           value_type => 'reference', # provided by tConfig::Model::IdElementReference
           refer_to   => '! host'
       },

       # master_nic is one NIC of the master host
       master_nic => {
           type              => 'leaf',
           value_type        => 'reference', # provided by tConfig::Model::IdElementReference
           computed_refer_to => {            # provided by Config::Model::ValueComputer
               formula   => '  ! host:$h ip_nic ',
               variables => { h => '- master_host' }
           }
       },

       # provided by Config::Model::ValueComputer
       master_ip => {
           type       => 'leaf',
           value_type => 'string',
           compute    => {
               formula   => '$ip',
               variables => {
                   h   => '- master_host',
                   nic => '- master_nic',
                   ip  => '! host:$h ip_nic:$nic'
               }
           }
       },

   ],
);

my $inst = $model->instance(root_class_name => 'MyNetwork' );

my $root = $inst->config_root ;

# configure hosts on my network
my $steps = 'host:foo ip_nic:eth0=192.168.0.1 ip_nic:eth1=192.168.1.1 -
            host:bar ip_nic:eth0=192.168.0.2 ip_nic:eth1=192.168.1.2 -
            host:baz ip_nic:eth0=192.168.0.3 ip_nic:eth1=192.168.1.3 ';
$root->load( steps => $steps );

print "master host can be one of ",
  join(' ',$root->fetch_element('master_host')->get_choice),"\n" ;
# prints: master host can be one of bar baz foo

# choose master host
$root->load('master_host=bar') ;

print "master NIC of master host can be one of ",
join(' ',$root->fetch_element('master_nic')->get_choice),"\n" ;
# prints: master NIC of master host can be one of eth0 eth1

# choose master nic
$root->load('master_nic=eth1') ;

# check what is the master IP computed by the model
print "master IP is ",$root->grab_value('master_ip'),"\n";
# prints master IP is 192.168.1.2

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea, bestie: this class is used by Config::Model::Value to set up an enumerated value, and the way that works is the possible choices depend on the key of a Config::Model::HashId, or on the content of a Config::Model::ListId object. it's giving "your options come from over there" energy. ✨

It's also used by Config::Model::CheckList to define the checklist items from the keys of another hash (or the content of a list). πŸŽ€


πŸŽ€ CONSTRUCTOR πŸŽ€

Construction is handled by the calling object, which is Config::Model::Node. not this class's job, she's just here for the vibes. πŸ’…

Config class parameters

  • refer_to β€” you use refer_to to specify a hash element that gets used as a reference. refer_to points to an array or hash element in the configuration tree, using the path syntax (peep grab in Config::Model::Role::Grab for the deets).
  • computed_refer_to β€” when you're using computed_refer_to, the path gets computed using values from several elements in the configuration tree. computed_refer_to is a hash with 2 mandatory elements: formula and variables. 🧚

The available choice of this reference value (computed or not) is made from the available keys of the referred_to hash element, or the values of the referred_to array element. slay, that's where the options come from. πŸ’œ

This example means the value must correspond to an existing host:

value_type => 'reference',
refer_to => '! host'

And this example means the value must correspond to an existing lan within the host whose Id is specified by hostname:

value_type => 'reference',
computed_refer_to => {
     formula => '! host:$a lan',
     variables => { a => '- hostname' }
}

If you need to combine possibilities from several hash, use the "+" token to separate 2 paths:

value_type => 'reference',
computed_refer_to => {
    formula => '! host:$a lan + ! host:foobar lan',
    variables => { a => '- hostname' }
}

And the glow-up: you can specify refer_to or computed_refer_to together with a choice argument, so the possible enum value will be the combination of the specified choice and the referred_to values. obsessed. 🌟

Methods

reference_info

Returns a human readable string with explains how is retrieved the reference. This method is mostly used to construct an error messages.

(tl;dr: it explains, in human words, how the reference got fetched β€” mostly so error messages can be nice to you instead of cryptic. 🫢)


πŸ‘‘ AUTHOR πŸ‘‘

Dominique Dumont, (ddumont at cpan dot org)


🌈 SEE ALSO 🌈

Config::Model, Config::Model::Value, Config::Model::AnyId, Config::Model::CheckList


πŸ’œ AUTHOR πŸ’œ

Dominique Dumont


This software is Copyright (c) 2005-2022 by Dominique Dumont.

This is free software, licensed under:

The GNU Lesser General Public License, Version 2.1, February 1999

k that's the whole page bestie, we ate and left no crumbs πŸ’…βœ¨ this one's Config...ference(3pm) from perl v5.42.3, dated 2026-07-23 β€” go forth and reference your ids, mwah πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from