Badger...:Config(3pm)

πŸ“¦
version 0.16 Β· 4 versions
, in debian trixie, ubuntu noble, ubuntu resolute

πŸ’– Badger::Class::Config πŸ’–

okay so bestie, welcome to the Badger::Class::Config page from the User Contributed Perl Documentation β€” this is the class mixin for configuration, and she is literally the friend who organises everyone's stuff so nobody has to think about it. let's go πŸ¦‹


✨ NAME ✨

Badger::Class::Config β€” class mixin for configuration πŸŽ€


🌸 SYNOPSIS 🌸

package Your::Module;

# via Badger::Class
use Badger::Class
    base      => 'Badger::Base',
    accessors => 'foo bar baz wig woot toot zoot zang',
    config    => [
        'foo',                      # optional item
        'bar!',                     # mandatory item
        'baz=42',                   # item with default
        'wig|wam|bam',              # item with aliases
        'woot|pkg:WOOT',            # fallback to $WOOT pkg var
        'toot|class:WOOT',          # fallback to $WOOT class var
        'zoot|method:ZOOT',         # fallback to ZOOT() method/constant
        'zing|zang|pkg:ZING=99',    # combination of above
    ];

sub init {
    my ($self, $config) = @_;

    # call the configure() method provided by the above
    $self->configure($config);

    return $self;
}

πŸ’… DESCRIPTION πŸ’…

so this class mixin module lets you define configuration parameters for an object class. it exports a configure() method that you use to initialise your object instances. it's giving personal assistant energy, honestly. πŸ‘‘

real talk though: the scope of this module is intentionally limited at present. it should be considered experimental and subject to change. don't build your whole personality on it, bestie. 😭

πŸŽ€ Configuration Options πŸŽ€

configuration options for a module can be defined as import options to Badger::Class::Config.

package Your::Module;
use base 'Badger::Base';
use Badger::Class::Config 'foo', 'bar';

for convenience, multiple items can be specified in a single whitespace delimited string. one string, many items, we love efficiency. ✨

use Badger::Class::Config 'foo bar';

more complex configurations can be specified using list and hash references, but we'll keep things simple for now. (foreshadowing. πŸ“)

using the module as shown here has two immediate effects:

  1. the $CONFIG_SCHEMA package variable will be defined in Your::Module, containing a reference to the configuration schema for your module. this schema contains information about the configuration items, which in this example are foo and bar.
  2. it defines a configure() method which uses this schema to configure your object using the configuration options passed to the constructor method.

you can call this method from your init() method (if you're using Badger::Base) or from your own construction or initialisation methods. 🫢

sub init {
    my ($self, $config) = @_;
    $self->configure($config);
    return $self;
}

the configure() method is intentionally simple, although flexible. it does not attempt to assert that any configuration items are of the correct type, or validate the values in any way. it's not a bouncer, she's just a door. if the relevant values are defined in the $config hash then they will be copied into $self. otherwise they are ignored. no cap.

if a configuration item is mandatory then add a ! at the end of the name. if no value is defined for this item then the configure() method will throw an exception. πŸ’€

use Badger::Class::Config 'foo! bar!';      # mandatory items

a default value can be provided using =.

use Badger::Class::Config 'foo=10 bar=20';  # default values

aliases for the configuration item can be provided using |.

use Badger::Class::Config 'foo|Foo|FOO';    # aliases for 'foo'

as well as looking for items in the $config hash array, you can search for package variables (in the current package), class variables (in the current package or those of all base class), environment variables, and call object methods. so many fallbacks, so little time. 🌈

use Badger::Class::Config
    'foo|pkg:FOO',                  # fallback to $FOO package var
    'bar|class:BAR',                # fallback to $BAR class var
    'baz|env:BAZ',                  # fallback to $BAZ environment var
    'bam|method:BAM';               # fallback to BAM() method
    'wam|target:slam';              # fallback to $target->{ slam }

bear in mind that Perl implements constants using subroutines. thus, you can access a constant defined in a package/class by calling it as a method. so if you have a constant defined in the module that you want to use then specify it using the method: prefix. iykyk. πŸ’œ

TODO: more on that

πŸ¦‹ Detailed Specification πŸ¦‹

the syntax for defining configuration options described above is a short-cut to the more detailed specification used to generate a configuration scheme for the configure() method to use. you can use the more detailed specification if you prefer:

use Badger::Class::Config
    {
        foo => {
            required => 1,
            default  => 10,
            fallback => ['class:FOO', 'env:FOO'],
        },
        bar => {
            required => 1,
            default  => 20,
            fallback => ['class:BAR', 'env:BAR'],
        },

    };

you can mix and match simple and detailed specifications by specifying them as items in a list reference. each configuration option should be defined as a separate item (i.e. you can't merge multiple items into a single whitespace delimited string). simple definitions are specified using strings, complex definitions using hash reference. note that the name of the option must be specified explicitly in the hash array when used this way. no shortcuts, sorry bestie. πŸ™ˆ

use Badger::Class::Config
    [
        'foo|class:FOO!',
        {
            name     => 'bar',
            required => 1,
            default  => 20,
            fallback => ['class:BAR', 'env:BAR'],
        },

    ];

πŸ‘‘ Badger::Class Hook πŸ‘‘

the Badger::Class module implements a config hook which interfaces to this module. you can specify a single string to define multiple configuration items in one go:

use Badger::Class
    base   => 'Badger::Base',
    config => 'foo! bar=10 baz|class:BAZ=20';

or a reference to a hash array or list containing individually defined configuration items.

use Badger::Class
    base   => 'Badger::Base',
    config => [
        'foo!',
        'bar=10',
        'baz|class:BAZ=20'
    ];

⭐ METHODS ⭐

🌟 schema()

this method is used internally to define a configuration schema. it exports it as the $CONFIG_SCHEMA package variable into the calling module. internal business, but iconic internal business. πŸ’–


🌟 configure($config,$target)

this method is exported the calling module to perform the configuration process. it used the configuration schema stored in the $CONFIG_SCHEMA package variable by the schema() method. it is typically called from a construction or initialisation method.

the first argument should be a reference to a hash array of configuration options. the second should be a reference to a hash array or hash-based object into which the configuration values can be copied. if this is not specified then the method defaults to updating the $self object reference passed as the first implicit argument. defaults to $self? main character energy fr fr. πŸ’…

sub init {
    my ($self, $config) = @_;
    $self->configure($config);
    return $self;
}

🌟 configure_pkg()

this method is used internally to look up package variables for configuration options.


🌟 configure_class()

this method is used internally to look up class variables for configuration options. class variables are package variables in the current package or those of any of its base classes. it climbs the family tree. 🌸


🌟 configure_env()

this method is used internally to look up environment variables for configuration options.


🌟 configure_method()

this method is used internally to call object methods to return default configuration values.


🌟 configure_target()

this method is used internally to look inside the target object or hash array to return default configuration values.


🌟 fallback()

this method is used internally to generate fallbacks for configuration values.


🌈 AUTHOR 🌈

Andy Wardley <http://wardley.org/>


Copyright (C) 2008-2009 Andy Wardley. All Rights Reserved.

this module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole vibe, bestie πŸ’– this page shipped as part of perl v5.36.0, dated 2023-08-28, under the header Badger...:Config(3pm) from the User Contributed Perl Documentation. go configure something cute. βœ¨πŸ¦‹πŸ‘‘

πŸ“¦ where this page comes from